PHP socket 模拟POST 恳求实例代码_.docx

上传人:PIYPING 文档编号:11626270 上传时间:2021-08-26 格式:DOCX 页数:8 大小:14.66KB
返回 下载 相关 举报
PHP socket 模拟POST 恳求实例代码_.docx_第1页
第1页 / 共8页
PHP socket 模拟POST 恳求实例代码_.docx_第2页
第2页 / 共8页
PHP socket 模拟POST 恳求实例代码_.docx_第3页
第3页 / 共8页
PHP socket 模拟POST 恳求实例代码_.docx_第4页
第4页 / 共8页
PHP socket 模拟POST 恳求实例代码_.docx_第5页
第5页 / 共8页
亲,该文档总共8页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《PHP socket 模拟POST 恳求实例代码_.docx》由会员分享,可在线阅读,更多相关《PHP socket 模拟POST 恳求实例代码_.docx(8页珍藏版)》请在三一文库上搜索。

1、PHP socket 模拟POST 恳求实例代码_ 我们用到最多的模拟POST恳求几乎都是用法php curl来实现了,没考虑到PHP socket也可以实现,今日看到伴侣写了一文章,下面我来给大家分享一下PHP socket模拟POST恳求实例。 以前模拟post恳求俺都用PHP curl扩展实现来着,没想过PHP socket也可以实现。最近翻了下相关资料才发觉原来没有那么高深,只是以前始终没有完全理解post的原理和本质而已,其实就是发送给目的程序一个标记为post的协议串如下: POST /目的程序url HTTP/1.1 Accept: 接收信息格式 Referer: url来路 A

2、ccept-Language: 接收语言 Content-Type: application/x-form-urlencoded Cookie: 网站cookie,不用俺过多说明,对吧? User-Agent: 用户代理,操作系统及版本、CPU 类型、扫瞄器及版本等信息 Host: 要发送到的主机地址 Content-Length: 发送数据的长度 Pragma: 本地是否存在缓存 Cache-Control: 是否需要网页缓存 Connection: 连接状态 username=fengdingbopassword= /post发送的数据 我想大家对表单的post方法提交数据应当是最熟识不过

3、了,例如我们想把用户名和密码发送给某个页面的时候,填写好相应的input框,点击提交按钮,最终把这个表单发送到action程序的就是以上数据。知道了这一点我想就不难了 这时候我们只需要用php的socket打开一个端口,例如80端口,把以上信息利用这个端口发送给目的程序就行了。 我们如何在一个端口上建立一个socket通道呢? 在PHP中是如此简洁呢! 官方给的原型: resource fsockopen ( string $hostname , int $port = -1 , int $errno , string $errstr , float $timeout = ini_get(de

4、fault_socket_timeout) ) 下边是人类的理解: fsockopen(主机名称,端口号,错误号的变量,错误提示的变量,超时时间) 主机名称就是你需要发送数据的目的地; 端口号就是这个目的程序会在哪个端口等着你的数据; 错误号的变量,这个是假如建立socket不胜利的时候返回的错误编号; 错误提示的变量,是错误的时候返回的错误提示信息; 超时时间,就是post数据之后假如对方没有回应信息,等待的最长时间。 假如不出意外(你正确的设置fsockopen()函数的参数)的话,一个socket通道现在已经打开了,我们下一步需要做的就是,通过这个打开的通道把post恳求协议发给目的程序

5、,这时候可以用法fwrite或者fputs函数中的任意一个,把post的恳求格式发给fsockopen()打开的资源句柄,这时候一个宏大的socket模拟的post恳求就诞生了。 代码如下: ?php /* * SOCKET扩展函数 * copyright (c) 2021 * author Qiufeng * link l Referer: $referer Accept-Language: zh-CN,zh;q=0.8 Content-Type: application/x-form-urlencodem Cookie: token=value; pub_cookietime=25920

6、21; pub_sauth1=value; pub_sauth2=value User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17 Host: $host Content-Length: $length Pragma: no-cache Cache-Control: no-cache Connection: closern $data HEADER; fwrite($fp, $POST); $result = ; whi

7、le(!feof($fp) / receive the results of the request $result .= fread($fp, 512); else return array( status = error, error = $errstr ($errno) ); / close the socket connection: fclose($fp); / split the result header from the content $result = explode(rnrn, $result, 2); / return as structured array: retu

8、rn array( status = ok, header = isset($result0) ? $result0 : , content = isset($result1) ? $result1 : ); print_r(socket_post(http:/ array(name=qiufeng,password=md5(); /* e.g: socket_post(http:/, array(name=qiufeng,password=md5(); */ /* End of file socket_helper.php */ 事实上,当socket通道打开时,我们传的COOKIE是正确的

9、话,(截图运行的php代码来自上边,运行后返回的网页消失了我的用户名,说明对方网站已经承认我已经登录了)咱就可以干N多事情,比如刷帖,刷回复等,你们懂的,对吧? 好了上面还不够有劝说力我们来看一个php socket实现图片上传 这个代码有两点要留意: 一是他是http的post 恳求; 二是表单上传协议, 下的恳求 串适合任何语言. 代码如下 ?php $remote_server = ; $boundary = -.substr(md5(rand(0,32021),0,10); / Build the header $header = POST /api.php?action=twuplo

10、ad HTTP/1.0rn; $header .= Host: $remote_serverrn; $header .= Content-type: multipart/form-data, boundary=$boundaryrn; /* / attach post vars foreach($_POST AS $index = $value) $data .=-$boundaryrn; $data .= Content-Disposition: form-data; name=.$index.rn; $data .= rn.$value.rn; $data .=-$boundaryrn;

11、*/ $file_name = aaa.jpg; $content_type = image/jpg; $data = ; / and attach the file $data .= -$boundaryrn; $content_file = file_get_contents(aaa.jpg); $data .=Content-Disposition: form-data; name=userfile; filename=$file_namern; $data .= Content-Type: $content_typernrn; $data .= .$content_file.rn; $data .=-$boundary-rn; $header .= Content-length: . strlen($data) . rnrn; / Open the connection $fp = fsockopen($remote_server, 80); / then just fputs($fp, $header.$data); / reader while (!feof($fp) echo fgets($fp, 128); fclose($fp); 盼望本文对大家的 PHP程序设计有所关心。 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1