作為php程式員一定會接觸http協議,也只有深入瞭解http協議,編程水平才會更進一步。最近我一直在學習php的關於http的編程,許多東西恍然大悟,受益匪淺。希望分享給大家。本文需要有一定http基礎的開發人員閱讀。希望對大家有所協助。
今天給大家帶來的是如何利用socket發送GET,POST請求。
在日常編程中相信很多人和我一樣大部分時間是利用瀏覽器向伺服器提出GET,POST請求,那麼可否利用其它方式提出GET,POST請求呢?答案必然是肯定的。瞭解過HTTP協議的人知道,瀏覽器提交請求的實質是向伺服器發送一個請求資訊,這個請求資訊有請求行,要求標頭,請求體(非必須)構成。伺服器根據請求資訊返回一個響應資訊。串連斷開。
HTTP請求的格式如下所示:
<request-line><headers><blank line>[<request-body>]
HTTP響應的格式與請求的格式十分相似:
<status-line><headers><blank line>[<response-body>]
我們可以利用HTTP發送請求的原理,可以重新考慮利用socket發送HTTP請求。
Socket的英文原義是“孔”或“插座”。通常也稱作“通訊端”,用於描述IP地址和連接埠,是一個通訊鏈的控制代碼,可以用來實現不同虛擬機器或不同電腦之間的通訊。在Internet上的主機一般運行了多個服務軟體,同時提供幾種服務。每種服務都開啟一個Socket,並綁定到一個連接埠上,不同的連接埠對應於不同的服務。如此看來,其實利用socket操作遠程檔案和讀寫本地的檔案一樣容易,把本地檔案看成通過硬體傳輸,遠程檔案通過網線傳輸就行了。
因而可以將發送請求的考慮成 建立串連->開啟socket介面(fsockopen())->寫入請求(fwrite())->讀出響應(fread()->關閉檔案(fclose())。話不多說,直接上代碼:
<?php interface Proto { // 串連url function conn($url); //發送get查詢 function get(); // 發送post查詢 function post(); // 關閉串連 function close();}class Http implements Proto { const CRLF = "\r\n"; protected $errno = -1; protected $errstr = ''; protected $response = ''; protected $url = null; protected $version = 'HTTP/1.1'; protected $fh = null; protected $line = array(); protected $header = array(); protected $body = array(); public function __construct($url) { $this->conn($url); $this->setHeader('Host: ' . $this->url['host']); } // 此方法負責寫請求行 protected function setLine($method) { $this->line[0] = $method . ' ' . $this->url['path'] . '?' .$this->url['query'] . ' '. $this->version; } // 此方法負責寫頭資訊 public function setHeader($headerline) { $this->header[] = $headerline; } // 此方法負責寫主體資訊 protected function setBody($body) { $this->body[] = http_build_query($body); } // 串連url public function conn($url) { $this->url = parse_url($url); // 判斷連接埠 if(!isset($this->url['port'])) { $this->url['port'] = 80; } // 判斷query if(!isset($this->url['query'])) { $this->url['query'] = ''; } $this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3); } //構造get請求的資料 public function get() { $this->setLine('GET'); $this->request(); return $this->response; } // 構造post查詢的資料 public function post($body = array()) { $this->setLine('POST'); // 設計content-type $this->setHeader('Content-type: application/x-www-form-urlencoded'); // 設計主體資訊,比GET不一樣的地方 $this->setBody($body); // 計算content-length $this->setHeader('Content-length: ' . strlen($this->body[0])); $this->request(); return $this->response; } // 真正請求 public function request() { // 把請求行,頭資訊,實體資訊 放在一個數組裡,便於拼接 $req = array_merge($this->line,$this->header,array(''),$this->body,array('')); //print_r($req); $req = implode(self::CRLF,$req); //echo $req; exit; fwrite($this->fh,$req); while(!feof($this->fh)) { $this->response .= fread($this->fh,1024); } $this->close(); // 關閉串連 } // 關閉串連 public function close() { fclose($this->fh); }}
利用此類發送一個簡單的GET請求:
<?php//記得引用Http類$url="http://home.***.net/u/DeanChopper/";$http=new Http($url);$response=$http->get();print_r($response);
傳回值為資訊,可以對響應資訊進行進一步處理,得到自己想得到的內容。
我們來看下一個具體的執行個體
<?php/** * 使用PHP Socket 編程類比Http post和get請求 * @author koma */class Http{ private $sp = "\r\n"; //這裡必須要寫成雙引號 private $protocol = 'HTTP/1.1'; private $requestLine = ""; private $requestHeader = ""; private $requestBody = ""; private $requestInfo = ""; private $fp = null; private $urlinfo = null; private $header = array(); private $body = ""; private $responseInfo = ""; private static $http = null; //Http對象單例 private function __construct() {} public static function create() { if ( self::$http === null ) { self::$http = new Http(); } return self::$http; } public function init($url) { $this->parseurl($url); $this->header['Host'] = $this->urlinfo['host']; return $this; } public function get($header = array()) { $this->header = array_merge($this->header, $header); return $this->request('GET'); } public function post($header = array(), $body = array()) { $this->header = array_merge($this->header, $header); if ( !empty($body) ) { $this->body = http_build_query($body); $this->header['Content-Type'] = 'application/x-www-form-urlencoded'; $this->header['Content-Length'] = strlen($this->body); } return $this->request('POST'); } private function request($method) { $header = ""; $this->requestLine = $method.' '.$this->urlinfo['path'].'?'.$this->urlinfo['query'].' '.$this->protocol; foreach ( $this->header as $key => $value ) { $header .= $header == "" ? $key.':'.$value : $this->sp.$key.':'.$value; } $this->requestHeader = $header.$this->sp.$this->sp; $this->requestInfo = $this->requestLine.$this->sp.$this->requestHeader; if ( $this->body != "" ) { $this->requestInfo .= $this->body; } /* * 注意:這裡的fsockopen中的url參數形式為"www.xxx.com" * 不能夠帶"http://"這種 */ $port = isset($this->urlinfo['port']) ? isset($this->urlinfo['port']) : '80'; $this->fp = fsockopen($this->urlinfo['host'], $port, $errno, $errstr); if ( !$this->fp ) { echo $errstr.'('.$errno.')'; return false; } if ( fwrite($this->fp, $this->requestInfo) ) { $str = ""; while ( !feof($this->fp) ) { $str .= fread($this->fp, 1024); } $this->responseInfo = $str; } fclose($this->fp); return $this->responseInfo; } private function parseurl($url) { $this->urlinfo = parse_url($url); }}// $url = "http://news.***.com/14/1102/01/AA0PFA7Q00014AED.html";$url = "http://localhost/httppro/post.php";$http = Http::create()->init($url);/* 發送get請求 echo $http->get(array( 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',));*/ /* 發送post請求 */echo $http->post(array( 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',), array('username'=>'發一個中文', 'age'=>22));
相關推薦:
詳解PHP修改檔案許可權函數chmod
詳解PHP如何?Hook機制
詳解使用PHP求兩個字串最長公用子串