Home >Backend Development >PHP Tutorial >Use PHP Socket to implement HTTP requests_PHP tutorial

Use PHP Socket to implement HTTP requests_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:03869browse

Recently, I have written a lot of HTTP remote access programs. I mainly come into contact with the WebHttpRequest class in the .NET class library and the common object Microsoft.XMLHTTP. With these convenient classes, we can use C#, ASP, Javascript to simulate Web clients accessing other Web pages. But today I wanted to use PHP to implement a similar function, but I couldn't find the direction for a while, so I had to "have enough food and clothing" by myself.

Currently, there is no class similar to HttpRequest in PHP. However, I found such a page of HttpRequest Class in the official online documentation, but it only mentioned that this function may only exist in the CVS version. Unfortunately... .

To implement HTTP access is nothing more than sending a request using Socket and then reading the return information. So I wrote an HttpRequest class using Socket. Of course, it can also be implemented using the fsockopen() function, but the difference between the two is not very big. The specific code is in the attachment. Take a look for yourself.

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];}}?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508281.htmlTechArticleRecently, I have written a lot of programs for HTTP remote access. I mainly come into contact with the WebHttpRequest class and the .NET class library. Microsoft.XMLHTTP is a universal object. With these convenient classes, we can pass...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn