<?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的更多相关文章

随机推荐

  1. spring 包下载地址

    留着,以备不时之需: http://repo.spring.io/libs-release-local/org/springframework/spring/

  2. Java随笔三

    1.接口: 1)接口不是类,不能使用new运算符实例化一个接口,而是对类的一组需求描述,这些类要遵循接口描述的统一格式进行定义.个人感觉像C语言中的函数在头文件中的预先声明,但是包含了一些类的特点,比 ...

  3. MVC 架构

    MVC 模式是一种严格实现应用程序个部分隔离的框架模式.这种"隔离"也叫"分离关注点" 通俗名称:"松耦合" 松耦合的应用程序价格设计方式, ...

  4. MongoDB初学笔记

    http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html

  5. 细节小bug

    1. function devChange(value){ $("#multipleLeft").empty(); ReportRemote.getDeviceFlow(value ...

  6. 1.Basic Techniques and Knowledge

    1.1 BASIC WINDOWS PROGRAMMING IN C/C++ 1.Hello World Version 1:Starting Your Browser Let's get down ...

  7. C++学习笔记28:运行期型式信息

    RTTI 运行期标识对象的型式信息 优势:允许使用指向基类的指针或引用自如地操作派生类的对象 typeid:获取表达式的型式:type_info:型式信息类 头文件:typeinfo 对象转型模板 d ...

  8. php读取json时无数据(为空)的解决方法

    在使用PHP调用一些json接口文件时 如果使用 file_get_contents 获取页面json数据后 再使用json_decode()解析后 数据无法正常输出 这是的返回值为null 这是由于 ...

  9. windows 下面安装npm

    npm(简称:Node Packaged Modules)是nodejs官方未nodejs定制的一个工具,是Node.js的包管理器,通过npm可以下载安装nodejs的模块包,nodejs有很多优秀 ...

  10. 原生js实现滚动条

    var SimulateScroll = (function(){ var oParent = document.getElementById('wrap-scroll-bar'), oBox = d ...