用PHP封裝一個強大且通用的cURL方法。 用PHP封裝一個強大且通用的cURL方法。 用PHP封裝一個強大且通用的cURL方法。 用PHP封裝一個強大且通用的cURL方法。 ```php /** * @function 強大且通用的cURL請求庫 * @param $url string 路徑 如 ...
用PHP封裝一個強大且通用的cURL方法。
用PHP封裝一個強大且通用的cURL方法。
用PHP封裝一個強大且通用的cURL方法。
用PHP封裝一個強大且通用的cURL方法。
/**
* @function 強大且通用的cURL請求庫
* @param $url string 路徑 如:https://example.com/a/b?key=val&k=>v
* @param $method string 請求方式 如: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 請求頭 如:['Content-Type' => 'json', 'Set-Cookie' => 'foo']
* @param $time_out int 超時秒數 如:10,(單位:秒)
* @return array [bool 請求是否成功, string 錯誤內容, [int http狀態碼, array 響應頭, string 響應主體內容]];
*/
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') {
//防止對12306類似的使用自家的ssl證書,造成的請求失敗
//禁止驗證對等證書
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//禁止驗證主機證書
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) {
//追加請求頭 配置curl內容
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);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$body = substr($response, $header_size);
$header_str = trim(substr($response, 0, $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];
}
}
}
if (curl_errno($curl)) {
return ['status' => false, 'msg' => curl_error($curl), 'data' => []];
}
curl_close($curl);
return ['status' => true, 'msg' => '', 'data' => ['http_code' => $http_code, 'body' => $body, 'header' => $header]];
}