Blogger Information
Blog 49
fans 1
comment 0
visits 44856
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
利用聚合数据提供的接口查询手机号归属地。2019年6月26日
Nick的博客
Original
1304 people have browsed it

实例

<?php
//抓取方式:接口抓取
//创建juheCurl所需的参数,用变量保存
//查询手机号归属地的接口测试
$url = 'http://apis.juhe.cn/mobile/get';    //接口地址
$params = array(
    'phone' => '13760842257',                         //需要查询的手机号码
    'key' => '4d6cce531387deeedf359687fb04c163',   //应用KEY(应用详细页查询)
);
$mobile = juheCurl($url, $params,1);       //调用接口函数方法
print_r($mobile);                                 //在页面中打印查询结果


//juhecurl聚合接口函数方法模板
// 通过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;
}

运行实例 »

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


最终在页面中打印出查询结果:

聚合数据接口查询手机号归属地.png

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!