$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 string $url 请求URL * @param array $query 查询参数 * @return mixed 请求结果 */ private static function _put($url, $query = array()) { curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'PUT'); return self::_httpPost($url, $query); } /** * 发送DELETE请求 * @param string $url 请求URL * @param array $query 查询参数 * @return mixed 请求结果 */ private static function _delete($url, $query = array()) { curl_setopt(self::$_ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); return self::_httpPost($url, $query); } /** * 发送HEAD请求 * @param string $url 请求URL * @param array $query 查询参数 * @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; } } 类声明:class Curl 定义了一个名为 Curl 的类,用于简化HTTP请求操作。 私有静态变量:private static $_ch; 等变量用于存储CURL会话句柄、头部、主体等信息。 存储数组:private static $_cookie = array(); 等数组用于存储Cookie、选项、URL和Referer等信息。 callWebServer方法:用于调用外部URL,支持GET和POST请求。根据请求方法调用相应的私有方法,并根据参数决定是否解析为JSON。 _init方法:初始化CURL会话,设置CURL选项。 setOption方法:设置CURL选项,传入选项数组。 _close方法:关闭CURL会话。 _httpGet方法:发送GET请求,构建查询字符串并设置CURL选项。 _httpPost方法:发送POST请求,对参数进行URL编码并设置CURL选项。 _put、_delete、_head方法:分别用于发送PUT、DELETE和HEAD请求,设置相应的CURL选项。 _execute方法:执行CURL请求,如果发生错误则抛出异常。