/** * CURL访问 * @param $url * @param bool $params * @param int $ispost * @param array $header * @param bool $verify * @param bool $userCert * @return bool|string */ public function http($url, $params = false, $ispost = 0, $header = [], $verify = false, $userCert = false) { $httpInfo = array(); $ch = curl_init(); if(!empty($header)){ curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //忽略ssl证书 if($verify === true){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); } else { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if ($ispost) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_URL, $url); } else { if (is_array($params)) { $params = http_build_query($params); } if ($params) { curl_setopt($ch, CURLOPT_URL, $url . '?' . $params); } else { curl_setopt($ch, CURLOPT_URL, $url); } } if ($userCert) { curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM'); // curl_setopt($ch, CURLOPT_SSLCERT, ROOT_PATH.'application/api/controller/apiclient_cert.pem'); // curl_setopt($ch, CURLOPT_SSLKEY, ROOT_PATH.'application/api/controller/apiclient_key.pem'); //双向证书验证时需要 } $response = curl_exec($ch); if ($response === FALSE) { trace("cURL Error: " . curl_errno($ch) . ',' . curl_error($ch), 'error'); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpInfo = array_merge($httpInfo, curl_getinfo($ch)); trace($httpInfo, 'error'); return false; } curl_close($ch); return $response; }
比较完善的一个curl封装方法,适用大部分网络请求场景。
评论