麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > PHP > 正文

php 利用socket發送HTTP請求(GET,POST)

2024-05-04 23:38:53
字體:
來源:轉載
供稿:網友

作為php程序員一定會接觸http協議,也只有深入了解http協議,編程水平才會更進一步。最近我一直在學習php的關于http的編程,許多東西恍然大悟,受益匪淺。希望分享給大家。本文需要有一定http基礎的開發者閱讀。

今天給大家帶來的是如何利用socket發送GET,POST請求。我借用燕十八老師封裝好的一個Http類給進行說明。

在日常編程中相信很多人和我一樣大部分時間是利用瀏覽器向服務器提出GET,POST請求,那么可否利用其它方式提出GET,POST請求呢?答案必然是肯定的。了解過HTTP協議的人知道,瀏覽器提交請求的實質是向服務器發送一個請求信息,這個請求信息有請求行,請求頭,請求體(非必須)構成。服務器根據請求信息返回一個響應信息。連接斷開。

HTTP請求的格式如下所示:

 

 
  1. <request-line> 
  2. <headers> 
  3. <blank line> 
  4. [<request-body>] 

HTTP響應的格式與請求的格式十分相似:

 

 
  1. <status-line> 
  2. <headers> 
  3. <blank line> 
  4. [<response-body>] 

我們可以利用HTTP發送請求的原理,可以重新考慮利用socket發送HTTP請求。

Socket的英文原義是“孔”或“插座”。通常也稱作“套接字”,用于描述IP地址和端口,是一個通信鏈的句柄,可以用來實現不同虛擬機或不同計算機之間的通信。在Internet上的主機一般運行了多個服務軟件,同時提供幾種服務。每種服務都打開一個Socket,并綁定到一個端口上,不同的端口對應于不同的服務。如此看來,其實利用socket操作遠程文件和讀寫本地的文件一樣容易,把本地文件看成通過硬件傳輸,遠程文件通過網線傳輸就行了。

因而可以將發送請求的考慮成 建立連接->打開socket接口(fsockopen())->寫入請求(fwrite())->讀出響應(fread()->關閉文件(fclose())。話不多說,直接上代碼:

 

 
  1. <?php  
  2. interface Proto { 
  3. // 連接url 
  4. function conn($url); 
  5. //發送get查詢 
  6. function get(); 
  7. // 發送post查詢 
  8. function post(); 
  9. // 關閉連接 
  10. function close(); 
  11. class Http implements Proto { 
  12. const CRLF = "/r/n"
  13. protected $errno = -1; 
  14. protected $errstr = ''
  15. protected $response = ''
  16. protected $url = null
  17. protected $version = 'HTTP/1.1'
  18. protected $fh = null
  19. protected $line = array(); 
  20. protected $header = array(); 
  21. protected $body = array(); 
  22. public function __construct($url) { 
  23. $this->conn($url); 
  24. $this->setHeader('Host: ' . $this->url['host']); 
  25. // 此方法負責寫請求行 
  26. protected function setLine($method) { 
  27. $this->line[0] = $method . ' ' . $this->url['path'] . '?' .$this->url['query'] . ' '. $this->version; 
  28. // 此方法負責寫頭信息 
  29. public function setHeader($headerline) { 
  30. $this->header[] = $headerline;  
  31. // 此方法負責寫主體信息 
  32. protected function setBody($body) { 
  33. $this->body[] = http_build_query($body); 
  34. // 連接url 
  35. public function conn($url) { 
  36. $this->url = parse_url($url); 
  37. // 判斷端口 
  38. if(!isset($this->url['port'])) { 
  39. $this->url['port'] = 80; 
  40. // 判斷query 
  41. if(!isset($this->url['query'])) { 
  42. $this->url['query'] = ''
  43. $this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3); 
  44. //構造get請求的數據 
  45. public function get() { 
  46. $this->setLine('GET'); 
  47. $this->request(); 
  48. return $this->response; 
  49. // 構造post查詢的數據 
  50. public function post($body = array()) {  
  51. $this->setLine('POST'); 
  52. // 設計content-type 
  53. $this->setHeader('Content-type: application/x-www-form-urlencoded'); 
  54. // 設計主體信息,比GET不一樣的地方 
  55. $this->setBody($body); 
  56. // 計算content-length 
  57. $this->setHeader('Content-length: ' . strlen($this->body[0])); 
  58. $this->request(); 
  59. return $this->response; 
  60. // 真正請求 
  61. public function request() { 
  62. // 把請求行,頭信息,實體信息 放在一個數組里,便于拼接 
  63. $req = array_merge($this->line,$this->header,array(''),$this->body,array('')); 
  64. //print_r($req); 
  65. $req = implode(self::CRLF,$req);  
  66. //echo $req; exit; 
  67. fwrite($this->fh,$req); 
  68. while(!feof($this->fh)) { 
  69. $this->response .= fread($this->fh,1024); 
  70. $this->close(); // 關閉連接 
  71. // 關閉連接 
  72. public function close() { 
  73. fclose($this->fh); 

利用此類發送一個簡單的GET請求:

  1. <?php 
  2. //記得引用Http類 
  3. $url="http://www.companysz.com/u/DeanChopper/"
  4. $http=new Http($url); 
  5. $response=$http->get(); 
  6. print_r($response); 

 

返回值為信息,可以對響應信息進行進一步處理,得到自己想得到的內容。

我們來看下一個具體的實例

 

 
  1. <?php 
  2. /** 
  3. * 使用PHP Socket 編程模擬Http post和get請求 
  4. * @author koma 
  5. */ 
  6. class Http{ 
  7. private $sp = "/r/n"//這里必須要寫成雙引號 
  8. private $protocol = 'HTTP/1.1'
  9. private $requestLine = ""
  10. private $requestHeader = ""
  11. private $requestBody = ""
  12. private $requestInfo = ""
  13. private $fp = null; 
  14. private $urlinfo = null; 
  15. private $header = array(); 
  16. private $body = ""
  17. private $responseInfo = ""
  18. private static $http = null; //Http對象單例 
  19.  
  20. private function __construct() {} 
  21.  
  22. public static function create() { 
  23. if ( self::$http === null ) {  
  24. self::$http = new Http(); 
  25. return self::$http
  26.  
  27. public function init($url) { 
  28. $this->parseurl($url); 
  29. $this->header['Host'] = $this->urlinfo['host']; 
  30. return $this
  31.  
  32. public function get($header = array()) { 
  33. $this->header = array_merge($this->header, $header); 
  34. return $this->request('GET'); 
  35.  
  36. public function post($header = array(), $body = array()) { 
  37. $this->header = array_merge($this->header, $header); 
  38. if ( !emptyempty($body) ) { 
  39. $this->body = http_build_query($body); 
  40. $this->header['Content-Type'] = 'application/x-www-form-urlencoded'
  41. $this->header['Content-Length'] = strlen($this->body); 
  42. return $this->request('POST'); 
  43.  
  44. private function request($method) { 
  45. $header = ""
  46. $this->requestLine = $method.' '.$this->urlinfo['path'].'?'.$this->urlinfo['query'].' '.$this->protocol; 
  47. foreach ( $this->header as $key => $value ) { 
  48. $header .= $header == "" ? $key.':'.$value : $this->sp.$key.':'.$value
  49. $this->requestHeader = $header.$this->sp.$this->sp; 
  50. $this->requestInfo = $this->requestLine.$this->sp.$this->requestHeader; 
  51. if ( $this->body != "" ) { 
  52. $this->requestInfo .= $this->body; 
  53. /* 
  54. * 注意:這里的fsockopen中的url參數形式為"www.xxx.com" 
  55. * 不能夠帶"http://"這種 
  56. */ 
  57. $port = isset($this->urlinfo['port']) ? isset($this->urlinfo['port']) : '80'
  58. $this->fp = fsockopen($this->urlinfo['host'], $port$errno$errstr); 
  59. if ( !$this->fp ) { 
  60. echo $errstr.'('.$errno.')'
  61. return false; 
  62. if ( fwrite($this->fp, $this->requestInfo) ) { 
  63. $str = ""
  64. while ( !feof($this->fp) ) { 
  65. $str .= fread($this->fp, 1024); 
  66. $this->responseInfo = $str
  67. fclose($this->fp); 
  68. return $this->responseInfo; 
  69.  
  70. private function parseurl($url) { 
  71. $this->urlinfo = parse_url($url); 
  72. // $url = "http://news.163.com/14/1102/01/AA0PFA7Q00014AED.html"; 
  73. $url = "http://localhost/httppro/post.php"
  74. $http = Http::create()->init($url); 
  75. /* 發送get請求  
  76. echo $http->get(array( 
  77. 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36', 
  78. )); 
  79. */ 
  80.  
  81. /* 發送post請求 */ 
  82. echo $http->post(array
  83. 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
  84. ), array('username'=>'發一個中文''age'=>22)); 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 91久久综合 | 久久综合给合久久狠狠狠97色69 | 中国杭州少妇xxxx做受 | 日韩每日更新 | 精品一区二区三区毛片 | 黄色高清视频网站 | 亚洲成人在线免费 | 日韩av片网站 | 成人在线视频在线观看 | 蜜桃视频在线播放 | 亚洲婷婷日日综合婷婷噜噜噜 | 久久精品日产高清版的功能介绍 | 美女毛片儿| 7777奇米成人四色影视 | 性生活香蕉视频 | 久章草影院 | 成人羞羞视频在线观看 | 狠狠干天天 | 成人在线免费视频观看 | 在线中文字幕亚洲 | 中文字幕一二三区芒果 | 久久久久久久久久久综合 | 日本特级a一片免费观看 | 欧美成人高清视频 | 日韩欧美综合在线 | 欧美性精品videofree | 国产精品视频不卡 | 欧美一区高清 | 欧美18—19sex性护士中国 | 黄色片网站在线看 | 天天色宗合| 成人福利在线免费观看 | 一本色道精品久久一区二区三区 | 一区二区三视频 | 第一区免费在线观看 | 性欧美极品xxxx欧美一区二区 | 91久久久久久亚洲精品禁果 | 性欧美性欧美 | 欧美一级高清免费 | 久久精品高清 | 日韩理论电影网 |