php模拟用户发送post数据方法

作者: 关于计算机  发布:2019-09-26

 代码如下

使用 PHP 向页面 POST 数据

POST /指标程序 HTTP/1.1
Accept: */*
Referer: http://www.111cn.net
Accept-Language: zh-cn,en-us;q=0.5
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; .NET CLR 1.0.3705; .NET CLR 1.1.4322)
Host: 要发送到的主机地址
Content-Length: 发送数据的长短
Pragma: no-cache
Cache-Control: no-cache
username=php&password=iask   //post发送的数额

 代码如下

fsockopen(主机名称,端口号码,错误号的承受变量,错误提醒的接受变量,超时时间)

<?php
function socketPost($url, $data, &$ret)
{
 $urlArr = parse_url($url);
 $host = $urlArr['host'];
 $port = isset($urlArr['port'])?$urlArr['port']:80;
 $path = isset($urlArr['path'])?$urlArr['path']:"/";
 $fp = fsockopen($host, $port, $errno, $errstr, 30);
 if (!$fp)
 {
     echo "$errstr ($errno)<br />n";
  return false;
 }
 else
 {
     $out = "POST $path HTTP/1.1rn";
     $out .= "Host: $hostrn";
  $out .= "Content-Type: application/x-www-form-urlencodedrn";
  $out .= "Content-Length: ".strlen($data)."rn";
     $out .= "Connection: Keep-Alivernrn";
  $out .= $data;
  $ret = "";
     fwrite($fp, $out);
     while (!feof($fp))
  {
         $ret .= fgets($fp, 128);
     }
     fclose($fp);
 }
 return true;
}
?>

长机名称就是你供给发送数据的目标地;
端口号就是以此目标程序会在哪些端口等着您的多寡;
错误号的收受变量,那个是一旦构建socket不成事的时候回来的一无所能编号;
荒唐提示的变量,是漏洞比较多的时候回来的一无所能提醒新闻;
逾期时间,正是post数据之后假如对方未有回答音信,等待的最长日子。

如果post报错,把$out .= "Connection: Keep-Alivernrn";中的Keep-Alive改成Close

 代码如下

选取php的socket模拟发送post数据的贰个实例

$port = $url['port'] ? $url['port'] : 80;
$fp = fsockopen($url['host'], $port, $errno, $errstr,10);
if (!$fp) return "在主机: $url[host] $port 张开socket退步,失败原因是: $errno - $errstr";
fputs($fp, sprintf("POST %s%s%s HTTP/1.0rn", $url['path'], $url['query'] ? "?" : "", $url['query']));
fputs($fp, "Host:".$url['host']."rn");
fputs($fp, "Content-type: application/x-www-form-urlencodedrn");
fputs($fp, "Content-length: " . strlen($encoded) . "rn");
fputs($fp, "Connection: closernrn");
fputs($fp, "$encodedn");

 

 代码如下

<?php  
$domain = "127.0.0.1";  
$port = 80;  
$uri = "/ly/post.php";  
$data="txtName=111&txtEmail=222@1.net&rabSex=%D0%A1%BD%E3&txtFrom=%BD%AD%CE%F7%C1%FA%C4%CF&txtQq=2222&txtUrl=33333333&txtFace=images%2Fface%2Fface05.gif&txtEm=images%2Fem%2Fem01.gif&txtBody=rrr";  
$protocolstr ="POST {$uri} HTTP/1.1rnHost: {$domain}rnContent-type: application/x-www-form-urlencodedrnContent-length: " . strlen($data) . "rnReferer: : Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)rnAccept: */*rnrn{$data}rnrn";  
  
  
$sock = fsockopen($domain, $port, $errno, $errstr, 30);  
if (!$sock) die("$errstr ($errno)n");  
fputs($sock, $protocolstr);  
  
$headers = "";  
while ($str = trim(fgets($sock, 4096)))  
  $headers .= "$strn";  
  
$body = "";  
while (!feof($sock))  
  $body .= fgets($sock, 4096);  
fclose($sock);  
  
echo "<h2>Response header:</h2>n";  
echo $headers;  
echo "n";  
  
echo "<h2>Response body:</h2>n";  
echo $body;  
?>  

在此地我们就不讲关于fsockopen fwrite这个函数的用法了,只汇报模仿进度,有亟待的可参看了下。

本文由金沙澳门官网送注册58发布于关于计算机,转载请注明出处:php模拟用户发送post数据方法

关键词: