/** * @param string $url 链接 * @param null $data 传入数据 * @param string $type 类型 * @param string $method 请求方法 * @param string[] $header header头 * @param bool $https 是否验证证书 * @param int $timeout 超时时间 * @return bool|string */ function curl_request($url = ‘http://www.4399.com/‘, $data=null, $type = ‘json‘, $method=‘get‘, $header = array("content-type: application/json;charset=‘utf-8‘"), $https=true, $timeout = 5){ $ch = curl_init();//初始化 curl_setopt($ch, CURLOPT_URL, $url);//访问的URL curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//只获取页面内容,但不输出 if($https){ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https请求 不验证证书 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//https请求 不验证HOST } //默认是GET请求 switch (strtoupper($method)){ case ‘GET‘: curl_setopt($ch, CURLOPT_POST, 0);//请求方式为get请求 break; case ‘POST‘: curl_setopt($ch, CURLOPT_POST, true);//请求方式为post请求 break; case ‘PUT‘: case "DELETE": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式 break; } if ($data){ switch ($type){ case ‘json‘: curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));//请求数据 break; default: curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//请求数据 } } curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //curl_setopt($ch, CURLOPT_HEADER, false);//设置不需要头信息 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //模拟的header头 $result = curl_exec($ch);//执行请求 curl_close($ch);//关闭curl,释放资源 return $result; }