自定义curl
自定义
function curl($url,$data,$headers){ $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'Errno'.curl_error($curl);//捕抓异常 } curl_close($curl); // 关闭CURL会话 echo($result); }
天行数据 curl( )
//页面顶部需设置为utf8编码 header('Content-Type: text/html; charset=utf-8'); <?php function posturl($url,$data){ $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl,CURLOPT_POST,1); curl_setopt($curl,CURLOPT_POSTFIELDS,$data); curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); $output = curl_exec($curl); curl_close($curl); return $output; } $parArr = array('key' => '你的APIKEY','city' => '上海'); $data = posturl('http://api.tianapi.com/worldtime/index',$parArr); $json = json_decode($data,true);//将json解析成数组 if($json['code'] == 200){ //判断状态码 print_r($json); //打印数组 }else{ echo '错误提示:'.$json['msg']; } ?>
聚合数据curl( )
//请求的接口URL $apiUrl = 'http://v.juhe.cn/joke/randJoke.php'; //请求参数 $params = [ 'key' => '聚合数据上申请的接口调用key', ]; //参数数组转换成字符串 $paramsString = http_build_query($params); //发起接口网络请求 $response = null; try { $response = juheHttpRequest($apiUrl, $paramsString, 1); } catch (Exception $e) { var_dump($e); //此处根据自己的需求进行具体的异常处理 } if (!$response) { echo '请求异常' . PHP_EOL; } //接收接口返回内容 $result = json_decode($response, true); if (!$result) { echo '请求异常' . PHP_EOL; } $errorCode = $result['error_code']; if ($errorCode == 0) { $data = $result['result']; } else { echo "请求异常:{$errorCode}_{$result['reason']}" . PHP_EOL; } //打印接口返回结果 var_dump($result); /** * 发起网络请求函数 * @param String $url 请求的URL * @param bool $params 请求的参数内容 * @param int $isPost 是否POST请求 * @return bool|string 返回内容 */ function juheHttpRequest($url, $params = false, $isPost = 0) { $httpInfo = []; $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_TIMEOUT, 12); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if ($isPost) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_URL, $url); } else { if ($params) { curl_setopt($ch, CURLOPT_URL, $url . '?' . $params); } else { curl_setopt($ch, CURLOPT_URL, $url); } } $reponse = curl_exec($ch); if ($reponse === FALSE) { // echo "cURL Error: ".curl_error($ch); return false; } $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpInfo = array_merge($httpInfo, curl_getinfo($ch)); curl_close($ch); return $reponse; }