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的更多相关文章
随机推荐
- html之页面元素印射
首先我遇到了一个问题,尽管不是搞前端开发的但事情交到了我这里就有必要去解决. 而这个问题就是我在这边文本框输入的内容要显示在另一个文本框中其实也是非常简单.但是对于初出茅庐的新手来说就有可能会难倒他. ...
- entity framework 新手入门篇(1.5)-lambda表达式与linq
在建立好了EF模型之后,先不着急使用它,在使用它之前,你还需要了解两个相关的技术,lambda表达式与linq. 作为微软C#语言中重要的语法糖-lambda表达式与LINQ,本质都是一个方法,以la ...
- php7.0支持调用lua脚本
需求前瞻: 最近因需求,需要支持php调用现有的Lua代码,可以减少php的代码量,同时在维护上也比较方便,不会照成需求变更,需要同时修改两份代码 第一步:安装php7.0及以上版本,此处安装方法就略 ...
- Java—工厂模式
工厂模式: public interface Cookie { public abstract void run (); } public abstract class CookieFactory { ...
- centos单用户模式修改ROOT密码
首先启动的时候的时候,需要进入单用户模式(进入单用户模式的前提是系统引导器能正常工作),单用户模式是不需要输入密码,并且(进入单用户模式,没有开启网络服务,不支持远程连接 )网上说可以通过GRUB ( ...
- linux-命令-ls
一.命令介绍: ls命令是linux常用的命令之一.ls用来打印当前目录的文件清单或指定目录的文件清单,也可以查看到文件的基本权限和隐藏文件. 二.命令格式: ls [OPTION]... [FILE ...
- okHttp使用
本文转载自:http://www.cnblogs.com/qifengshi/p/5405550.html okHttp是一个http请求框架,相当于android原生的httpclient和http ...
- js one() 方法
js 中有个类似于bind()方法的one() 方法,one()方法的作用是执行一次,为每一个匹配元素的特定事件(像click)绑定一个一次性的事件处理函数. 事例代码如下: <script t ...
- 常用的7个.htaccess代码组织某个国家的IP访问
htAccess 文件(Hypertext Access file)是Apache Web服务器的一个非常强大的配置文件,对于这个文件,Apache有一堆参数可以让你配置出几乎随心所欲的功能. 使用. ...
- PHP explode()函数
源起:将日期格式的字符串拆分成年.月.日,用于组织关系介绍信的特定位置打印.感谢倪同学提供思路 定义和用法 explode()函数把字符串分割为数组 语法 explode(separator,stri ...