首頁 > 後端開發 > php教程 > PHP之cURL實現手機號碼歸屬地查詢功能

PHP之cURL實現手機號碼歸屬地查詢功能

小云云
發布: 2023-03-20 14:06:01
原創
2355 人瀏覽過

本文主要和大家分享PHP之cURL实现手机号码归属地查询功能,希望能帮助到大家。

1、代码如下:

index.html

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <meta name="viewport" content="width=device-width,initial-scale=1.0">

    <title>手机号码归属地查询</title>

    <style type="text/css">

        *{margin: 0;padding: 0;border: 0;}

        body{font-family: 微软雅黑;width: 100%;background-color: #f8f8f8}

        form input{

            outline: none;

            font-family: 微软雅黑;

            font-size: 16px;

            transition: all .5s ease;

        }

        form input:focus {

            border-color: rgba(82, 168, 236, 0.8);

            outline: thin dotted \9;

            box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);

        }

        p{text-align: center;padding: 10 0 0 0;}

        .query_phone{

            width: 90%;

            height: 40px;

            padding-left: 5%;/*设置placeholder的位置*/

        }

        .submit{

            width: 90%;

            height: 50px;

            color: #fff;

            font-size: 16px;

            background-color: rgb(97,178,220);

            transition: all .5s ease;

            font-family: 微软雅黑;

        }

        .submit:hover{

            background-color: rgb(97,178,255);

        }

         

        /*input placeholder样式*/

        :-moz-placeholder { /* Mozilla Firefox 4 to 18 */

            opacity:.5;

        }

 

        ::-moz-placeholder { /* Mozilla Firefox 19+ */

            opacity:.5;

        }

 

        input:-ms-input-placeholder{

            opacity:.5;

        }

 

        input::-webkit-input-placeholder{

            opacity:.5;

        }

 

        input::-webkit-input-placeholder {  /* WebKit browsers*/

        }

        input:-moz-placeholder {            /* Mozilla Firefox 4 to 18*/

        }

        input::-moz-placeholder {           /* Mozilla Firefox 19+*/

        }

        input:-ms-input-placeholder {       /* Internet Explorer 10+*/

        }

    </style>

 

    <script type="text/javascript">

        function checkMobile(){

            var sMobile = document.mobileform.phone.value.trim();

            if(!(/^1[3|4|5|6|7|8|9][0-9]{5,9}$/.test(sMobile))){

                alert("不是完整的11位手机号或者正确的手机号前七位");

                document.mobileform.phone.focus();

                return false;

            }

        }

    </script>

</head>

<body>

<form class="phone_check_form" name="mobileform" method="get" action="query_phone.php" onsubmit="return checkMobile()";>

    <p><input class="query_phone" name="phone" type="text"/ required="" placeholder="13414503051" maxlength="11"></p>

    <p><input class="submit" name="submit" type="submit" value="查询"/></p>

    <p align="center" style="font-size: 12px;margin-top: 50px;">by Vegeta</p>

</form>

 

</body>

</html>

登入後複製

query_phone.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

<meta charset="utf-8">

<?php

    header("Content-type:text/html,charset=utf8");

    include(&#39;simple_html_dom.php&#39;);

 

    function query($phone=&#39;&#39;)

    {

        $url=&#39;http://www.ip138.com:8080/search.asp?action=mobile&mobile=&#39;.$phone;

        $ch = curl_init();

        $timeout = 5; 

        curl_setopt ($ch, CURLOPT_URL, $url); 

        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 

        curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); 

        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 

        curl_setopt ($ch, CURLOPT_REFERER, &#39;http://www.cltt.org/studentscore&#39;); 

        $contents = curl_exec($ch); 

        $contents = mb_convert_encoding($contents, &#39;utf-8&#39;, &#39;GBK,gb18030,gb2312&#39;);

        curl_close($ch);

        return $contents;

    }

 

    function simple($phone=&#39;&#39;)

    {

        $contents = query($phone);

        $html = new simple_html_dom();

        $html ->load($contents);

        $arrMsg = array();         

        foreach($html->find(&#39;table tbody tr td&#39;) as $m) {

            array_push($arrMsg,$m->plaintext);

        }

        return $arrMsg;

    }

 

    $ph=$_GET["phone"];

    var_dump(simple($ph));

?>

登入後複製

simple_html_dom.php

需要下载php解析html类库:simple_html_dom.php

2、效果截图



相关推荐:

php手机号码归属地查询api接口

手机号码归属地查询:PHP+MYSQL

PHP手机号码归属地查询代码(API接口/mysql)_PHP教程

以上是PHP之cURL實現手機號碼歸屬地查詢功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
TP5.1 多表關聯查詢中實現搜尋功能
來自於 1970-01-01 08:00:00
0
0
0
PHP—基於Yii2框架的問卷調查功能實現
來自於 1970-01-01 08:00:00
0
0
0
能不整合一個 REDIS 功能?
來自於 1970-01-01 08:00:00
0
0
0
修改功能有bug
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板