http协议(2)
2016-03-18 21:14
405 查看
学习资源:燕十八的http课程(公开形式的资源,请自行查找) 二三节
我的笔记:
面向对象
新建一个http请求类,实现自定义接口Proto
在http类中重新实现方法
主要是通过fsockopen打开socket,然后从socket中读取网页资源,写入字符串中,再echo出来,具体的响应体可以通过查看源代码来获知
我的笔记:
面向对象
新建一个http请求类,实现自定义接口Proto
在http类中重新实现方法
主要是通过fsockopen打开socket,然后从socket中读取网页资源,写入字符串中,再echo出来,具体的响应体可以通过查看源代码来获知
<?php //http请求类接口 interface Proto{ //连接url function conn($url); //发送GET请求 function get(); //发送POST请求 function post(); //关闭连接 function close(); } class Http implements Proto{ const CRLF = "\r\n";//carriage return line feed protected $url = array(); protected $line = array(); protected $header = array(); protected $body = array(); protected $fh = null; protected $version = 'HTTP/1.1'; protected $errno = -1; protected $errstr = ''; protected $response = ''; public function __construct($url) { $this->conn($url); $this->setHeader("Host:".$this->url["host"]); } //此方法负责写请求行 protected function setLine($method){ $this->line[] = $method." ".$this->url['path']." ".$this->version; } //此方法负责写请求头 protected function setHeader($headerLine){ $this->header[] = $headerLine; } //此方法负责写主体信息 protected function setBody(){ } //连接url public function conn($url){ $this->url = parse_url($url); //判断端口 if(!isset($this->url['port'])){ $this->url['port'] = 80; } $this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3); } //构造GET请求 public function get(){ $this->setLine("GET"); $this->request(); return $this->response; } //构造POST请求 public function post(){ } //真正请求 public function request(){ //把请求行和body信息放在一个数组中,便于拼接 $req = array_merge($this->line,$this->header,array(''),$this->body,array('')); $req = implode(self::CRLF, $req); //print_r($req); fwrite($this->fh,$req); while(!feof($this->fh)){ $this->response.=fread($this->fh,1024); } $this->close();//关闭连接 } //关闭连接 public function close(){ } } $url = "http://news.163.com/16/0317/20/BICRPR0400014PRF.html"; $http = new http($url); echo $http->get(); //print_r($http); //$http->request();
相关文章推荐
- 极路由的“802.1x手机号登陆wifi”插件,电脑连接wifi方法
- TCP/IP协议栈
- http status 400,http 400,400 错误
- 1570: [JSOI2008]Blue Mary的旅行|网络流
- TCP协议连接建立与连接断开过程(含断开时的TCP状态图)
- 压测网络随记
- IOS 网络浅析-(七 JSON解析之三方JSONKit)
- 基于TCP协议的Socket编程,实现文件的上传
- 常见http状态码详解
- 使用RNN解决句子对匹配问题的常见网络结构
- nginx支持负载均衡及https安装配置
- TCP/IP、UDP、Http、Socket的区别
- http协议返回状态值及意义
- 网络管理
- 神经网络之BP神经网络
- screen命令详解
- hdu 1532 网络流-最大流 DINIC
- 【Win 10 应用开发】TCP通信过程
- 大并发下TCP链接的内存消耗优化小记(80万并发业务正常服务)
- unix 网络编程 头件下载安装