php提交数据和返回,php模拟提交并取得返回数据

先查看了下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 ("''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."
"; preg_match("/您查询的手机号码段(.*)卡号归属地/",$content,$aaa); if(!empty($aaa[1]))echo "手机号码段=[".$aaa[1]."]
"; preg_match("/卡号归属地(.*)卡类型/",$content,$bbb); if(!empty($bbb[1]))echo "卡号归属地=[".$bbb[1]."]
"; preg_match("/卡类型(.*)区号/",$content,$ccc); if(!empty($ccc[1]))echo "卡类型=[".$ccc[1]."]
"; fclose($fp); ?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值