溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP socket怎么模擬POST請(qǐng)求

發(fā)布時(shí)間:2021-09-01 21:38:34 來源:億速云 閱讀:132 作者:chen 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“PHP socket怎么模擬POST請(qǐng)求”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

以前模擬post請(qǐng)求我都用PHP curl擴(kuò)展實(shí)現(xiàn)來著,沒想過PHP socket也可以實(shí)現(xiàn)。最近翻了下相關(guān)資料才發(fā)現(xiàn)原來沒有那么高深,只是以前一直沒有完全理解post的原理和本質(zhì)而已,其實(shí)就是發(fā)送給目的程序一個(gè)標(biāo)志為post的協(xié)議串如下:

POST /目的程序url HTTP/1.1

Accept: 接收信息格式

Referer: url來路

Accept-Language: 接收語言

Content-Type: application/x-www-form-urlencoded

Cookie: 網(wǎng)站cookie,不用俺過多解釋,對(duì)吧?

User-Agent: 用戶代理,操作系統(tǒng)及版本、CPU 類型、瀏覽器及版本等信息

Host: 要發(fā)送到的主機(jī)地址

Content-Length: 發(fā)送數(shù)據(jù)的長(zhǎng)度

Pragma: 本地是否存在緩存

Cache-Control: 是否需要網(wǎng)頁緩存

Connection: 連接狀態(tài)

username=fengdingbo&password=jb51.net   //post發(fā)送的數(shù)據(jù)

我想大家對(duì)表單的post方法提交數(shù)據(jù)應(yīng)該是最熟悉不過了,例如我們想把用戶名和密碼發(fā)送給某個(gè)頁面的時(shí)候,填寫好相應(yīng)的input框,點(diǎn)擊提交按鈕,最后把這個(gè)表單發(fā)送到action程序的就是以上數(shù)據(jù)。知道了這一點(diǎn)我想就不難了

這時(shí)候我們只需要用php的socket打開一個(gè)端口,例如80端口,把以上信息利用這個(gè)端口發(fā)送給目的程序就行了。

我們?nèi)绾卧谝粋€(gè)端口上建立一個(gè)socket通道呢?

在PHP中是如此簡(jiǎn)單呢!

官方給的原型:

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

下邊是人類的理解:

fsockopen(主機(jī)名稱,端口號(hào),錯(cuò)誤號(hào)的&變量,錯(cuò)誤提示的&變量,超時(shí)時(shí)間)
主機(jī)名稱就是你需要發(fā)送數(shù)據(jù)的目的地;
端口號(hào)就是這個(gè)目的程序會(huì)在哪個(gè)端口等著你的數(shù)據(jù);
錯(cuò)誤號(hào)的&變量,這個(gè)是如果建立socket不成功的時(shí)候返回的錯(cuò)誤編號(hào);
錯(cuò)誤提示的&變量,是錯(cuò)誤的時(shí)候返回的錯(cuò)誤提示信息;
超時(shí)時(shí)間,就是post數(shù)據(jù)之后如果對(duì)方?jīng)]有回應(yīng)信息,等待的最長(zhǎng)時(shí)間。

如果不出意外(你正確的設(shè)置fsockopen()函數(shù)的參數(shù))的話,一個(gè)socket通道現(xiàn)在已經(jīng)打開了,我們下一步需要做的就是,通過這個(gè)打開的通道把post請(qǐng)求協(xié)議發(fā)給目的程序,這時(shí)候可以使用fwrite或者fputs函數(shù)中的任意一個(gè),把post的請(qǐng)求格式發(fā)給fsockopen()打開的資源句柄,這時(shí)候一個(gè)偉大的socket模擬的post請(qǐng)求就誕生了。

 代碼如下

<?php
/**
 * SOCKET擴(kuò)展函數(shù)
 * @copyright (c) 2013
 * @author Qiufeng <fengdingbo@gmail.com>
 * @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 = <<<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_sauth2=value; pub_sauth3=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('https://www.jb51.net/', array('name='=>'qiufeng','password'=>md5('www.jb51.net'))));
/* e.g: socket_post('https://www.jb51.net', array('name='=>'qiufeng','password'=>md5('jb51.net'))); */
/* End of file socket_helper.php */

實(shí)際上,當(dāng)socket通道打開時(shí),我們傳的COOKIE是正確的話,(截圖運(yùn)行的php代碼來自上邊,運(yùn)行后返回的網(wǎng)頁出現(xiàn)了我的用戶名,說明對(duì)方網(wǎng)站已經(jīng)承認(rèn)我已經(jīng)登錄了)咱就可以干N多事情,比如刷帖,刷回復(fù)等,你們懂的,對(duì)吧?

好了上面還不夠有說服力我們來看一個(gè)php socket實(shí)現(xiàn)圖片上傳

這個(gè)代碼有兩點(diǎn)要注意:

一是他是http的post 請(qǐng)求;

二是表單上傳協(xié)議,

下的請(qǐng)求 串適合任何語言.

代碼如下 

<?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 socket怎么模擬POST請(qǐng)求”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI