/**
* @param $url
* @param string $method
* @param array $data
* @param array $request_headers
* @return array|string
*/
protected function shoplazzaHandle($url, $method = 'GET', $data = [], $request_headers = [])
{
if (in_array($method, array('GET', 'DELETE'))) {
if ($data) {
$url = $url . "?" . http_build_query($data);
}
}
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
if (in_array($method, array('POST', 'PUT'))) {
if (is_array($data)) {
$data = json_encode($data);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
$response = curl_exec($curl);
$response = str_replace("HTTP/1.1 100 Continue\r\r", '', $response);
$response = str_replace("HTTP/1.1 100 Continue\r\n\r\n", '', $response);
$response = str_replace("HTTP/1.1 100 Continue\n\n", '', $response);
$error_number = curl_errno($curl);
$error_message = curl_error($curl);
curl_close($curl);
if ($error_number) {
return $error_message;
} else {
$response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
$headers = array();
$header_data = explode("\n", $response[0]);
$headers['status'] = $header_data[0];
array_shift($header_data);
foreach ($header_data as $part) {
$h = explode(":", $part);
$headers[trim($h[0])] = trim($h[1]);
}
// return array('response' => $response, 'headers' => $headers);
return JD($response[1]);
}
}
curl 四通请求方法
最新推荐文章于 2023-03-23 11:51:12 发布