if (!function_exists('curl_data')) {
/**
* curl 请求
* Author:刘星麟
* @param $url
* @param $data
* @param string $method 支持 GET POST PUT DELETE
* @param string $type
* @return bool|string
*/
function curl_data($url, $data, $method = 'POST', $type = 'form-data')
{
//初始化
$ch = curl_init();
header('Content-Type:application/json; charset=utf-8');
$headers = [
'form-data' => ['Content-Type: multipart/form-data'], 'json' => ['Content-Type: application/json'],
];
//统一转化为大写
$method = strtoupper($method);
if ($method == 'GET') {
if ($data) {
$querystring = http_build_query($data);
$url = $url . '?' . $querystring;
}
}
// 请求头,可以传数组
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers[$type]);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($method == 'POST') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if ($method == 'PUT') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if ($method == 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //最大相应超时时间
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}