curl发送数据函数

使用curl发送post或者get数据

$data是一个数组

小程序中,$data数组需进行处理urldecode(json_encode($data));

function http_request( $url, $data = '', $timeout = 5 ){
if( empty( $url ) ){
return ;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if( $data != '' && !empty( $data ) ){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));
}
curl_setopt($ch, CURLOPT_TIMEOUT, 15);//超时时间
$res = curl_exec($ch);
curl_close($ch);
return $res;
}