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
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

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