PHP cURL学习-手机号码归属地查询

梦想不抛弃苦心追求的人,只要不停止追求,你们会沐浴在梦想的光辉之中。再美好的梦想与目标,再完美的计划和方案,如果不能尽快在行动中落实,最终只能是纸上谈兵,空想一番。只要瞄准了大方向,坚持不懈地做下去,才能够扫除挡在梦想前面的障碍,实现美好的人生蓝图。PHP cURL学习-手机号码归属地查询,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

大二学了一下PHP cURL,做了几个demo。

1、代码如下:

index.html

<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" οnsubmit="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="查询"/></div>
	<p align="center" style="font-size: 12px;margin-top: 50px;">by Vegeta</p>
</form>

</body>
</html>

query_phone.php

<meta charset="utf-8">
<?php
    header("Content-type:text/html,charset=utf8"); 
    include('simple_html_dom.php');

    function query($phone='')
    {
        $url='http://www.ip138.com:8080/search.asp?action=mobile&mobile='.$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, 'http://www.cltt.org/studentscore');  
        $contents = curl_exec($ch);  
        $contents = mb_convert_encoding($contents, 'utf-8', 'GBK,gb18030,gb2312');
        curl_close($ch);
        return $contents;
    }

    function simple($phone='')
    {
        $contents = query($phone);
        $html = new simple_html_dom();
        $html ->load($contents);
        $arrMsg = array();          
        foreach($html->find('table tbody tr td') 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 cURL学习-手机号码归属地查询


PHP cURL学习-手机号码归属地查询


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/151238.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!