Heim  >  Artikel  >  Backend-Entwicklung  >  PHP利用socket模拟post之fsockopen发送数据_PHP教程

PHP利用socket模拟post之fsockopen发送数据_PHP教程

WBOY
WBOYOriginal
2016-07-20 11:10:24854Durchsuche

今天有点心烦啊,,还是写一篇关于php fsockopen post相关数据的文章吧,我们可以模仿来自然后再发送给对方服务器,有需要的朋友看看吧。

 代码如下 复制代码

POST /目的程序 HTTP/1.1
Accept: */*
Referer: http://www.hzhuti.com
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(主机名称,端口号码,错误号的接受变量,错误提示的接受变量,超时时间)

主机名称就是你需要发送数据的目的地;
端口号就是这个目的程序会在哪个端口等着你的数据;
错误号的接受变量,这个是如果建立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");


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/444733.htmlTechArticle今天有点心烦啊,,还是写一篇关于php fsockopen post相关数据的文章吧,我们可以模仿来自然后再发送给对方服务器,有需要的朋友看看吧。...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn