在之前get請求的類上繼續,更改了 construct,request,setHeader方法
寫setBody,post,close方法
實例化,參數 你要提交帖子內容的網址(是需要收到post內容的網址)
調用post方法 ,參數 你要提交的帖子需要的參數組成的數組詳情看下方代碼
<?php
/*php+socket編程 發送http
http類 操作案例get--獲取網頁 post批量發帖 */
// 接口
interface Proto{
//連接
function conn($url);
//發送get
function get();
//發送post*繼承後 參數必須一致
function post($body);
//關閉
function close();
}
//* 繼承的東西全要繼承不能不寫
class Http implements Proto{
protected $url=null;
protected $line=array();
protected $header=array();
protected $body=array();
protected $fh=NULL;
protected $version="HTTP/1.1";
protected $errno= -1;
protected $errstr='';
protected $resp='';//響應
const CRLF="\r\n";
//* 一開始就需要的變量 填在construct裏
public function __construct($url){
$this->conn($url);
//$this->setHeader();//本來寫在get方法,但所有請求方法header相同
$this->setHeader("Host:".$this->url['host']);
}
//連接,得到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);
// resource fsockopen ( string $hostname [, int $port = -1 [,
// int &$errno [, string &$errstr [, float $timeout
// = ini_get("default_socket_timeout") ]]]] )
// 初始化一個套接字連接到指定主機(hostname)。也就是打開了通道可以讀取寫入進這個網站
}
//發送get *怎麼get寫在$line前面 setline方法設一個變量,在不同請求方法裏用
public function get(){
$this->setLine('GET');
$this->request();
return $this->resp;
}
//發送post
public function post($body){
// print_r($body);exit;
$this->setLine('POST');
// 設置content-type
$this->setHeader("Content-type:application/x-www-form-urlencoded");
$this->setBody($body);
//設置content-length
$this->setHeader("Content-length: ".strlen($this->body[0]));
$this->request();
// return $this->resp;
}
// 請求拼接發送 *怎麼整合 請求數組變成str
public function request(){
$req=array_merge($this->line,$this->header,array(''),$this->body,array(''));
$req=implode(self::CRLF, $req);//crlf換行
echo $req;exit;
// 寫入請求*
fwrite($this->fh, $req);
while (!feof($this->fh)) {
$this->resp.=fread($this->fh, 1024);
}
// 關閉連接
$this->close();
}
//關閉
public function close(){
//
fclose($this->fh);
}
// 拼接 請求行1
protected function setLine($method){
$this->line[]=$method." ".$this->url['path']." ".$this->version;
}
//請求頭部2,*因爲post有type和length,所以改!
protected function setHeader($headerline){
// $this->header[]="Host:".$this->url['host'];
$this->header[]=$headerline;
}
//請求主體部分3,收到數組
protected function setBody($body){
// 把數組直接變成&連接的post形式
$this->body[]=http_build_query($body);
}
}
// 下面兩行,是get方法,發送請求獲取網頁
// $http=new Http('http://news.qq.com/a/20160107/019440.htm');
// echo $http->get();
// url====[scheme] => http [host] => news.163.com [path] => /16/0107/06/BCN66S6S00014AED.html
//line === Array ( [0] => GET /16/0107/06/BCN66S6S00014AED.html HTTP/1.1 )
//header === Array ( [0] => Host:news.163.com )
// $http->request();
// 下面兩行是post ,發帖 #如果需要循環就循環下面兩行,就str_shuffle一下,然後substr截取前幾個字符給下面的參數
$http=new Http('http://localhost/~xxxxx/project/tieba/p_action.php');
$http->post(array('username'=>'lisi','title'=>'good idea','content'=>'yep'));
// ,'submit'=>'發佈新帖'
// 一直空白,檢查提交的http://localhost/~xxxxx/project/tieba/p_action.php 這個
// 路徑對不對,之前是index頁就是顯示頁,其實應該是參數提交到的那個操作頁
?>
卡過兩次,一次是post里加了參數,但是上面接口沒有加,一直沒反應,而且看network報304
還有一次就是發現寫的網址不是接收post數據的網址(因爲我用的是自己的貼吧項目),也是一直空白,network200OK