PHP socket 模拟POST 请求实例代码

时间:2021-05-26

我们用到最多的模拟POST请求几乎都是使用php curl来实现了,没考虑到PHP socket也可以实现,今天看到朋友写了一文章,下面我来给大家分享一下PHP socket模拟POST请求实例。

以前模拟post请求俺都用PHP curl扩展实现来着,没想过PHP socket也可以实现。最近翻了下相关资料才发现原来没有那么高深,只是以前一直没有完全理解post的原理和本质而已,其实就是发送给目的程序一个标志为post的协议串如下:

POST /目的程序url HTTP/1.1

Accept: 接收信息格式

Referer: url来路

Accept-Language: 接收语言

Content-Type: application/x-> * @link https://www.jb51.net * @version 1.0 */ /** * Post Request * * @param string $url * @param array $data * @param string $referer * @return array */if ( ! function_exists('socket_post')){ function socket_post($url, $data, $referer='') { if( ! is_array($data)) { return; } $data = http_build_query($data); $url = parse_url($url); if ( ! isset($url['scheme']) || $url['scheme'] != 'http') { die('Error: Only HTTP request are supported !'); } $host = $url['host']; $path = isset($url['path']) ? $url['path'] : '/'; // open a socket connection on port 80 - timeout: 30 sec $fp = fsockopen($host, 80, $errno, $errstr, 30); if ($fp) { // send the request headers: $length = strlen($data); $POST = <<<HEADERPOST {$path} HTTP/1.1Accept: text/plain, text/htmlReferer: {$referer}Accept-Language: zh-CN,zh;q=0.8Content-Type: application/x-www-form-urlencodem Cookie: token=value; pub_cookietime=2592000; pub_sauth1=value; pub_sauth2=valueUser-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17Host: {$host}Content-Length: {$length}Pragma: no-cacheCache-Control: no-cacheConnection: closern{$data}HEADER; fwrite($fp, $POST); $result = ''; while(!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: return array( 'status' => 'ok', 'header' => isset($result[0]) ? $result[0] : '', 'content' => isset($result[1]) ? $result[1] : '' ); }} print_r(socket_post('https://www.jb51.net/', array('name='=>'qiufeng','password'=>md5('www.jb51.net'))));

实际上,当socket通道打开时,我们传的COOKIE是正确的话,(截图运行的php代码来自上边,运行后返回的网页出现了我的用户名,说明对方网站已经承认我已经登录了)咱就可以干N多事情,比如刷帖,刷回复等,你们懂的,对吧?

好了上面还不够有说服力我们来看一个php socket实现图片上传

这个代码有两点要注意:

一是他是http的post 请求;

二是表单上传协议,

下的请求 串适合任何语言.

代码如下

<?php $remote_server = "jb51.net"; $boundary = "---------------------".substr(md5(rand(0,32000)),0,10); // Build the header $header = "POST /api.php?action=twupload HTTP/1.0rn"; $header .= "Host: {$remote_server}rn"; $header .= "Content-type: multipart/form-data, boundary=$boundaryrn"; $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_name"rn"; $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程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章