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