<?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. Spring3.0 与 MyBatis框架 整合小实例

    本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...

  2. RecyclerView因版本问题无法加载

    前几天在学习RecyclerView时候,一直失败,各种加载不上.下面是错误信息 D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL ...

  3. protobuf简介和使用

    1.Protocol Buffers简介 Protocol Buffers (ProtocolBuffer/ protobuf )是Google公司开发的一种数据描述语言,类似于XML能够将结构化数据 ...

  4. JavaScript数组模拟栈和队列

    *栈和队列:js中没有真正的栈和队列的类型              一切都是用数组对象模拟的 栈:只能从一端进出的数组,另一端封闭       FILO   何时使用:今后只要仅希望数组只能从一端进 ...

  5. C#数组全解

    数组概述 C# 数组从零开始建立索引,即数组索引从零开始.C# 中数组的工作方式与在大多数其他流行语言中的工作方式类似.但还有一些差异应引起注意. 声明数组时,方括号 ([]) 必须跟在类型后面,而不 ...

  6. UCanCode发布升级E-Form++可视化源码组件库2015全新版 (V23.01)!

    2015年4月. 成都 UCanCode发布升级E-Form++可视化源码组件库2015全新版 (V23.01)! --- 全面性能提升,UCanCode有史以来最强大的版本发布! E-Form++可 ...

  7. php大力力 [051节] 支付宝支付.申请支付资质,等待审核中

    https://beecloud.cn/doc/payapply/?index=6 支付宝支付申请支付资质 一.注册支付宝用户 在支付宝官网注册成为用户 二.签约对应支付产品 应用集成支付宝支付,需要 ...

  8. JavaScript构造函数学习笔记

    1 理解Javascript constructor实现原理 在 JavaScript 中,每个函数都有名为“prototype”的属性,用于引用原型对象.此原型对象又有名为“constructor” ...

  9. mongodb C# 驱动查询

    INoSqlProvider provider = NoSqlManager.Create("CloudTable"); IMongoCollection<FormMongo ...

  10. 最全的运营推广方案,教你如何从零开始运营APP

    转载自:最全的运营推广方案,教你如何从零开始运营APP(http://www.kusocial.com/9319 ) 最全的运营推广方案,教你如何从零开始运营APP 2014-10-07 • 运营 • ...