Update ZMRequest.php

This commit is contained in:
Whale 2020-06-05 19:24:56 +08:00 committed by GitHub
parent 775672d515
commit e67958a8d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,23 +16,22 @@ class ZMRequest
* @param array $headers
* @param array $set
* @param bool $return_body
* @param array $data 请求body
* @return bool|string|Client
* @version 1.1
* 返回请求后的body
* 如果请求失败或返回状态不是200则返回 false
*/
public static function get($url, $headers = [], $set = [], $return_body = true, $data = []) {
public static function get($url, $headers = [], $set = [], $return_body = true) {
$parse = parse_url($url);
if (!isset($parse["host"])) {
Console::warning("ZMRequest: url must contains scheme such as \"http(s)\"");
return false;
}
if(!isset($parse["path"])) $parse["path"] = "/";
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
$cli->setHeaders($headers);
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
$cli->setData($data);
$cli->get($parse["path"] . (isset($parse["query"]) ? "?" . $parse["query"] : ""));
if ($return_body) {
if ($cli->errCode != 0 || $cli->statusCode != 200) return false;
@ -54,19 +53,18 @@ class ZMRequest
* @param $data
* @param array $set
* @param bool $return_body
* @param array $data 请求body
* @return bool|string|Client
*/
public static function post($url, array $header, $data, $set = [], $return_body = true, $data = []) {
public static function post($url, array $header, $data, $set = [], $return_body = true) {
$parse = parse_url($url);
if (!isset($parse["host"])) {
Console::warning("ZMRequest: url must contains scheme such as \"http(s)://\"");
return false;
}
if(!isset($parse["path"])) $parse["path"] = "/";
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
$cli->setData($data);
$cli->setHeaders($header);
$cli->post($parse["path"] . (isset($parse["query"]) ? ("?" . $parse["query"]) : ""), $data);
if ($return_body) {
@ -98,4 +96,34 @@ class ZMRequest
public static function session($option) {
return Saber::session($option);
}
public static function request($url, $attribute = [], $return_body = true) {
$parse = parse_url($url);
if (!isset($parse["host"])) {
Console::warning("ZMRequest: url must contains scheme such as \"http(s)://\"");
return false;
}
if(!isset($parse["path"])) $parse["path"] = "/";
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
$cli->set($attribute["set"] ?? ["timeout" => 15.0]);
$cli->setMethod($attribute["method"] ?? "GET");
$cli->setHeaders($attribute["headers"] ?? []);
if(isset($attribute["data"])) $cli->setData($attribute["data"]);
if(isset($attribute["file"])) {
foreach($attribute["file"] as $k => $v) {
$cli->addFile($v["path"], $v["name"], $v["mime_type"] ?? null, $v["filename"] ?? null, $v["offset"] ?? 0, $v["length"] ?? 0);
}
}
$cli->execute($parse["path"] . (isset($parse["query"]) ? "?" . $parse["query"] : ""));
if ($return_body) {
if ($cli->errCode != 0 || $cli->statusCode != 200) return false;
$a = $cli->body;
$cli->close();
return $a;
} else {
$cli->close();
return $cli;
}
}
}