使用curl实现http请求
/** * 使用curl实现http请求 * @param $url 请求地址 * @param $timeout 请求超时 * @param $method 请求方式 * @param $body POST请求报文体 * @param $header 请求头 * @param $res 返回结果 */ function reqCurl($url, $timeout = 5, $method="get", $body = array(), $header = array()){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); if ($header) { curl_setopt($ch,CURLOPT_HTTPHEADER,$header); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); if($method == "post"){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($body)); } $res = curl_exec($ch); curl_close($ch); return $res; }