PHP Parsing HTTP protocol

Source: Internet
Author: User

This article uses PHP to parse the HTTP protocol

The HTTP protocol consists of two parts: the request header and the request body, and we mainly parse the request header to obtain cookie,get,post information.

http/1.1 Okdate:sun, Oct 15:43:47 gmtserver:apacheset-cookie:phpsessid=4v2actjf96v614r2kh36380kq6; Path=/expires:thu, 1981 08:52:00 Gmtcache-control:privatepragma:no-cachevary:accept-encodingcontent-length: 5105connection:closecontent-type:text/html; Charset=utf-8

  

The analysis shows that each line is ended with \ r \ n, the HTTP response header header and the response body body are split with \r\n\r\n, and the relevant elements can be extracted by regular expressions below.

The code is as follows.

/************** Test End ****************************//** Function: parse_http* Description: Parsing http protocol */function parse_http ($http) {        Initialize $_post = $_get = $_cookie = $_request = $_session = $_files = Array ();        $GLOBALS [' http_raw_post_data '] = ';              The variable name that needs to be set $_server = Array (' query_string ' = = ', ' request_method ' = ' = ',              ' Request_uri ' = ' ', ' server_protocol ' = ', ' and ' server_software ' and ', ' ' server_name ' = ' ', ' http_host ' = ', ' and ', ' http_user_agent ' and ', ' HTT              P_accept ' + ' ', ' http_accept_language ' = ', ', ' http_accept_encoding ' and ', ' ' Http_cookie ' + ' ', ' http_connection ' = ', ', ' remote_addr ' + ', ' REM                Ote_port ' = ' 0 ',); Divide the header into the array list ($http _header, $http _body) = Explode ("\r\n\r\n", $httP, 2);                $header _data = Explode ("\ r \ n", $http _header); List ($_server[' Request_method '), $_server[' Request_uri '], $_server[' server_protocol ']) = Explode (", $header _data[0                ]);        Unset ($header _data[0]);                foreach ($header _data as $content) {//\r\n\r\n if (empty ($content)) {            Continue            } list ($key, $value) = Explode (': ', $content, 2);            $key = Strtolower ($key);            $value = Trim ($value);  Switch ($key) {//Http_host case ' HOST ': $_server[' http_host ']                    = $value;                    $tmp = Explode (': ', $value);                    $_server[' server_name ' = $tmp [0];                    if (Isset ($tmp [1])) {$_server[' server_port ') = $tmp [1];                } break;                  Cookie case ' cookie ':  $_server[' http_cookie '] = $value; Parse_str (Str_replace (';                    ', ' & ', $_server[' Http_cookie '), $_cookie);                Break                    User-agent case ' user-agent ': $_server[' http_user_agent '] = $value;                Break                    Accept case ' accept ': $_server[' http_accept '] = $value;                Break                    Accept-language case ' accept-language ': $_server[' http_accept_language '] = $value;                Break                    accept-encoding case ' accept-encoding ': $_server[' http_accept_encoding '] = $value;                Break                    Connection case ' connection ': $_server[' http_connection '] = $value;                Break             Case ' referer ': $_server[' http_referer '] = $value;       Break                    Case ' if-modified-since ': $_server[' http_if_modified_since '] = $value;                Break                    Case ' If-none-match ': $_server[' http_if_none_match '] = $value;                Break Case ' Content-type ': if (!preg_match ('/boundary= "? (                    \s+) "?/', $value, $match)) {$_server[' content_type '] = $value; } else {$_server[' content_type '] = ' multipart/form-data '                        ;                    $http _post_boundary = '--'. $match [1];            } break; }}//need to parse $_post if ($_server[' request_method ' = = = = ' POST ') {if (Isset ($_se rver[' Content_Type ') && $_server[' content_type '] = = = ' Multipart/form-data ') {PARSE_UPL Oad_files ($http _body, $http _post_boundary);                } else {parse_str ($http _body, $_post);            $GLOBALS [' Http_raw_post_data '] $GLOBALS [' http_raw_post_data '] = $http _body; }}//Query_string $_server[' query_string '] = Parse_url ($_server[' Request_uri '), Php_url_que        RY);        if ($_server[' query_string ') {//$GET parse_str ($_server[' query_string '), $_get);        } else {$_server[' query_string '] = ';                }//REQUEST $_request = Array_merge ($_get, $_post); Return array (' Get ' =>$_get, ' post ' =>$_post, ' cookie ' =>$_cookie, ' server ' =>$_server, ' files ' =>$_files) ; }/** function: parse_upload_files* Description: Parse the uploaded file */function parse_upload_files ($http _body, $http _post_boundary) {$        Http_body = substr ($http _body, 0, strlen ($http _body)-(strlen ($http _post_boundary) + 4)); $boundary _data_array = Explode ($http_post_boundary. "        \ r \ n ", $http _body);        if ($boundary _data_array[0] = = = ") {unset ($boundary _data_array[0]); } foreach ($boundary _data_array as $boundary _data_buffer) {list ($boundary _header_buffer, $boundar            Y_value) = explode ("\r\n\r\n", $boundary _data_buffer, 2);            Remove the end \ r \ n $boundary _value = substr ($boundary _value, 0,-2);  foreach (Explode ("\ r \ n", $boundary _header_buffer) as $item) {list ($header _key, $header _value) =                Explode (":", $item);                $header _key = strtolower ($header _key);                        Switch ($header _key) {case "content-disposition"://Is File if (Preg_match ('/name= ". *?); Filename= "(. *?)"                                $/', $header _value, $match)) {$_files[] = array ( ' file_name ' = $match [1], 'File_data ' = $boundary _value, ' file_size ' and strlen ($boundary _value),                            );                        Continue  }//Is post field else {// Collect post if ('/name= ' (. *?) "Preg_match $/', $header _value, $match)) {$_post[$match [1]] = $boundary _val                            Ue                }} break; }            }        }}

Note that the $_post,$_get,$_cookie,$_request,$_session,$_files are now resolved by our own protocol, not by PHP.

Under test code:

$ch = Curl_init (); curl_setopt ($ch, Curlopt_url, ' http://githubs.cn '); curl_setopt ($ch, curlopt_header,1); curl_setopt ($ch, curlopt_returntransfer,1); curl_setopt ($ch, curlopt_followlocation,0);//grab Jump $output = Curl_ EXEC ($ch); Curl_close ($ch); Var_dump (Parse_http ($output));    

PHP Parsing HTTP protocol

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.