Blogger Information
Blog 61
fans 1
comment 0
visits 69730
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0626-爬虫接口获取数据
我的博客
Original
1131 people have browsed it

实例 爬虫后端页面: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>

运行实例 »

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


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