sokite
<?php
interface Proto {
//连接
function conn($url);
//发送get请求
function get();
//发送post请求
function post();
//关闭连接
function close($f);
}
class Http implements Proto {
const CRLF = "\r\n";
protected $errno = -1;
protected $errstr = '';
protected $response = '';
protected $timeout = 3;
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']);
}
//写请求行
public function setLine($method){
$this->line[0] = $method . ' ' . $this->url['path'].'?'.$this->url['query'] . ' '. $this->version;
}
//写头信息
protected function setHeader($headerline){
$this->header[] = $headerline;
}
//写主体信息
protected function setBody($body){
$this->body[] =http_build_query($body);
}
//连接
public function conn($url){
$this->url = parse_url($url);
//判断多口
if(!isset($this->url['port'])){
$this->url['port'] = 80;
}
//fsockopen — 打开一个网络连接或者一个Unix套接字连接
$this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,$this->timeout);
}
//发送get请求
public function get(){
$this->setLine('GET');
$this->request();
return $this->response;
}
//发送post请求
public function post($body=array()){
$this->setLine('POST');
$this->setBody($body);
$this->setHeader('Content-Type: application/x-www-form-urlencoded'.self::CRLF.'Content-Length:'.strlen($this->body[0]));
$this->request();
return $this->response;
}
//真正请求
public function request(){
//请求行,头信息,实体信息,放在同一个数组,便于拼接
$req = array_merge($this->line,$this->header,array(''), $this->body ,array(''));
$req = implode(self::CRLF, $req );
// print_r($this);
// print_r($req);
// die;
fwrite($this->fh, $req);
while (!feof($this->fh)) {
$this->response .= fread($this->fh, 512) ;
}
$this->close($this->fh);
}
//关闭连接
public function close($f){
fclose($f);
}
}
/*
//循环灌水
set_time_limit(0);
for($i = 0; $i<10; $i++){
$str = str_shuffle('abcdefjghiqeuoiqutqkldfjmnzbcznasdjkhfaklwghtbfvh12354654879');
$data = array(
'dname' => substr($str, 0,5),
'xname' => substr($str, 6,9),
'intro' => substr($str, 10,16),
'cat_id' =>''
);
$url = 'http://127.0.0.1/shang/admin/boardAct.php';
$http = new Http($url);
echo $http->post($data);
}
*/
// $url = 'http://127.0.0.1/shang/a1.php';
// $http = new Http($url);
// echo $http->get();
$url = 'http://127.0.0.1/shang/a1.php?d=3&c=1';
$http = new Http($url);
echo $http->post(array(1=>1,2=>2));
sokite的更多相关文章
随机推荐
- DOM扩展之 HTML5
11.3.1 与类相关的扩充 2. classList属性 在操作类名时,需要通过className属性添加,删除和替换类名.因为className是一个字符串,所以即使只修改一部分内容也要设置整个字 ...
- iis express 局域网访问
查找到配置文件添加一条绑定局域网可访问的IP地址即可: 事例: <site name="Join.Lims.Mob(11)" id="19"> &l ...
- docker push 实现过程
这一篇文章分析一下docker push的过程:docker push是将本地的镜像上传到registry service的过程: 根据前几篇文章,可以知道客户端的命令是在api/client/pus ...
- PHPSTORM模板变量注释
类似于这种注释,方便使用. 有两种方式一种是生成PHP文件时,自动生成,一种是手动生成. 第一种:自动生成 一图解释所有.这么配置就OK了. 这种方法还有一种就是,在包括里边编写,直接引用,先写PHP ...
- 2016-08-01一起领略ReactJs的风采
现在最热门的前端框架有AngularJS.React.Bootstrap等.自从接触了ReactJS,ReactJs的虚拟DOM(Virtual DOM)和组件化的开发深深的吸引了我,下面来跟我一起领 ...
- RabbitMQ - TcpConnection析构引发的一次handshake_timeout
使用RabbitMQ时,连接rabbit-server一直连接失败,代码没有任何错误提示.但是通过rabbitmqctl始终查询不到连接以及创建的queue等信息. 官方的文件demo里面也没有Tcp ...
- js 闭包演示
function test2() { var scope = "global scope"; var f = enclose(scope); scope = 'aaa'; aler ...
- LinuxMint17.1 Rebecca中安装设置输入法
LinuxMint14使用了几年一直未更新,突然想去更新一下去发现源已经不支持了,所以就直接安装了最新版本. 安装好以后发现还是跟以前一样的毛病,没有中文输入法,直接sudo aptitude ins ...
- 带head的gridview
这是github上的一个项目,根据谷歌的那个HeadGridView改的,因为谷歌的那个addHeadView后宽度不能填充屏幕,下面是代码. 来源:https://github.com/liaohu ...
- iOS中的extern与static
1.extern #import <Foundation/Foundation.h> extern NSString *DBDefaultName; @interface DataBase ...