Use PHP Socket to implement HTTP requests

Source: Internet
Author: User
PHP Socket is used to implement HTTP requests. Reading the PHP Socket is used to implement HTTP requests. recently, many HTTP remote access programs have been written. the WebHttpRequest class and Microsoft. XMLHTTP is a common object. with these convenient classes, we can simulate Web client access through C #, ASP, and Javascript.

Recently, HTTP remote access programs have been written much more, mainly in contact. the WebHttpRequest class and Microsoft. XMLHTTP is a common object. with these convenient classes, we can simulate Web client access to other Web pages through C #, ASP, and Javascript. But today, I want to use PHP to implement similar functions, but I cannot find a direction for the moment, so I can only "enrich myself.

Currently, there are no classes similar to HttpRequest in PHP, but such an HttpRequest Class page is found in the official online document, however, this function may only exist in the CVS version...

To implement HTTP access, you only need to use a Socket to send a request and then read the returned information. So I used Socket to write an HttpRequest class. of course, I can also use the fsockopen () function. However, the difference between the two is not great. the specific code is included in the attachment, you can check it yourself.

SHostAdd = gethostbyname ($ armatchillpart [1]); if (empty ($ armatchillpart [4]) {$ this-> sUri = '/';} else {$ this-> sUri = $ armatchillpart [4];} if (empty ($ armatchillpart [3]) {$ this-> iPort = 80 ;} else {$ this-> iPort = $ armatchillpart [3];} $ this-> addRequestHeader ('host :'. $ armatchillpart [1]); $ this-> addRequestHeader ('connection: close');} function addRequestHeader ($ sHeader) {$ this-> sRequestHeader. = trim ($ sHeader ). "\ R \ n ";} function sendRequest ($ sMethod = 'get', $ sPostData ='') {$ sRequest = $ sMethod. "". $ this-> sUri. "HTTP/1.1 \ r \ n"; $ sRequest. = $ this-> sRequestHeader; if ($ sMethod = 'post') {$ sRequest. = "Content-Type: application/x-www-form-urlencoded \ r \ n"; $ sRequest. = "Content-Length :". strlen ($ sPostData ). "\ r \ n"; $ sRequest. = "\ r \ n"; $ sRequest. = $ sPostData. "\ r \ n" ;}$ sRequest. = "\ r \ n"; $ sockHttp = socket_create (AF _ INET, SOCK_STREAM, SOL_TCP); if (! $ SockHttp) {die ('socket _ create () failed! ') ;}$ ResSockHttp = socket_connect ($ sockHttp, $ this-> sHostAdd, $ this-> iPort); if (! $ ResSockHttp) {die ('socket _ connect () failed! ');} Socket_write ($ sockHttp, $ sRequest, strlen ($ sRequest); $ this-> sResponse = ''; while ($ sRead = socket_read ($ sockHttp, (4096) {$ this-> sResponse. = $ sRead;} socket_close ($ sockHttp);} function getResponse () {return $ this-> sResponse;} function getResponseBody () {$ sPatternSeperate = '/\ r \ n/'; $ arMatchResponsePart = preg_split ($ sPatternSeperate, $ this-> sResponse, 2 ); return $ arMatchResponsePart [1];} function GetResponseHead () {$ sPatternSeperate = '/\ r \ n/'; $ arMatchResponsePart = preg_split ($ sPatternSeperate, $ this-> sResponse, 2 ); return $ arMatchResponsePart [0] ;}}?>

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.