HTTP协议分析系列(五)------php+socket编程发送http请求
2017-01-22 15:51
856 查看
一、PHP+socket请求原理
二、模拟POST请求
三、封装自己的HTTP类
[php]
view plain
copy
//http请求类的接口
interface Proto{
//连接url
function conn($url);
//发送get查询
function get();
//发送post查询
function post();
//关闭连接
function close();
}
class Http implements Proto{
const CRLF="\r\n";
protected $errno=-1;
protected $errstr='';
protected $response='';
protected $url=null;
protected $version='HTTP/1.1';
protected $fh=null;
protected $line=array();
protected $header=array();
protected $body=array();
public function __construct($url){
$this->conn($url);
$this->setHeader('Host:'.$this->url['host']);
}
//此方法负责写请求行
protected function setLine($method){
$this->line[0]=$method.' '.$this->url['path'].' '.$this->version;
}
//此方法负责写头信息
protected function setHeader($headerline){
$this->header[]=$headerline;
}
//此方法负责写主体信息
protected function setBody($body){
$this->body[]=http_build_query($body);;
}
//连接url
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请求的数据
function get(){
$this->setLine('GET');
$this->request();
return $this->response;
}
//构造post请求的数据
function post($body=array()){
//构造主体信息
$this->setLine('POST');
//设置content-type
$this->setHeader('Content-type:application/x-www-form-urlencoded');
//设置主体信息,比GET不一样的地方
$this->setBody($body);
//计算content-length
$this->setHeader('Content-length:'.strlen($this->body[0]));
$this->request();
return $this->response;
}
//真正请求
function request(){
//把请求行,头信息,实体信息 放在一个数组里,便于拼接
$req=array_merge($this->line,$this->header,array(''),$this->body,array(''));
$req=implode(self::CRLF,$req);
fwrite($this->fh,$req);
while(!feof($this->fh)){
$this->response.=fread($this->fh,1024);
}
$this->close();//关闭连接
return $this->response;
}
//关闭连接
function close(){
fclose($this->fh);
}
}
四、测试功能,实现批量发帖功能
(一)、建立数据表
(二)、程序实现
(1)、建立liuyanben.php
[php]
view plain
copy
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$link=mysql_connect('localhost','root','root');
mysql_select_db('test');
mysql_set_charset('utf8');
if(!empty($_POST)){
$sql="insert liuyanben(id,title,content) values(null,'{$_POST['title']}','{$_POST['content']}')";
mysql_query($sql);
}
$sql="select * from liuyanben";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){
$list[]=$row;
}
?>
<form method='post' action='liuyanben.php'>
<table border='1'>
<tr>
<td>编号</td>
<td>标题</td>
<td>内容</td>
</tr>
<?php
foreach($list as $key=>$value){
?>
<tr>
<td><?php echo $value['id'];?></td>
<td><?php echo $value['title'];?></td>
<td><?php echo $value['content'];?></td>
</tr>
<?php
}
?>
</table>
<input type='text' name='title'><br/>
<input type='text' name='content'><br/>
<input type='submit' value='提交'>
</form>
</body>
</html>
(2).实现批量发帖
[php]
view plain
copy
set_time_limit(0);
$url='http://localhost/socket/liuyanben.php';
for($i=1;$i<100;$i++){
$str=str_shuffle('abcdefghigklmnopqrstuvwxyz');
$title=substr($str,0,5);
$content=substr($str,6,8);
$http=new Http($url);
$http->post(array('title'=>$title,'content'=>$content,'submit'=>'留言'));
echo $title.'--------'.$con.'<br/>';
usleep(2000);
}
(三)、实验结果
查看页面
二、模拟POST请求
三、封装自己的HTTP类
[php]
view plain
copy
//http请求类的接口
interface Proto{
//连接url
function conn($url);
//发送get查询
function get();
//发送post查询
function post();
//关闭连接
function close();
}
class Http implements Proto{
const CRLF="\r\n";
protected $errno=-1;
protected $errstr='';
protected $response='';
protected $url=null;
protected $version='HTTP/1.1';
protected $fh=null;
protected $line=array();
protected $header=array();
protected $body=array();
public function __construct($url){
$this->conn($url);
$this->setHeader('Host:'.$this->url['host']);
}
//此方法负责写请求行
protected function setLine($method){
$this->line[0]=$method.' '.$this->url['path'].' '.$this->version;
}
//此方法负责写头信息
protected function setHeader($headerline){
$this->header[]=$headerline;
}
//此方法负责写主体信息
protected function setBody($body){
$this->body[]=http_build_query($body);;
}
//连接url
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请求的数据
function get(){
$this->setLine('GET');
$this->request();
return $this->response;
}
//构造post请求的数据
function post($body=array()){
//构造主体信息
$this->setLine('POST');
//设置content-type
$this->setHeader('Content-type:application/x-www-form-urlencoded');
//设置主体信息,比GET不一样的地方
$this->setBody($body);
//计算content-length
$this->setHeader('Content-length:'.strlen($this->body[0]));
$this->request();
return $this->response;
}
//真正请求
function request(){
//把请求行,头信息,实体信息 放在一个数组里,便于拼接
$req=array_merge($this->line,$this->header,array(''),$this->body,array(''));
$req=implode(self::CRLF,$req);
fwrite($this->fh,$req);
while(!feof($this->fh)){
$this->response.=fread($this->fh,1024);
}
$this->close();//关闭连接
return $this->response;
}
//关闭连接
function close(){
fclose($this->fh);
}
}
四、测试功能,实现批量发帖功能
(一)、建立数据表
(二)、程序实现
(1)、建立liuyanben.php
[php]
view plain
copy
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$link=mysql_connect('localhost','root','root');
mysql_select_db('test');
mysql_set_charset('utf8');
if(!empty($_POST)){
$sql="insert liuyanben(id,title,content) values(null,'{$_POST['title']}','{$_POST['content']}')";
mysql_query($sql);
}
$sql="select * from liuyanben";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){
$list[]=$row;
}
?>
<form method='post' action='liuyanben.php'>
<table border='1'>
<tr>
<td>编号</td>
<td>标题</td>
<td>内容</td>
</tr>
<?php
foreach($list as $key=>$value){
?>
<tr>
<td><?php echo $value['id'];?></td>
<td><?php echo $value['title'];?></td>
<td><?php echo $value['content'];?></td>
</tr>
<?php
}
?>
</table>
<input type='text' name='title'><br/>
<input type='text' name='content'><br/>
<input type='submit' value='提交'>
</form>
</body>
</html>
(2).实现批量发帖
[php]
view plain
copy
set_time_limit(0);
$url='http://localhost/socket/liuyanben.php';
for($i=1;$i<100;$i++){
$str=str_shuffle('abcdefghigklmnopqrstuvwxyz');
$title=substr($str,0,5);
$content=substr($str,6,8);
$http=new Http($url);
$http->post(array('title'=>$title,'content'=>$content,'submit'=>'留言'));
echo $title.'--------'.$con.'<br/>';
usleep(2000);
}
(三)、实验结果
查看页面
相关文章推荐
- Mixed Content Blocking导致Firefox23 无法显示iframe
- HTTP协议分析系列(四)------重定向
- HTTP协议分析系列(三)------HTTP协议方法和状态码
- HTTP协议分析系列(二)------HTTP请求信息和相应信息的格式
- 手把手教你使用 virtualBox 让虚拟机连接网络
- HTTP协议分析系列(一)------初识HTTP协议
- nginx处理http
- 记录Debug神经网络的方法
- 神经网络与深度学习(第一章)(一)
- 循环神经网络教程第一部分-RNN简介
- Python 3.X 要使用urllib.request 来抓取网络资源。转
- 使用HttpUrlConnection多线程下载文件
- 循环神经网络(Recurrent)——介绍
- 使用nginx作为http负载均衡器
- httpclient的简单使用
- Nginx基于TCP的四层负载均衡介绍
- 发现了一个挺好用的LoadingLayout,网络错误,无数据,动态显示!
- okhttp3.2.0适配https,虽然网络上有很多文章。在此借助前人的经验,做一些处理。
- 强制https访问,在浏览器中输入http的访问地址自动转换为https,端口问题
- tomcat下配置https环境