php利用socket函数模拟发送http请求
来源:互联网 发布:淘宝平台费用是多少 编辑:程序博客网 时间:2024/05/20 09:26
- <?php
- /*
- * Network: HTTP request object* Author: Li HouYu (Karajan Lee), Shanghai, China.
- */
- class HttpRequest{
- private $sHostAdd;//服务器ip
- private $sUri;//请求的文件
- private $iPort;//服务器端口
- private $sRequestHeader;//请求头信息
- private $sResponse;//请求信息
- //构造函数
- function __construct($sUrl){
- $sPatternUrlPart = '/http:\/\/([a-z-\.0-9]+)(:(\d+)){0,1}(.*)/i';
- $arMatchUrlPart = array();
- preg_match($sPatternUrlPart, $sUrl, $arMatchUrlPart);
- $this->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)."\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\r\n/';
- $arMatchResponsePart = preg_split($sPatternSeperate, $this->sResponse, 2);
- return $arMatchResponsePart[1];
- }
- //获取响应头
- function getResponseHead(){
- $sPatternSeperate = '/\r\n\r\n/';
- $arMatchResponsePart = preg_split($sPatternSeperate, $this->sResponse, 2);
- return $arMatchResponsePart[0];
- }
- }
- $url='http://www.baidu.com';
- $httpRequest=new HttpRequest($url);
- $httpRequest->sendRequest();
- file_put_contents('temp.txt',$httpRequest->getResponse());
- echo $httpRequest->getResponseBody();
- ?>
0 0
- php利用socket函数模拟发送http请求
- php利用socket函数模拟发送http请求
- php利用socket函数模拟发送http请求
- php利用socket函数模拟发送http请求
- socket模拟http发送请求
- 利用Socket模拟http请求
- PHP SOCKET模拟HTTP请求
- 利用fsockopen模拟HTTP发送请求
- [php]模拟socket发送GET,POST请求
- 模拟发送http请求
- socket 发送http请求
- socket发送http请求
- socket 发送http请求
- 继续研究socket模拟浏览器发送http请求 大家帮忙
- socket模拟http请求
- socket模拟http请求
- socket模拟http请求
- PHP中以SOCKET方式发送HTTP请求的函数及使用方法
- ucos-ii示例6:消息量集测试
- JMeter压力测试使用入门
- c++ operator的两种应用
- iOS 关于ios中的setNeedsDisplay和setNeedsLayout
- Lambda表达式
- php利用socket函数模拟发送http请求
- my epoll
- hdu5094(BFS+状压)
- uvaoj 147 - Dollars 动态规划
- java内省操作
- linux之 sed的用法
- 多线程中引入Spring自动注入的对象为null
- UVA 11729 Commando War
- Android 快速开发框架androidannotations详解