登录  /  注册
博主信息
博文 61
粉丝 1
评论 0
访问量 68110
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
0626-爬虫接口获取数据
我的博客
原创
1105人浏览过

实例 爬虫后端页面:zyh.php

<?php



$phone=(isset($_GET['phone']) ? $_GET['phone'] : '');


$url = "http://apis.juhe.cn/mobile/get";
$params = array(
    "phone" => $phone,//需要查询的手机号码
    "key" => "107cf0ace7d53e1fdc0e6158a755a58e",//应用APPKEY(应用详细页查询)
);
echo juheCurl($url,$params,1);   //post 传1, GET 传0




$paramstring = http_build_query($params);
$content = juheCurl($url, $paramstring);
$result = json_decode($content, true);

//聚合天气预报接口
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 {
        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;
}
?>

运行实例 »

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

实例 爬虫接口获取数据:前端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>电话号码归属地查询</title>
</head>
<body>
<label for="">手机号</label><input type="text">
<label for=""></label>
<button type="button">查询</button>
<div>
   <ul>



   </ul>


</body>
</html>
<script>
    var input = document.getElementsByTagName('input')[0];
    var btn = document.getElementsByTagName('button')[0];
    var ul = document.getElementsByTagName('ul')[0];
    btn.onclick = function () {

        var res = new XMLHttpRequest();
        res.onreadystatechange = function () {
            if (res.readyState === 4) {
               console.log(res.responseText) ;

               // console.log(JSON.parse(res.responseText));
                var phone = JSON.parse(res.responseText);
                phone1 = phone.result;
               // console.log(phone);


                // var th = document.createElement('th');
                // th.innerText='省份:'+ phone.province ;
                // ul.append(th);
                // th.innerText ='城市:'+ phone.city ;
                //  ul.append(th);
                // th.innerText ='运营商:'+ phone.company;
                // ul.append(th);

                    if(phone.resultcode==='200'){

                        for(var i in phone1){

                            console.log(phone1[i]);
                            var li = document.createElement('li');
                            li.innerHTML = i+ ' : ' + phone1[i];
                            ul.appendChild(li);
                        }

                    }else{
                        alert('请检查手机号码是否正确!');
                }




            }

            };
        res.open('GET', 'http://qq.io/0624/zyh.php?phone=' + input.value, true);
        res.send(null);
    }

</script>

运行实例 »

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


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学