应用Guzzle刷豆瓣热评激发的遐想

Guzzle

Guzzle是一个PHP的HTTP客户端,用来易如反掌地发送要求,并集成到我们的WEB效劳上。

  • 接口简朴:构建查询语句、POST要求、分流上传下载大文件、运用HTTP cookies、上传JSON数据等等 。

  • 发送同步或异步的要求均运用雷同的接口。

  • 运用PSR-7接口来要求、相应、分流,许可你运用其他兼容的PSR-7类库与Guzzle共同开发。

  • 笼统了底层的HTTP传输,许可你转变环境以及其他的代码,如:对cURL与PHP的流或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('账号','暗码');

还没做的事

  • 验证码辨认

  • 优化效力

致敬

像 @娃娃性情 大佬致敬

    原文作者:无敌小笼包
    原文地址: https://segmentfault.com/a/1190000009037770
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞