PHP+Socket類比發送GET、POST請求

來源:互聯網
上載者:User

標籤:http   io   ar   os   使用   for   sp   on   cti   

/** * 使用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 = array();public function __construct($url) {$this->parseurl($url);$port = isset($this->urlinfo[‘port‘]) ? isset($this->urlinfo[‘port‘]) : ‘80‘;/* * 注意:這裡的fsockopen中的url參數形式為"www.xxx.com" * 不能夠帶"http://"這種 */$this->fp = fsockopen($this->urlinfo[‘host‘], $port, $errno, $errstr, 30);if ( !$this->fp ) {echo $errstr.‘(‘.$errno.‘)‘;exit();}$this->header[‘Host‘] = $this->urlinfo[‘host‘];}public function get($header = array(), $body = array()) {$this->header = array_merge($this->header, $header);$this->body = $body;$this->request(‘GET‘);}public function post($header = array(), $body = array()) {$this->header = array_merge($this->header, $header);$this->body = $body;$this->request(‘POST‘);}private function request($method) {$header = "";$this->requestLine = $method.‘ ‘.$this->urlinfo[‘path‘].‘ ‘.$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 ( !empty($this->body) ) {$body = "";foreach ( $this->body as $key => $value ) {$body .= $body == "" ? $key.‘=‘.$value : ‘&‘.$key.‘=‘.$value;}$this->requestInfo .= $body;}if ( fwrite($this->fp, $this->requestInfo) ) {while ( !feof($this->fp) ) {$str = fread($this->fp, 1024);echo $str;}}fclose($this->fp);}private function parseurl($url) {$this->urlinfo = parse_url($url);}}$url = "http://news.163.com/14/1102/01/AA0PFA7Q00014AED.html";$http = new Http($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‘,//對於post請求必須增加這兩項‘Content-Length‘ => 20,‘Content-Type‘ => ‘application/x-www-form-urlencoded‘), array(‘username‘=>‘koma‘, ‘age‘=>22));*/


看一看,你怎麼看?歡迎指正!




PHP+Socket類比發送GET、POST請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.