欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP socket 模拟POST 请求实例代码

程序员文章站 2024-02-24 18:24:28
我们用到最多的模拟post请求几乎都是使用php curl来实现了,没考虑到php socket也可以实现,今天看到朋友写了一文章,下面我来给大家分享一下php socke...

我们用到最多的模拟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-www-form-urlencoded

cookie: 网站cookie,不用俺过多解释,对吧?

user-agent: 用户代理,操作系统及版本、cpu 类型、浏览器及版本等信息

host: 要发送到的主机地址

content-length: 发送数据的长度

pragma: 本地是否存在缓存

cache-control: 是否需要网页缓存

connection: 连接状态

username=fengdingbo&password=jb51.net   //post发送的数据

我想大家对表单的post方法提交数据应该是最熟悉不过了,例如我们想把用户名和密码发送给某个页面的时候,填写好相应的input框,点击提交按钮,最后把这个表单发送到action程序的就是以上数据。知道了这一点我想就不难了

这时候我们只需要用php的socket打开一个端口,例如80端口,把以上信息利用这个端口发送给目的程序就行了。

我们如何在一个端口上建立一个socket通道呢?

在php中是如此简单呢!

官方给的原型:

resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )

下边是人类的理解:

fsockopen(主机名称,端口号,错误号的&变量,错误提示的&变量,超时时间)
主机名称就是你需要发送数据的目的地;
端口号就是这个目的程序会在哪个端口等着你的数据;
错误号的&变量,这个是如果建立socket不成功的时候返回的错误编号;
错误提示的&变量,是错误的时候返回的错误提示信息;
超时时间,就是post数据之后如果对方没有回应信息,等待的最长时间。

如果不出意外(你正确的设置fsockopen()函数的参数)的话,一个socket通道现在已经打开了,我们下一步需要做的就是,通过这个打开的通道把post请求协议发给目的程序,这时候可以使用fwrite或者fputs函数中的任意一个,把post的请求格式发给fsockopen()打开的资源句柄,这时候一个伟大的socket模拟的post请求就诞生了。

 代码如下

<?php
/**
 * socket扩展函数
 * @copyright (c) 2013
 * @author qiufeng <fengdingbo@gmail.com>
 * @link //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 = <<<header
post {$path} http/1.1
accept: text/plain, text/html
referer: {$referer}
accept-language: zh-cn,zh;q=0.8
content-type: application/x-www-form-urlencodem 
cookie: token=value; pub_cookietime=2592000; 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 = '';
 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('//www.jb51.net/', array('name='=>'qiufeng','password'=>md5('www.jb51.net'))));
/* e.g: socket_post('//www.jb51.net', array('name='=>'qiufeng','password'=>md5('jb51.net'))); */
/* end of file socket_helper.php */

实际上,当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"; 
 
  /* 
  // attach post vars 
  foreach($_post as $index => $value){ 
   $data .="--$boundaryrn"; 
   $data .= "content-disposition: form-data; name="".$index.""rn"; 
   $data .= "rn".$value."rn"; 
   $data .="--$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程序设计有所帮助。