diff --git a/HttpServer/Client/Curl.php b/HttpServer/Client/Curl.php index 550a4f5b..31339164 100644 --- a/HttpServer/Client/Curl.php +++ b/HttpServer/Client/Curl.php @@ -372,7 +372,7 @@ class Curl */ public function get($path, $params = []) { - return $this->request($path, self::GET, $params); + return $this->request($this->joinGetParams($path, $params), self::GET); } @@ -467,7 +467,7 @@ class Curl return $this->curl_multi[$hash]; } - $resource = $this->do(curl_init($host . $path), $host . $path, $method, $params); + $resource = $this->do(curl_init($host . $path), $host . $path, $method); if ($method === self::POST && !empty($params)) { curl_setopt($resource, CURLOPT_POSTFIELDS, HttpParse::parse($params)); } @@ -479,6 +479,26 @@ class Curl } + /** + * @param $path + * @param $params + * @return mixed|resource + * @throws Exception + */ + public function upload($path, $params) + { + [$host, $isHttps, $path] = $this->matchHost($path); + $resource = $this->do(curl_init($host . $path), $host . $path, self::POST); + + @curl_setopt($resource, CURLOPT_POSTFIELDS, $params); + + if ($isHttps !== false) { + return $this->execute($this->curlHandlerSslSet($resource)); + } + return $this->execute($resource); + } + + /** * @param $resource * @return bool @@ -506,11 +526,10 @@ class Curl * @param $resource * @param $path * @param $method - * @param array $params * @return resource * @throws Exception */ - private function do($resource, $path, $method, $params = []) + private function do($resource, $path, $method) { curl_setopt($resource, CURLOPT_TIMEOUT, $this->timeout); // 超时设置 curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->connection_timeout); // 超时设置 @@ -532,11 +551,6 @@ class Curl curl_setopt($resource, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向 curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate'); - $method = strtoupper($method); - if ($method === self::GET) { - $path = $this->joinGetParams($path, $params); - } - if ($method === self::POST) { curl_setopt($resource, CURLOPT_POST, 1); } diff --git a/function.php b/function.php index 9b312377..93f572e2 100644 --- a/function.php +++ b/function.php @@ -34,6 +34,113 @@ if (!function_exists('make')) { } + +if (!function_exists('isUrl')) { + + + /** + * @param $url + * @param bool $get_info + * @return false|array + */ + function isUrl($url, $get_info = true) + { + $queryMatch = '/((http[s]?):\/\/)?(([\w-_]+\.)+\w+(:\d+)?)(\/.*)?/'; + if (!preg_match($queryMatch, $url, $outPut)) { + return false; + } + + $port = str_replace(':', '', $outPut[5]); + + [$isHttps, $domain, $port, $path] = [$outPut[2] == 'https', $outPut[3], $port, $outPut[6] ?? '']; + if ($isHttps && empty($port)) { + $port = 443; + } + + unset($outPut); + + return [$isHttps == 'https', $domain, $port, $path]; + } + +} + + +if (!function_exists('split_request_uri')) { + + + /** + * @param $url + * @return false|array + */ + function split_request_uri($url) + { + if (($parse = isUrl($url, null)) === false) { + return false; + } + + [$isHttps, $domain, $port, $path] = $parse; + $uri = $isHttps ? 'https://' . $domain : 'http://' . $domain; + if (!empty($port)) { + $uri .= ':' . $port; + } + return [$uri, $path]; + } + +} + + +if (!function_exists('hadDomain')) { + + + /** + * @param $url + * @return false|array + */ + function hadDomain($url) + { + if (($parse = isUrl($url, null)) === false) { + return false; + } + [$isHttps, $domain, $port, $path] = $parse; + $uri = $isHttps ? 'https://' . $domain : 'http://' . $domain; + if (!empty($port)) { + $uri .= ':' . $port; + } + return $uri; + } + +} + + +if (!function_exists('isDomain')) { + + + /** + * @param $url + * @param $replace + * @return false|array + */ + function isDomain($url) + { + return !isIp($url); + } + +} +if (!function_exists('isIp')) { + + + /** + * @param $url + * @return false|array + */ + function isIp($url) + { + return preg_match('/(\d{1,3}\.){3}\.\d{1,3}(:\d{1,5})?/', $url); + } + +} + + if (!function_exists('loadByDir')) {