PHp + socket編程練習——部落格園回複

來源:互聯網
上載者:User

標籤:des   blog   http   io   os   ar   for   strong   sp   

因為也是學習的路上,先畫出思路,代碼和看別人的學習的,最後是根據的思路完成的,可能比較簡單,後期在去完善和更改

<?php/** * php+socket 編程 發送HTTP請求 * @version  c.php * 類比部落格園評論 */interface Proto{// 串連urlpublic function conn($url);// 發送get請求public function get();// 發送post請求public function post($body);//關閉串連    public function close();}class Http implements Proto{protected $response = ‘‘;protected $fh=null;protected $errno = -1;protected $errorstr = ‘‘;protected $line = array();protected $header = array();protected $body = array();public $url = 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‘] .  ‘ ‘ . ‘HTTP/1.1‘;}// 頭資訊public function setHeader($headerline){$this->header[] = $headerline;}// 主體資訊protected function setBody($body){                $this->body[] = http_build_query($body);}// 串連urlpublic 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->errorstr,3);}// 構建get請求public function get(){$this->setLine(‘GET‘);$this->request();return $this->response;}// 發送post請求public function post($bo){$this->setLine(‘POST‘);                // content-type                $this->setHeader("Content-type: application/x-www-form-urlencoded");                $this->setBody($bo);                // content-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);die;$req = implode(PHP_EOL,$req); // PHP_EOL 換行//echo $req;die;fwrite($this->fh,$req);while(!feof($this->fh)){$this->response .= fread($this->fh,1024);}$this->close();}public function close(){            fclose($this->fh);}}

 上面是簡單的類

<?phprequire("./c.php"); // 引入上面的類$url = "http://www.cnblogs.com/mvc/PostComment/Add.aspx";$http = new Http($url);$http->setHeader("Cookie:xxxx");$http->setHeader("Referer: http://www.cnblogs.com/geek12/p/4024793.html");$http->setHeader("User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0");$msg =array("blogApp"=>"geek12","body"=>"來自robot","parentCommentId"=>0,"postId"=>4024793);$http->post($msg);

 發送成功的

在寫的過程中遇到的幾個問題,根據請求的url 不一樣,要求標頭的拼接也需要改變,這裡請求的url是 http://www.cnblogs.com/mvc/PostComment/Add.aspx  靜態串連,所有path 直接拿到後面的,如果是動態就需要修改了,所以本方法只適合靜態串連,動態需要修改要求標頭

不是所有的網站都可以類比,雖然沒有驗證碼,我在VeryCd 網站,怎麼類比都沒有成功,最後只好放棄了

我是菜鳥,請不要抨擊,待續。。。。

PHp + socket編程練習——部落格園回複

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.