先查看了下ip138.com的手机号码段查询的表单,发现它提交两个字段值:
action , mobile
action的值为mobile , mobile的值就是欲查询手机号。
新建一个php页面post.php ,代码如下:
<?
//******************************如何用PHP模拟POST方法****************************************
$flag = 0;
//要post的数据
$argv = array(
'action'=>'mobile',
'mobile'=>'13143215566');
//构造要post的字符串
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$params .= "&";
$flag = 1;
}
$params.= $key."=";
$params.= urlencode($value);
$flag = 1;
}
$length = strlen($params);
//创建socket连接
$fp = fsockopen("www.ip138.com",8080,$errno,$errstr,10) or exit($errstr."--->".$errno);
//构造post请求的头
$header = "POST /search.asp HTTP/1.1/r/n";
$header .= "Host:www.ip138.com/r/n";
$header .= "Referer:post.php/r/n";
$header .= "Content-Type: application/x-www-form-urlencoded/r/n";
$header .= "Content-Length: ".$length."/r/n";
$header .= "Connection: Close/r/n/r/n";
//添加post的字符串
$header .= $params."/r/n";
//发送post的数据
fputs($fp,$header);
$inheader = 1;
$content='';
while (!feof($fp)) {
$content.= fgetss($fp,1024); //去除请求包的头只显示页面的返回数据
}
$search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript
"' '",
"'<[///!]*?[^<>]*?>'si", // 去掉 HTML 标记
"'([/r/n])[/s]+'", // 去掉空白字符
"'&(quot|#34);'i", // 替换 HTML 实体
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(/d+);'e"); // 作为 PHP 代码运行
$replace = array ("",
"",
"",
"",
"/"",
"&",
"<",
">",
"",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(//1)");
$content = preg_replace ($search, $replace, $content);
//echo $content."<br>";
preg_match("/您查询的手机号码段(.*)卡号归属地/",$content,$aaa);
if(!empty($aaa[1]))echo "手机号码段=[".$aaa[1]."]<br>";
preg_match("/卡号归属地(.*)卡类型/",$content,$bbb);
if(!empty($bbb[1]))echo "卡号归属地=[".$bbb[1]."]<br>";
preg_match("/卡类型(.*)区号/",$content,$ccc);
if(!empty($ccc[1]))echo "卡类型=[".$ccc[1]."]<br>";
fclose($fp);
?>
action , mobile
action的值为mobile , mobile的值就是欲查询手机号。
新建一个php页面post.php ,代码如下:
<?
//******************************如何用PHP模拟POST方法****************************************
$flag = 0;
//要post的数据
$argv = array(
'action'=>'mobile',
'mobile'=>'13143215566');
//构造要post的字符串
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$params .= "&";
$flag = 1;
}
$params.= $key."=";
$params.= urlencode($value);
$flag = 1;
}
$length = strlen($params);
//创建socket连接
$fp = fsockopen("www.ip138.com",8080,$errno,$errstr,10) or exit($errstr."--->".$errno);
//构造post请求的头
$header = "POST /search.asp HTTP/1.1/r/n";
$header .= "Host:www.ip138.com/r/n";
$header .= "Referer:post.php/r/n";
$header .= "Content-Type: application/x-www-form-urlencoded/r/n";
$header .= "Content-Length: ".$length."/r/n";
$header .= "Connection: Close/r/n/r/n";
//添加post的字符串
$header .= $params."/r/n";
//发送post的数据
fputs($fp,$header);
$inheader = 1;
$content='';
while (!feof($fp)) {
$content.= fgetss($fp,1024); //去除请求包的头只显示页面的返回数据
}
$search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript
"' '",
"'<[///!]*?[^<>]*?>'si", // 去掉 HTML 标记
"'([/r/n])[/s]+'", // 去掉空白字符
"'&(quot|#34);'i", // 替换 HTML 实体
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(/d+);'e"); // 作为 PHP 代码运行
$replace = array ("",
"",
"",
"",
"/"",
"&",
"<",
">",
"",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(//1)");
$content = preg_replace ($search, $replace, $content);
//echo $content."<br>";
preg_match("/您查询的手机号码段(.*)卡号归属地/",$content,$aaa);
if(!empty($aaa[1]))echo "手机号码段=[".$aaa[1]."]<br>";
preg_match("/卡号归属地(.*)卡类型/",$content,$bbb);
if(!empty($bbb[1]))echo "卡号归属地=[".$bbb[1]."]<br>";
preg_match("/卡类型(.*)区号/",$content,$ccc);
if(!empty($ccc[1]))echo "卡类型=[".$ccc[1]."]<br>";
fclose($fp);
?>