socket类主体
<?php
/*
PHP+socket编程 发送http请求
要求能 模拟下载 注册 登录 批量发帖
*/
set_time_limit(0);
//http请求类的接口
interface Proto
{
// 连接url
function conn($url);
// 发送get查询
function get();
// 发送post查询
function post();
// 关闭连接
function close();
}
class Http implements Proto
{
const CRLF="\r\n";
protected $errno=-1;
protected $errstr='';
protected $response='';
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']);
}
// 次此方法复杂写请求行
protected function setLine($method)
{
$this->line[0]=$method.' '.$this->url['path'].'?'.$this->url['query'].' '. $this->version;
}
// 此方法复杂写头信息
public function setHeader($headerline)
{
$this->header[]=$headerline;//'Host: '.$this->url['host'];
}
// 此方法负责写主体信息
protected function setBody($body)
{
$this->body[]=http_build_query($body);
}
// 连接url
public function conn($url)
{
$this->url=parse_url($url);
// 判断端口
if(!isset($this->url['port'])){
$this->url['port']=80;
}
$this->fh=fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);
}
// 构造get请求的数据
public function get()
{
$this->setLine('GET');
$this->request();
return $this->response;
}
// 发送post查询
public function post($body=array()){
$this->setLine('POST');
// 设置content-type
$this->setHeader('Content-type: application/x-www-form-urlencoded');
// 设置主体信息
$this->setBody($body);
// 计算conteng-length
$this->setHeader('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(''));
//print_r($req);exit;
$req=implode(self::CRLF,$req);
//echo $req;exit();
fwrite($this->fh, $req);
while (!feof($this->fh)) {
$this->response .=fread($this->fh, 1024);
}
$this->close();//关闭连接
}
// 关闭连接
public function close(){
fclose($this->fh);
}
}
使用部分:
qq空间发说说
冒用cookie
<?php
require('./http.class.php');
$url='http://user.qzone.qq.com/q/taotao/cgi-bin/emotion_cgi_publish_v6?g_tk=1669290965';
$http=new http($url);
$http->setHeader('Accept: */*');
$http->setHeader('Accept-Encoding: gzip,deflate,sdch');
$http->setHeader('Accept-Language: zh-CN,zh;q=0.8');
$http->setHeader('Avail-Dictionary: XprLfaX');
$http->setHeader('Connection: keep-alive');
$http->setHeader('Cookie: RK=ROPbchyMX+; pgv_pvid=4907422075; __Q_w_s_hat_seed=1; __Q_w_s__QZN_TodoMsgCnt=1; pgv_info=ssid=s9782741028; pt2gguin=o0648462920; uin=o0648462920; skey=@f3amg7Sh0; ptisp=ctc; qzone_check=648462920_1429170797; ptcz=161b4251cc65c85db65e0be64499ed288ca48ed54c9c4fd92ae9490d1985d4a6; Loading=Yes; qzspeedup=sdch; p_skey=iSzj3pwpcmGA76NGPz9f3BuXWZ-8AYUJxRXG-pKhe0c_; pt4_token=eyYb7KCCCiDqU9srXOh*Cw__; Hm_lvt_f5127c6793d40d199f68042b8a63e725=1429083144,1429146692; Hm_lpvt_f5127c6793d40d199f68042b8a63e725=1429170688; blabla=dynamic; TKmsg=true; TKPaoPao=true; qz_screen=1440x900; QZ_FE_WEBP_SUPPORT=1; cpu_performance_v8=2');
$http->setHeader('Origin: http://user.qzone.qq.com');
$http->setHeader('Referer: http://user.qzone.qq.com/648462920/infocenter?ptsig=PjlIX0R4XHAPfJPx*Of*L8*v4NqwsyicgSMmFAWoK3Y_');
$http->setHeader('User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36');
$http->setHeader('X-Real-Url: http://taotao.qq.com/cgi-bin/emotion_cgi_publish_v6?g_tk=1669290965');
$msg=array('syn_tweet_verson'=>1,
'paramstr'=>1,
'pic_template'=>'',
'richtype'=>'',
'richval'=>'',
'special_url'=>'',
'subrichtype'=>'',
'who'=>1,
'con'=>'我是机器人4',
'feedversion'=>1,
'ver'=>1,
'ugc_right'=>1,
'to_tweet'=>0,
'to_sign'=>0,
'hostuin'=>'648462920',
'code_version'=>1,
'format'=>'fs',
'qzreferrer'=>'http://user.qzone.qq.com/648462920/infocenter?ptsig=PjlIX0R4XHAPfJPx*Of*L8*v4NqwsyicgSMmFAWoK3Y_',
);
file_put_contents('./mao.txt', $http->post($msg));