Laravel中用GuzzleHttp



今天項目中用到GuzzleHttp,開始不知道怎么用,其實還是很簡單的。 
直接在項目根目錄,輸入以下命令

composer require guzzlehttp/guzzle

    
    
    
            
  • 1

等下載安裝好,在vendor文件夾下,有一個guzzle目錄,此文件夾就是guzzlehttp的package了。 
如何使用,可以參考官方文檔http://docs.guzzlephp.org/en/latest/ 
下面這段代碼就是官網文檔中的一段

$client = new GuzzleHttp\Client(); $res = $client->request('GET', 'https://api.github.com/user', [ 'auth' => ['user', 'pass'] ]); echo $res->getStatusCode(); // "200" echo $res->getHeader('content-type'); // 'application/json; charset=utf8' echo $res->getBody(); // {"type":"User"...' // Send an asynchronous request. $request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org'); $promise = $client->sendAsync($request)->then(function ($response) { echo 'I completed! ' . $response->getBody(); }); $promise->wait();

    
    
    
            
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

我在項目中,已經使用了form表單post,異步請求等等。 
這篇文章還是挺有意思的《Laravel 下使用 Guzzle 編寫多線程爬蟲實戰》,代碼啥都有,雖然是個小玩意,但能學到很多東西。 
比如:

  1. 在Laravel中如何創建命令
  2. 怎么用多線程

貼一下代碼

<?php namespace App\Console\Commands; use GuzzleHttp\Client; use GuzzleHttp\Pool; use GuzzleHttp\Psr7\Request; use GuzzleHttp\Exception\ClientException; use Illuminate\Console\Command; class MultithreadingRequest extends Command { private $totalPageCount; private $counter = 1; private $concurrency = 7; // 同時並發抓取 private $users = ['CycloneAxe', 'appleboy', 'Aufree', 'lifesign', 'overtrue', 'zhengjinghua', 'NauxLiu']; protected $signature = 'test:multithreading-request'; protected $description = 'Command description'; public function __construct() { parent::__construct(); } public function handle() { $this->totalPageCount = count($this->users); $client = new Client(); $requests = function ($total) use ($client) { foreach ($this->users as $key => $user) { $uri = 'https://api.github.com/users/' . $user; yield function() use ($client, $uri) { return $client->getAsync($uri); }; } }; $pool = new Pool($client, $requests($this->totalPageCount), [ 'concurrency' => $this->concurrency, 'fulfilled' => function ($response, $index){ $res = json_decode($response->getBody()->getContents()); $this->info("請求第 $index 個請求,用戶 " . $this->users[$index] . " 的 Github ID 為:" .$res->id); $this->countedAndCheckEnded(); }, 'rejected' => function ($reason, $index){ $this->error("rejected" ); $this->error("rejected reason: " . $reason ); $this->countedAndCheckEnded(); }, ]); // 開始發送請求 $promise = $pool->promise(); $promise->wait(); } public function countedAndCheckEnded() { if ($this->counter < $this->totalPageCount){ $this->counter++; return; } $this->info("請求結束!"); } }

    
    
    
            
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

運行結果如下:

$ php artisan test:multithreading-request 請求第 5 個請求,用戶 zhengjinghua 的 Github ID 為:3413430 請求第 6 個請求,用戶 NauxLiuGithub ID 為:9570112 請求第 0 個請求,用戶 CycloneAxeGithub ID 為:6268176 請求第 1 個請求,用戶 appleboy 的 Github ID 為:21979 請求第 2 個請求,用戶 AufreeGithub ID 為:5310542 請求第 3 個請求,用戶 lifesign 的 Github ID 為:2189610 請求第 4 個請求,用戶 overtrue 的 Github ID 為:1472352 請求結束!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM