Implement HTTP request _php tutorial with PHP socket

Source: Internet
Author: User

The recent HTTP remote Access program is much more written, the main contact is. NET class Library in the Webhttprequest class and Microsoft.XMLHTTP this generic object, with these convenient classes, we can use the C#,asp,javascript to simulate the Web Client access to other Web pages. But today I want to use PHP to achieve a similar function, but for a moment can not find the direction, so only their own "clothed".

Currently, PHP does not have a similar HttpRequest class, but found in the official online documents such a HttpRequest class page, but only mentioned that this feature may only exist in the CVS version, helpless ...

The only way to implement HTTP access is to send a request with the socket and then read the return information. So I used the socket to write a HttpRequest class, of course, can also be used fsockopen () This function to achieve, but the difference is not very large two, the specific code in the attachment, see for themselves.

Shostadd = gethostbyname ($arMatchUrlPart [1]), if (Empty ($arMatchUrlPart [4])) {$this->suri =/;} else{$this->suri = $arMatchUrlPart [4];} if (Empty ($arMatchUrlPart [3])) {$this->iport = 80;} else{$this->iport = $arMatchUrlPart [3];} $this->addrequestheader (Host:. $arMatchUrlPart [1]); $this->addrequestheader (connection:close);} function Addrequestheader ($sHeader) {$this->srequestheader. = Trim ($sHeader). ""; function SendRequest ($sMethod = GET, $sPostData =) {$sRequest = $sMethod. " ". $this->suri." http/1.1 "; $sRequest. = $this->srequestheader;if ($sMethod = = POST) {$sRequest. =" content-type:application/ X-www-form-urlencoded "; $sRequest. =" Content-length: ". strlen ($sPostData)." "; $sRequest. = ""; $sRequest. = $sPostData. "";} $sRequest. = ""; $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 =//; $arMatchResponsePart = Preg_split ($sPatternSeperate, $this- Sresponse, 2); return $arMatchResponsePart [1];} function Getresponsehead () {$sPatternSeperate =//; $arMatchResponsePart = Preg_split ($sPatternSeperate, $this- Sresponse, 2); return $arMatchResponsePart [0];}}? >

http://www.bkjia.com/PHPjc/508281.html www.bkjia.com true http://www.bkjia.com/PHPjc/508281.html techarticle the recent HTTP remote Access program is much more written, the main contact is. NET class Library in the Webhttprequest class and Microsoft.XMLHTTP this universal object, with these convenient classes, we can pass ...

  • 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.