mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-24 00:55:35 +08:00
Update ZMRequest.php
This commit is contained in:
@@ -16,23 +16,22 @@ class ZMRequest
|
|||||||
* @param array $headers
|
* @param array $headers
|
||||||
* @param array $set
|
* @param array $set
|
||||||
* @param bool $return_body
|
* @param bool $return_body
|
||||||
* @param array $data 请求body
|
|
||||||
* @return bool|string|Client
|
* @return bool|string|Client
|
||||||
* @version 1.1
|
* @version 1.1
|
||||||
* 返回请求后的body
|
* 返回请求后的body
|
||||||
* 如果请求失败或返回状态不是200,则返回 false
|
* 如果请求失败或返回状态不是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);
|
$parse = parse_url($url);
|
||||||
if (!isset($parse["host"])) {
|
if (!isset($parse["host"])) {
|
||||||
Console::warning("ZMRequest: url must contains scheme such as \"http(s)\"");
|
Console::warning("ZMRequest: url must contains scheme such as \"http(s)\"");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if(!isset($parse["path"])) $parse["path"] = "/";
|
||||||
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
|
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
|
||||||
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
|
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
|
||||||
$cli->setHeaders($headers);
|
$cli->setHeaders($headers);
|
||||||
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
|
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
|
||||||
$cli->setData($data);
|
|
||||||
$cli->get($parse["path"] . (isset($parse["query"]) ? "?" . $parse["query"] : ""));
|
$cli->get($parse["path"] . (isset($parse["query"]) ? "?" . $parse["query"] : ""));
|
||||||
if ($return_body) {
|
if ($return_body) {
|
||||||
if ($cli->errCode != 0 || $cli->statusCode != 200) return false;
|
if ($cli->errCode != 0 || $cli->statusCode != 200) return false;
|
||||||
@@ -54,19 +53,18 @@ class ZMRequest
|
|||||||
* @param $data
|
* @param $data
|
||||||
* @param array $set
|
* @param array $set
|
||||||
* @param bool $return_body
|
* @param bool $return_body
|
||||||
* @param array $data 请求body
|
|
||||||
* @return bool|string|Client
|
* @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);
|
$parse = parse_url($url);
|
||||||
if (!isset($parse["host"])) {
|
if (!isset($parse["host"])) {
|
||||||
Console::warning("ZMRequest: url must contains scheme such as \"http(s)://\"");
|
Console::warning("ZMRequest: url must contains scheme such as \"http(s)://\"");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if(!isset($parse["path"])) $parse["path"] = "/";
|
||||||
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
|
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
|
||||||
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
|
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
|
||||||
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
|
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
|
||||||
$cli->setData($data);
|
|
||||||
$cli->setHeaders($header);
|
$cli->setHeaders($header);
|
||||||
$cli->post($parse["path"] . (isset($parse["query"]) ? ("?" . $parse["query"]) : ""), $data);
|
$cli->post($parse["path"] . (isset($parse["query"]) ? ("?" . $parse["query"]) : ""), $data);
|
||||||
if ($return_body) {
|
if ($return_body) {
|
||||||
@@ -98,4 +96,34 @@ class ZMRequest
|
|||||||
public static function session($option) {
|
public static function session($option) {
|
||||||
return Saber::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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user