最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

PHP socket模拟POST请求实例

时间:2014-06-09 16:40来源: 作者: 点击: 次
我们用到最多的模拟POST请求几乎都是使用php curl来实现了,没考虑到PHP socket也可以实现,今天看到朋友写了一文章,下面我来给大家分享一下PHP socket模拟POST请求实例. 以前模拟post请求俺都用PHP

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

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

  1. POST /目的程序url HTTP/1.1 
  2. Accept: 接收信息格式 
  3. Referer: url来路 
  4. Accept-Language: 接收语言 
  5. Content-Type: application/x-www-form-urlencoded 
  6. Cookie: 网站cookie,不用俺过多解释,对吧? 
  7. User-Agent: 用户代理,操作系统及版本、CPU 类型、浏览器及版本等信息 
  8. Host: 要发送到的主机地址 
  9. Content-Length: 发送数据的长度 
  10. Pragma: 本地是否存在缓存 
  11. Cache-Control: 是否需要网页缓存 
  12. Connection: 连接状态 
  13. username=fengdingbo&password=111cn.net   //post发送的数据 

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

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

我们如何在一个端口上建立一个socket通道呢?在PHP中是如此简单呢!官方给的原型:

  1. 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请求就诞生了,代码如下:

  1. <?php 
  2. /** 
  3.  * SOCKET扩展函数 
  4.  * @copyright (c) 2013 
  5.  * @author Qiufeng <fengdingbo@gmail.com> 
  6.  * @link http://www.vcphp.com 
  7.  * @version 1.0 
  8.  */ 
  9.  
  10. /** 
  11.  * Post Request 
  12.  * 
  13.  * @param string $url  
  14.  * @param array $data 
  15.  * @param string $referer 
  16.  * @return array 
  17.  */ 
  18. if ( ! function_exists('socket_post')) 
  19. { 
  20.  function socket_post($url, $data, $referer='') 
  21.  { 
  22.   if( ! is_array($data)) 
  23.   { 
  24.    return; 
  25.   } 
  26.  
  27.   $data = http_build_query($data); 
  28.   $url = parse_url($url); 
  29.  
  30.   if ( ! isset($url['scheme']) || $url['scheme'] != 'http') 
  31.   { 
  32.    die('Error: Only HTTP request are supported !'); 
  33.   } 
  34.  
  35.   $host = $url['host']; 
  36.   $path = isset($url['path']) ? $url['path'] : '/'; 
  37.  
  38.   // open a socket connection on port 80 - timeout: 30 sec 
  39.   $fp = fsockopen($host, 80, $errno, $errstr, 30); 
  40.  
  41.   if ($fp) 
  42.   { 
  43.    // send the request headers: 
  44.    $length = strlen($data); 
  45.    $POST = <<<HEADER 
  46. POST {$path} HTTP/1.1 
  47. Accept: text/plain, text/html 
  48. Referer: {$referer} 
  49. Accept-Language: zh-CN,zh;q=0.8 
  50. Content-Type: application/x-www-form-urlencodem  
  51. Cookie: token=value; pub_cookietime=2592000; pub_sauth1=value; pub_sauth2=value 
  52. User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17 
  53. Host: {$host} 
  54. Content-Length: {$length} 
  55. Pragma: no-cache 
  56. Cache-Control: no-cache 
  57. Connection: closern 
  58. {$data} 
  59. HEADER; 
  60.    fwrite($fp, $POST); 
  61.    $result = ''; 
  62.    while(!feof($fp)) 
  63.    { 
  64.     // receive the results of the request 
  65.     $result .= fread($fp, 512); 
  66.    } 
  67.   } 
  68.   else 
  69.   { 
  70.    return array( 
  71.      'status' => 'error', 
  72.      'error' => "$errstr ($errno)" 
  73.      ); 
  74.   } 
  75.  
  76.   // close the socket connection: 
  77.   fclose($fp); 
  78.  
  79.   // split the result header from the content 
  80.   $result = explode("rnrn", $result, 2); 
  81.  
  82.   // return as structured array: 
  83.   return array( 
  84.     'status' => 'ok', 
  85.     'header' => isset($result[0]) ? $result[0] : '', 
  86.     'content' => isset($result[1]) ? $result[1] : '' 
  87.     ); 
  88.  } 
  89. } 
  90.  
  91. print_r(socket_post('http://hzhuti.com/', array('name='=>'qiufeng','password'=>md5('www.111cn.net')))); 
  92. /* e.g: socket_post('http://www.111cn.net', array('name='=>'qiufeng','password'=>md5('111cn.net'))); */ 
  93. /* End of file socket_helper.php */ 

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

好了上面还不够有说服力我们来看一个php socket实现图片上传,这个代码有两点要注意.

一是他是http的post 请求;

二是表单上传协议,

下面的请求串适合任何语言,代码如下:

  1. <?php  
  2.      
  3.         $remote_server = "vcphp.com";  
  4.    
  5.         $boundary = "---------------------".substr(md5(rand(0,32000)),0,10);  
  6.           
  7.         // Build the header  
  8.         $header = "POST /api.php?action=twupload HTTP/1.0rn";  
  9.         $header .= "Host: {$remote_server}rn";  
  10.         $header .= "Content-type: multipart/form-data, boundary=$boundaryrn";  
  11.    
  12.         /*  
  13.         // attach post vars  
  14.         foreach($_POST AS $index => $value){  
  15.             $data .="--$boundaryrn";  
  16.             $data .= "Content-Disposition: form-data; name="".$index.""rn";  
  17.             $data .= "rn".$value."rn";  
  18.             $data .="--$boundaryrn";  
  19.         }  
  20.         */ 
  21.         $file_name = "aaa.jpg";  
  22.         $content_type = "image/jpg";  
  23.    
  24.         $data = '';  
  25.         // and attach the file  
  26.         $data .= "--$boundaryrn";  
  27.    
  28.         $content_file = file_get_contents('aaa.jpg');  
  29.         $data .="Content-Disposition: form-data; name="userfile"; filename="$file_name"rn";  
  30.         $data .= "Content-Type: $content_typernrn";  
  31.         $data .= "".$content_file."rn";  
  32.         $data .="--$boundary--rn";  
  33.    
  34.        $header .= "Content-length: " . strlen($data) . "rnrn";  
  35.          // Open the connection  
  36.         $fp = fsockopen($remote_server, 80);  
  37.         // then just  
  38.         fputs($fp, $header.$data);  
  39.         // reader  
  40.    
  41.     while (!feof($fp)) {  
  42.         echo fgets($fp, 128);  
  43.     }  
  44.    
  45. fclose($fp); 
(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容