diff --git a/curl.lib.php b/curl.lib.php new file mode 100644 index 0000000..07a13bc --- /dev/null +++ b/curl.lib.php @@ -0,0 +1,207 @@ + $val) { + if($is_urlcode){ + $encode_key = urlencode($key); + }else{ + $encode_key = $key; + } + if ($encode_key != $key) { + unset($query[$key]); + } + if($is_urlcode){ + $query[$encode_key] = urlencode($val); + }else{ + $query[$encode_key] = $val; + } + } + } + // 设置CURL选项 + curl_setopt(self::$_ch, CURLOPT_URL, $url); + curl_setopt(self::$_ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt(self::$_ch, CURLOPT_HEADER, 0); + curl_setopt(self::$_ch, CURLOPT_POST, true ); + curl_setopt(self::$_ch, CURLOPT_POSTFIELDS, $query); + // 禁用SSL证书验证 + curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt(self::$_ch, CURLOPT_SSL_VERIFYHOST, FALSE); + curl_setopt(self::$_ch, CURLOPT_SSLVERSION, 1); + // 执行请求并返回结果 + $ret = self::_execute(); + self::_close(); + return $ret; + } + + /** + * 发送PUT请求 + * @param $url string 请求URL + * @param $query array 查询参数 + * @return mixed 请求结果 + */ + private static function _put($url, $query = array()) { + curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'PUT'); + return self::_httpPost($url, $query); + } + + /** + * 发送DELETE请求 + * @param $url string 请求URL + * @param $query array 查询参数 + * @return mixed 请求结果 + */ + private static function _delete($url, $query = array()) { + curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); + return self::_httpPost($url, $query); + } + + /** + * 发送HEAD请求 + * @param $url string 请求URL + * @param $query array 查询参数 + * @return mixed 请求结果 + */ + private static function _head($url, $query = array()) { + curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); + return self::_httpPost($url, $query); + } + + /** + * 执行CURL请求 + * @return mixed 请求结果 + */ + private static function _execute() { + $response = curl_exec(self::$_ch); + $errno = curl_errno(self::$_ch); + // 如果发生错误,抛出异常 + if ($errno > 0) { + throw new \Exception(curl_error(self::$_ch), $errno); + } + return $response; + } +} \ No newline at end of file