thinkphp5.1怎么實(shí)現(xiàn)多線程爬蟲

創(chuàng)建一個(gè)cli命令

php?think?make:command?Thread?thread

測(cè)試能否成功執(zhí)行

php?think?thread

安裝Guzzle類庫(kù)

文檔地址:guzzle文檔地址(https://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html)

實(shí)現(xiàn)代碼

<?php /**  * Created by.  * User: Jim  * Date: 2020/9/29  * Time: 14:31  */  namespace appcommand;  use GuzzleHttpClient; use GuzzleHttpPool; use thinkconsoleCommand; use thinkconsoleInput; use thinkconsoleOutput;  /**  * Guzzle  * Class Thread  * @package appcommand  * 文檔地址 https://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html  */  class Thread extends Command {      /**      * 請(qǐng)求的總次數(shù)      * @var int      */     protected $totalPageCount = 50;     /**      * 當(dāng)前請(qǐng)求的次數(shù)      * @var int      */     protected static $counter = 1;     /**      * 線程的數(shù)量      * @var int      */     protected $threads = 20;      protected function configure()     {         // 指令配置         $this->setName('thread'); ????????//?設(shè)置參數(shù)  ????}  ????protected?function?execute(Input?$input,?Output?$output) ????{  ????????$client?=?new?Client(); ????????$requests?=?function?($total)?use?($client)?{ ????????????foreach?(range(1,?$total)?as?$r)?{ ????????????????$uri?=?'https://apinew.juejin.im/content_api/v1/short_msg/detail'; ????????????????yield?function?()?use?($client,?$uri)?{ ????????????????????return?$client-&gt;postAsync($uri,?[ ????????????????????????'verify'?=&gt;?false, ????????????????????????'json'?=&gt;?[ ????????????????????????????'msg_id'?=&gt;?'6845185452727599118' ????????????????????????] ????????????????????]); ????????????????}; ????????????}  ????????};  ????????$pool?=?new?Pool($client,?$requests($this-&gt;totalPageCount),?[ ????????????'concurrency'?=&gt;?$this-&gt;threads, ????????????//?請(qǐng)求成功 ????????????'fulfilled'?=&gt;?function?($response,?$index)?use?($output)?{ ????????????????$res?=?$response-&gt;getBody()-&gt;getContents(); ????????????????$output-&gt;writeln($res); ????????????????$output-&gt;writeln("正在執(zhí)行第{$index}個(gè)·····"); ????????????????if?($this-&gt;checkThreadIsEnd()?==?true)?{ ????????????????????$output-&gt;writeln("------------請(qǐng)求結(jié)束---------"); ????????????????????return?false; ????????????????} ????????????}, ????????????//?請(qǐng)求失敗 ????????????'rejected'?=&gt;?function?($reason,?$index)?use?($output)?{ ????????????????$output-&gt;writeln("執(zhí)行失敗,{$reason}"); ????????????}, ????????]); ????????$promise?=?$pool-&gt;promise(); ????????$promise-&gt;wait(); ????}  ????/** ?????*?檢測(cè)任務(wù)是否結(jié)束 ?????*?@return?bool ?????*/ ????private?function?checkThreadIsEnd() ????{ ????????if?(self::$counter?totalPageCount)?{ ????????????self::$counter++; ????????????return?false; ????????}?else?{ ????????????return?true; ????????} ????}   }

執(zhí)行命令

php?think?thread

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊10 分享