PHP+SOCKET 模拟HTTP请求
HTTP消息结构
客户端请求包括四部份:请求行(状态行)、请求头、空行、请求主体(数据),如下图:
服务端响应包括四部份:响应行(状态行)、响应头、空行、响应主体(数据),如图:
HTTP请求方法:
POST 、GET 、HEADE、 PUT、 TRACE 、DELETE 、OPTIONS 、CONNECT (前三种最实用),有这麽多的请求方法,但web服务器不一定所有的都支持。
GET 基本一致,请求指定的页面信息,并返回实体主体。
HEAD 基本和GET一致 ,只不过返回的响应中没有具体的内容,用于获取报头
POST 向指定资源提交数据进行处理请求(例如表单、上传文件)。数据被包含在请求体中。该请求可能会导致新资源的建立或 已有资源的修改
OPTIONS 返回服务器可用的方法 ,允许客户端查看服务器的性能。
PUT 从客户端向服务器传送的数据取代指定的文档的内容。
DELETE 请求服务器删除指定的页面
CONNECT HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。
TRACE 回显服务器收到的请求,主要用于测试或诊断。
HTTP状态码
HTTP状态码分五类
1xxx 接收到请求,继续处理
2xxx 请求成功
3xxx 重定向
4xxx 客户端错误
5xxx 服务器请求错误
常用的常态码
200 请求成功
301/302 永久/临时重定向
304 未修改,从缓存中进行读取
404 资源不存在
500 服务器内部错误
307 重定向中保持原有得数据不丢失
503 服务器暂时不可用
根据上面的对HTTP有了一定的了解,根据对应的请求的方式进行拼接
PHP+SOCKET 模拟HTTP请求
接口
**
* Interface php_socket
*/
interface php_socket
{
/**
* @return mixed
*/
public function get(); /**
* @return mixed
*/
public function post($param); /**
* @return mixed
*/
public function request(); /**
* @return mixed
*/
public function header_info(); /**
* @return mixed
*/
public function body_info($param); /**
* @return mixed
*/
public function connect();
}
实现接口
class implement_socket implements php_socket
{
protected $http_tpye = 'HTTP/1.1';
protected $url = '';
protected $request_type = '';
protected $lines = '';
protected $fsoket_open = '';
protected $port = '';
protected $errstr = '';
protected $timeout = 0;
protected $parse_url = '';
protected $content_type = '';
protected $content_length = 0;
protected $body = ''; function __construct($url, $request_type = '', $port = 80, $timeout = 5)
{
$this->url = $url;
$this->request_type = $request_type;
$this->port = $port;
$this->timeout = $timeout;
$this->parse_url = parse_url($url);
//链接
$this->connect(); } /*
*设置请求行
* */
public function get()
{
$this->content_type = 'text/html';
$this->lines = $this->request_type . ' ' . $this->parse_url['path'] . ' ' . $this->http_tpye;
$this->request();
} public function post($param)
{
//设置头信息
$this->content_type = 'application/x-www-form-urlencoded';
$data = $this->body_info($param);
$this->content_length = strlen($data);
$this->lines = $this->request_type . ' ' . $this->parse_url['path'] . ' ' . $this->http_tpye;
$this->body = $data;
$this->request();
} public function request()
{
$getinfo = '';
echo $this->lines . "\r\n" . implode("\r\n", $this->header_info()) . " \n\r\n" . $this->body;
exit();
//链接成功进行写入头信息
fwrite($this->fsoket_open, $this->lines . "\n" . implode("\n", $this->header_info()) . "\n\r\n" . $this->body);
while (!feof($this->fsoket_open)) {
$getinfo .= fgets($this->fsoket_open, 1024);
}
fclose($this->fsoket_open);
echo "以下是获取的信息:<br>" . $getinfo;
} /*
* 链接
* */
public function connect()
{
try {
$this->fsoket_open = fsockopen($this->parse_url['host'], $this->port, $this->errstr, $this->timeout);
} catch (Exception $exception) {
echo 'connect is failed :' . $exception->getMessage() . 'r\n' . $this->errstr;
}
} /*
* 设置头信息
* */
public function header_info()
{
return array(
"Host:" . $this->parse_url['host'],
"Content-type:" . $this->content_type,
"Content-length:" . $this->content_length
);
} /*
* 设置主体
* */
public function body_info($param)
{
// 生成 URL-encode 之后的请求字符串
return http_build_query($param);
}
}
最新文章
- 关于mysql数据库的备份和还原
- C#.NET 大型企业信息化系统集成快速开发平台 4.1 版本 - 面向数据库SQL语句的应用开发二
- Linux C 字符串函数 strlen()、strcat()、strncat()、strcmp()、strncmp()、strcpy()、strncpy() 详解
- portotype
- 查看外网出口IP &;&; Traceroute
- Hibernate(九)__OpenSessionInView解决懒加载问题
- DDD:当视图模型、领域模型和数据模型都采用了同样的类型的时候,我们该如何处理?
- selenium+python笔记6
- HDU 3333 Turing Tree (树状数组)
- java I/O Stream 代码学习总结
- centos 6.3安装mono和monoDevelop过程
- JavaDoc的生成规则---ShinePans
- Java NIO之内存映射文件——MappedByteBuffer
- Linux学习 -- 备份与恢复
- 分30条依次解析xml并插入数据库成功
- 【BZOJ 3754】: Tree之最小方差树
- Vue 中动态添加class(使用v-bind:class)
- 13-03 Java 基本类型包装类概述,Integer类,Character
- oracle创建表空间 授权
- 20165236 2017-2018-2 《Java程序设计》结对编程练习_四则运算