diff --git a/src/ZM/Utils/ZMRequest.php b/src/ZM/Utils/ZMRequest.php index 18b56d3d..a488ae1a 100644 --- a/src/ZM/Utils/ZMRequest.php +++ b/src/ZM/Utils/ZMRequest.php @@ -27,6 +27,7 @@ class ZMRequest 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); @@ -60,6 +61,7 @@ class ZMRequest 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); @@ -94,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; + } + } }