Blogger Information
Blog 40
fans 0
comment 0
visits 29311
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
聚合抓取数据--2019-06-27
小人物的博客
Original
782 people have browsed it

实例

<?php
$url = "http://apis.juhe.cn/mobile/get";
$data =array (
  "phone" => '13805009300',
  "key" => "4d6cce531387deeedf359687fb04c163",
);
print_r(juheCurl($url,$data,1));



// 通过curl请求接口
function juheCurl($url, $params = false, $ispost = 0){
    $httpInfo = array();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); // 默认值,让 cURL 自己判断使用哪个版本。 (强制使用 HTTP/1.1)。
    curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData'); // 在HTTP请求中包含一个"User-Agent: "头的字符串。
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); // 在尝试连接时等待的秒数。设置为0,则无限等待。
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);  // 设置超时限制防止死循环
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 要求结果保存到字符串中还是输出到屏幕上
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 爬取重定向页面
    if ($ispost) {
        curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的Post请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params); // Post提交的数据包
        curl_setopt($ch, CURLOPT_URL, $url); // 设置URL
    } else {
        // GET请求,组装url
        if ($params) {
            curl_setopt($ch, CURLOPT_URL, $url.'?'.$params);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }
    $response = curl_exec($ch); // 运行cURL,请求URL,把结果复制给变量
    if ($response === FALSE) {
        echo "cURL Error: " . curl_error($ch); //捕抓异常
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 获取一个cURL连接资源句柄的信息
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $response;
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post