Guzzle是一個PHP的HTTP客戶端,用來垂手可得地發送請求,並集成到咱們的WEB服務上。php
接口簡單:構建查詢語句、POST請求、分流上傳下載大文件、使用HTTP cookies、上傳JSON數據等等 。json
發送同步或異步的請求均使用相同的接口。cookie
使用PSR-7接口來請求、響應、分流,容許你使用其餘兼容的PSR-7類庫與Guzzle共同開發。app
抽象了底層的HTTP傳輸,容許你改變環境以及其餘的代碼,如:對cURL與PHP的流或socket並不是重度依賴,非阻塞事件循環。異步
中間件系統容許你建立構成客戶端行爲。socket
"require": { "guzzlehttp/guzzle": "6.2.*" }
<?php include_once dirname(__FILE__).'/vendor/autoload.php'; use \GuzzleHttp\Client; use \GuzzleHttp\Cookie\CookieJar; use \GuzzleHttp\Exception\RequestException; /** * 豆瓣租房刷留言 */ class DouBanBrush{ public $jar; public $clock = 10; const SLEEP = 1200; // 睡20 分鐘 /** * [__autoload 初始化] * @author Shaowei Pu <542684913@qq.com> * @CreateTime 2017-04-12T10:39:28+0800 * @param [type] $account [description] * @param [type] $password [description] * @return [type] [description] */ public function __construct( $account, $password ){ $this->jar = new CookieJar; try{ if( $this->login( $account, $password ) == '200'){ echo "----------【 START 】----------\n"; $this->send(); echo "----------【 END 】----------\n"; }else{ echo "登陸失敗~!"; } }catch (RequestException $e) { var_dump( $e->getRequest()); if ($e->hasResponse()) { var_dump( $e->getResponse()); } } } /** * [login 登陸] * @author Shaowei Pu <542684913> * @CreateTime 2017-04-12T10:42:16+0800 * @return [type] [description] */ public function login( $account , $password ){ // 清楚空間內cookie // $this->jar->clear(); return ( new Client([ 'cookies' => true ]) )->request( 'POST', 'https://accounts.douban.com/j/popup/login/basic', [ 'version' => 1.1 , 'cookies' => $this->jar, 'headers' => [ 'Accept' => 'application/json', 'Referer' => '登陸來源頁' ], 'form_params' => [ 'source' => 'group', 'referer' => '提交接口', 'name' => $account, 'password' => $password, 'captcha_id' => '', 'captcha_solution' => '' ] ])->getStatusCode(); } /** * [send 發送內容] * @author Shaowei Pu <542684913@qq.com> * @CreateTime 2017-04-12T10:43:17+0800 * @return [type] [description] */ public function send() { // 得到 ck $this->reload(); $ck = 'ntxB'; array_map(function( $val ) use (& $ck ){ $val['Name'] == 'ck' && $ck = $val['Value']; }, $this->jar->toArray()); // 計時器 while ( $this->clock > 0 ) { $send_content = ( new Client([ 'cookies' => true ]) )->request( 'POST', '提交接口', [ 'version' => 1.1, 'cookies' => $this->jar, 'headers' => [ 'Accept' => 'application/json', 'Referer' => '來源頁' ], 'form_params' => [ 'ck' => $ck, 'rv_comment' => '本身頂一下~!', 'start' => 0, 'submit_btn' =>'加上去' ] ])->getBody()->getContents(); echo date('Y-m-d H:i:s').' '.$this->clock."\n"; sleep( self::SLEEP ); --$this->clock; } } /** * [reload 刷新頁面] * @author Shaowei Pu <542684913@qq.com> * @CreateTime 2017-04-12T13:35:58+0800 * @return [type] [description] */ public function reload(){ (new Client([ 'cookies' => true ])) ->request('GET', '訪問頁',[ 'cookies' => $this->jar, 'headers' => [ 'Accept' => 'application/json', 'Referer' => '來源頁' ]]); } } new DouBanBrush('帳號','密碼');
驗證碼識別優化
優化效率ui
像 @娃娃脾氣 大佬致敬this