用PHP封裝一個(gè)強(qiáng)大且通用的cURL方法。
用PHP封裝一個(gè)強(qiáng)大且通用的cURL方法。
用PHP封裝一個(gè)強(qiáng)大且通用的cURL方法。
用PHP封裝一個(gè)強(qiáng)大且通用的cURL方法。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-634901.html
/**
* @function 強(qiáng)大且通用的cURL請(qǐng)求庫(kù)
* @param $url string 路徑 如:https://example.com/a/b?key=val&k=>v
* @param $method string 請(qǐng)求方式 如:get、post、put、delete、patch、options
* @param $payload array|string 荷載 如:['foo' => 'bar', 'upload_file' => new CURLFile(file_path)]或json{"foo":"bar"}
* @param $request_header array 請(qǐng)求頭 如:['Content-Type' => 'json', 'Set-Cookie' => 'foo']
* @param $time_out int 超時(shí)秒數(shù) 如:10,(單位:秒)
* @return array [bool 請(qǐng)求是否成功, string 錯(cuò)誤內(nèi)容, [int http狀態(tài)碼, array 響應(yīng)頭, string 響應(yīng)主體內(nèi)容]];
*/
function curl($url, $method = 'GET', $payload = [], $request_header = [], $time_out = 10) {
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL, $url);
$method = strtoupper($method);
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
} else if ($method == 'PUT') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
} else if ($method == 'DELETE') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
} else if ($method == 'PATCH') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
} else if ($method == 'OPTIONS') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'OPTIONS');
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
} else if($method == 'HEAD') {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
} else {
curl_setopt($curl, CURLOPT_HTTPGET, true);
}
if ((parse_url($url)['scheme'] ?? '') == 'https') {
//防止對(duì)12306類(lèi)似的使用自家的ssl證書(shū),造成的請(qǐng)求失敗
//禁止驗(yàn)證對(duì)等證書(shū)
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//禁止驗(yàn)證主機(jī)證書(shū)
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
if ($time_out) {
curl_setopt($curl, CURLOPT_TIMEOUT, $time_out);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $time_out);
}
if ($request_header) {
//追加請(qǐng)求頭 配置curl內(nèi)容
curl_setopt($curl, CURLOPT_HTTPHEADER, array_map(function ($key, $value) {return $key . ': ' . $value;}, array_keys($request_header), $request_header));
}
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
$response = curl_exec($curl);
$curl_info = curl_getinfo($curl);
$body = substr($response, $curl_info['header_size']);
$header_str = trim(substr($response, 0, $curl_info['header_size']));
$header = [];
if($header_str) {
$header_arr = explode("\r\n", $header_str);
foreach($header_arr as $every_header) {
$header_temp = explode(': ', $every_header, 2);
if(count($header_temp) == 2) {
$header[$header_temp[0]] = $header_temp[1];
}
}
}
curl_close($curl);
if (curl_errno($curl)) {
return ['status' => false, 'msg' => curl_error($curl), 'data' => ['http_code' => $curl_info['http_code'], 'body' => '', 'header' => [], 'info' => $curl_info]];
}
return ['status' => true, 'msg' => '', 'data' => ['http_code' => $curl_info['http_code'], 'body' => $body, 'header' => $header, 'info' => $curl_info]];
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-634901.html
到了這里,關(guān)于用PHP封裝一個(gè)強(qiáng)大且通用的cURL方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!