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的更多相关文章
随机推荐
- ARMLinux下Alignment trap的一些测试 【转自 李迟的专栏 CSDN http://blog.csdn.net/subfate/article/details/7847356
项目中有时会遇到字节对齐的问题,英文为“Alignment trap”,如果直译,意思为“对齐陷阱”,不过这个说法不太好理解,还是直接用英文来表达. ARM平台下一般是4字节对齐,可以参考文后的给出的 ...
- KTV项目 SQL数据库的应用 结合C#应用窗体
五道口北大青鸟校区 KTV项目 指导老师:袁玉明 歌曲播放原理 SQL数据库关系图 C#解决方案类图 第一步:创建数据库连接方法和打开方法和关闭方法! public class DBHelper { ...
- javascript获取asp.net服务器端控件的值
代码如下: <%@ Page Language="C#" CodeFile="A.aspx.cs" Inherits="OrderManage_ ...
- X删除数据表的新用法
删除数据表,可以这样进行,以前傻不拉唧的用sql去手动删除. DAL dal = ... dal.Db.CreateMetaData().SetSche ...
- PKU 3983
很久前写了一个24点的算法,刚好POJ上也有个24点,顺便给解了,POJ上的相对于我原始来写的较为简单许多,因为,限制了数字的位置固定,实际上24点的话是不可能限制这个固定的,所以我之前会对数据进行一 ...
- Android实现Layout缩放动画
最近看到Any.do的缩放效果很酷,看到一篇讲Layout缩放动画实现的文章,记录一下: http://edison-cool911.iteye.com/blog/704812
- RABBITMQ(小总结 持续更新...
(一)理解消息通信 1.消息通信概念---消费者.生产者和代理 生产者(producer)创建消息,然后发送到代理服务器(RaabitMQ). 其中消息包括两部分内容:有效载荷(payload)和标签 ...
- DataTable,DataGridVIew转换到xls 方法 (转)
private void dataTableToCsv(DataTable table, string file) { string title = ""; FileStream ...
- codeforces 731C(DFS)
题目链接:http://codeforces.com/contest/731/problem/C 题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少 ...
- 关系型数据之LinQ基本查询
如下图所示关系型数据,LinQ语句为: