標籤:Proxy 伺服器 伺服器 comm error code 微軟 return float gets
HTTP(一)httpphphttp請求
HTTP請求:請求行、訊息前序、請求本文。格式如下:
Method Request-URI HTTP-Veraion CRLF
參數說明
- Method 要求方法
- Request-URI 一個統一資源識別項
- HTTP-Version 請求的HTTP協議版本
- CRLF 斷行符號和換行
響應:狀態行、訊息前序、響應本文
HTTP-Version Status-Code Reason-Phrase CRLF
參數說明
- HTTP-Verson 伺服器HTTP協議的版本
- Status-Code 伺服器發回的響應狀態代碼
- Reson-Phrase 狀態碼的文本描述
- CRLF 斷行符號和換行
響應狀態代碼:
- 1XX:指示資訊——請求已接收、已處理
- 2XX:成功——請求已被成功接收、理解、接收
- 3XX:重新導向——要完成請求必須進行更進一步的操作
- 4XX:用戶端錯誤——請求有語法錯誤或請求無法實現
- 5XX:服務端錯誤——服務端未能實現合法的請求
常見狀態代碼
- 200 OK:用戶端請求成功
- 400 Bad Request:用戶端請求有語法錯誤,不被伺服器所理解
- 401 Unauthorize:請求未經授權,此狀態代碼必須和WWW Authenticate 前序域一起使用
- 403 Forbidden:伺服器收到請求,但是拒絕提供服務
- 404 Not Found:請求資源不存在
- 500 Internal Server Error :伺服器發生不可預期的錯誤
- 503 Server Unavailable:伺服器當前不能處理用戶端的請求,一段時間後可能恢複正常
相關函數:
get_headers(string $url [, int $format = 0 ] ) — 取得伺服器響應一個 HTTP 要求所發送的所有標題
返回包含有伺服器響應一個 HTTP 要求所發送標題的索引或關聯陣列,如果失敗則返回 FALSE 。 (通過判斷狀態代碼是否為200,就可以判斷請求的資源存在與否)
telnet類比請求
- cmd ->telnet localhost(網域名稱) 80
- 按下crtl+] ,仔按下enter (開啟回顯功能)
3. 發送報文GET /test/httptest.php?id=1 HTTP/1.1Connection: close Host: localhostContent-type:application/x-www-form-urlencodedcontent-length:20(兩個斷行符號)#通過1.1版本協議請求index.html頁面;connection: close是實用短串連,即伺服器返回後就中斷連線;Host欄位知名頁面所在的主機名稱。
測試服務端 httptest.php
<?php$dataGet = $_GET;$dataPost = $_POST;$dataGetstr = http_build_query($dataGet); $dataPoststr = http_build_query($dataPost);if( $dataGet ){echo ‘get ‘.$dataGetstr;}if( $dataPost ){echo ‘post ‘.$dataPoststr;}?>
PHP實現HTTP請求
$postData =http_build_query( array( ‘title‘ => "這裡是 file_get_contents 提交的資料", ‘content‘ => "你好 !", ‘type‘ => 1 ));#file_get_contents$opts = array( ‘http‘ =>array( ‘method‘ => "POST" , ‘header‘ => "Host:localhost\r\n". "Content-type:application/x-www-form-urlencoded\r\n". "Content-length:".(strlen($postData))."\r\n". "Cookie: foo=bar\r\n", "content" => $postData, //‘timeout‘ => 60 * 60 // 逾時時間(單位:s) )); $context = stream_context_create ( $opts );//建立資料流上下文//file_get_contents( ‘http://localhost/test/httptest.php‘,false,$context );#fopen$postData = http_build_query(array( ‘title‘ => "這裡是 fopen 提交的資料", ‘content‘ => "你好 !", ‘type‘ => 1));$fp = fopen( ‘http://localhost/test/httptest.php‘,‘r‘,false,$context );
- stream_context_create 建立並返回一個文本資料流並應用各種選項,可用於fopen(),file_get_contents()等過程的逾時設定、Proxy 伺服器、請求方式、頭資訊設定的特殊過程。
curl方式提交
$url = "http://localhost/test/httptest.php";$postData = http_build_query(array( ‘title‘ => "這裡是 fopen 提交的資料", ‘content‘ => "你好 !", ‘type‘ => 1));$ch = curl_init();curl_setopt( $ch,CURLOPT_URL,$url );curl_setopt( $ch,CURLOPT_POST,1 );curl_setopt( $ch,CURLOPT_POSTFIELDS,$postData );curl_setopt( $ch,CURLOPT_RETURNTRANSFER,1 );curl_exec();curl_close();
socket方式提交
$postData = http_build_query(array( ‘title‘ => "這裡是 fopen 提交的資料", ‘content‘ => "你好 !", ‘type‘ => 1));$fp = fsockopen( "localhost",80,$errno,$errorStr,5 );$request = "POST http://localhost/test/httptest.php HTTP/1.1\r\n";$request .= "Host:localhost\r\n";$request .= "Content-type:application/x-www-form-urlencoded\r\n";$request .= "Content-length:".(strlen($postData))."\r\n";$request .= $postData;fwrite( $fp,$request );while(!feof($fp)){ echo fgets($fp,1024);}fclose($fp);
-- 這段時間讀讀書,理解理解原理,生活也很充實。
HTTP(一)