一、php+socket請求原理
二、模擬POST請求
三、封裝自己的HTTP類
//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->version;
}
//此方法負責寫頭信息
protected function setHeader($headerline){
$this->header[]=$headerline;
}
//此方法負責寫主體信息
protected function setBody($body){
$this->body[]=http_build_query($body);;
}
//連接url
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請求的數據
function get(){
$this->setLine('GET');
$this->request();
return $this->response;
}
//構造post請求的數據
function post($body=array()){
//構造主體信息
$this->setLine('POST');
//設置content-type
$this->setHeader('Content-type:application/x-www-form-urlencoded');
//設置主體信息,比GET不一樣的地方
$this->setBody($body);
//計算content-length
$this->setHeader('Content-length:'.strlen($this->body[0]));
$this->request();
return $this->response;
}
//真正請求
function request(){
//把請求行,頭信息,實體信息 放在一個數組裏,便於拼接
$req=array_merge($this->line,$this->header,array(''),$this->body,array(''));
$req=implode(self::CRLF,$req);
fwrite($this->fh,$req);
while(!feof($this->fh)){
$this->response.=fread($this->fh,1024);
}
$this->close();//關閉連接
return $this->response;
}
//關閉連接
function close(){
fclose($this->fh);
}
}
四、測試功能,實現批量發帖功能
(一)、建立數據表
(二)、程序實現
(1)、建立liuyanben.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$link=mysql_connect('localhost','root','root');
mysql_select_db('test');
mysql_set_charset('utf8');
if(!empty($_POST)){
$sql="insert liuyanben(id,title,content) values(null,'{$_POST['title']}','{$_POST['content']}')";
mysql_query($sql);
}
$sql="select * from liuyanben";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result)){
$list[]=$row;
}
?>
<form method='post' action='liuyanben.php'>
<table border='1'>
<tr>
<td>編號</td>
<td>標題</td>
<td>內容</td>
</tr>
<?php
foreach($list as $key=>$value){
?>
<tr>
<td><?php echo $value['id'];?></td>
<td><?php echo $value['title'];?></td>
<td><?php echo $value['content'];?></td>
</tr>
<?php
}
?>
</table>
<input type='text' name='title'><br/>
<input type='text' name='content'><br/>
<input type='submit' value='提交'>
</form>
</body>
</html>
(2).實現批量發帖
set_time_limit(0);
$url='http://localhost/socket/liuyanben.php';
for($i=1;$i<100;$i++){
$str=str_shuffle('abcdefghigklmnopqrstuvwxyz');
$title=substr($str,0,5);
$content=substr($str,6,8);
$http=new Http($url);
$http->post(array('title'=>$title,'content'=>$content,'submit'=>'留言'));
echo $title.'--------'.$con.'<br/>';
usleep(2000);
}
(三)、實驗結果
查看頁面