guzzle cURL error 77: error set ting certificate verify locations

在sf 看到这篇文章 https://segmentfault.com/a/119000000514206... 自己测试了下,报错
curl: (60) SSL certificate : unable to get local issuer certificate
于是 使用 $client->setDefaultOption('verify', false); 类似 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);,然后提示

$ php artisan guzzle

In Client.php line 128:

  Type error: Argument 3 passed to GuzzleHttp\Client::request() must be of th
  e type array, boolean given

于是去掉 $client->setDefaultOption('verify', false); 继续报错 cURL error 77: error set ting certificate verify locations
在 sf 上搜到答案 https://stackoverflow.com/questions/246116...

下载文件 https://curl.haxx.se/ca/cacert.pem 加入 php.ini curl.cainfo="/path/to/downloaded/cacert.pem" 注意使用绝对路径

先创建一个 command 见文档 Artisan 命令行 具体代码

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request as guzzleRequest;
use GuzzleHttp\Promise;
use GuzzleHttp\Pool;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
class Guzzle extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'guzzle';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';
    private $totalPageCount;
    private $counter        = 1;
    private $concurrency    = 7;  // 同时并发抓取
    private $users = ['CycloneAxe', 'appleboy', 'Aufree', 'lifesign','overtrue', 'zhengjinghua', 'NauxLiu'];
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $client = new Client();
        // $client->setDefaultOption('verify', false);
        $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());
                dump("请求第 $index 个请求,用户 " . $this->users[$index] . " 的 Github ID 为:" .$res->id);

                $this->countedAndCheckEnded();
            },
            'rejected' => function ($reason, $index){

               dump("rejected reason: " . $reason );
                $this->countedAndCheckEnded();
            },
        ]);

        // 开始发送请求
        $promise = $pool->promise();
        $promise->wait();
    }

    public function countedAndCheckEnded()
    {
        if ($this->counter < $this->totalPageCount){
            $this->counter++;
            return;
        }
        dump("done");
    }
}

推荐一个 PHP 网络请求插件 Guzzle
Beanbun: 简单开放的 PHP 爬虫框架

本作品采用《CC 协议》,转载必须注明作者和本文链接
sushengbuhuo
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!