guzzle 6.2 在 PHP 7.2 下及升级到 6.3 遇到的问题


作者: 我不是鱼 (2018-10-12 14:43) 分类: PHP   标签: guzzle

程序在 PHP 7.1 运行没有任何问题,但升级到 PHP 7.2 后,出现这个错误

[ErrorException]                                                            
  count(): Parameter must be an array or an object that implements Countable  
                                                                              
Exception trace:
 () at vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:67
 Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at n/a:n/a
 count() at vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:67
 GuzzleHttp\Handler\CurlFactory->release() at vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:107
 GuzzleHttp\Handler\CurlFactory::finish() at vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php:43
 GuzzleHttp\Handler\CurlHandler->__invoke() at vendor/guzzlehttp/guzzle/src/Handler/Proxy.php:28
 GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}() at vendor/guzzlehttp/guzzle/src/Handler/Proxy.php:51
 GuzzleHttp\Handler\Proxy::GuzzleHttp\Handler\{closure}() at vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php:42
 GuzzleHttp\PrepareBodyMiddleware->__invoke() at vendor/guzzlehttp/guzzle/src/Middleware.php:30
 GuzzleHttp\Middleware::GuzzleHttp\{closure}() at vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php:68
 GuzzleHttp\RedirectMiddleware->__invoke() at vendor/guzzlehttp/guzzle/src/Middleware.php:59
 GuzzleHttp\Middleware::GuzzleHttp\{closure}() at vendor/guzzlehttp/guzzle/src/HandlerStack.php:67
 GuzzleHttp\HandlerStack->__invoke() at vendor/guzzlehttp/guzzle/src/Client.php:275
 GuzzleHttp\Client->transfer() at vendor/guzzlehttp/guzzle/src/Client.php:123
 GuzzleHttp\Client->requestAsync() at vendor/guzzlehttp/guzzle/src/Client.php:129
 GuzzleHttp\Client->request() at app/Services/Collect/CollectBase.php:46
 App\Services\Collect\CollectBase->request() at app/Services/Collect/Segmentfault.php:20
 App\Services\Collect\Segmentfault->collect() at app/Console/Commands/Collect.php:65
 App\Console\Commands\Collect->handle() at n/a:n/a
 call_user_func_array() at vendor/laravel/framework/src/Illuminate/Container/Container.php:508
 Illuminate\Container\Container->call() at vendor/laravel/framework/src/Illuminate/Console/Command.php:169
 Illuminate\Console\Command->execute() at vendor/symfony/console/Command/Command.php:254
 Symfony\Component\Console\Command\Command->run() at vendor/laravel/framework/src/Illuminate/Console/Command.php:155
 Illuminate\Console\Command->run() at vendor/symfony/console/Application.php:821
 Symfony\Component\Console\Application->doRunCommand() at vendor/symfony/console/Application.php:187
 Symfony\Component\Console\Application->doRun() at vendor/symfony/console/Application.php:118
 Symfony\Component\Console\Application->run() at vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:121
 Illuminate\Foundation\Console\Kernel->handle() at artisan:35

 

guzzle 版本是 6.2,查了一下,应该是 guzzle 自身的bug,并在 6.3 中已经修复:https://github.com/guzzle/guzzle/issues/1973

根本问题在于 count 函数在 PHP 7.2 版本之后,如果参数不是 array 或者 countable 的,都会产生这个 warning:

Warning: count(): Parameter must be an array or an object that implements Countable

 

这里 guzzle 其实就是用 null 作为 count 函数参数,在 PHP 7.2 之前的版本包括 PHP 5 都返回0,但在 PHP 7.2 中除了返回0还产生 warning,导致被抛出。

guzzle 说在 6.3 的版本已经修复,那就尝试升级到 6.3。将 composer.json 中的

"guzzlehttp/guzzle": "6.2.*"

修改成

"guzzlehttp/guzzle": "6.3.*"

然后

$ composer update guzzlehttp/guzzle
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: don't install guzzlehttp/guzzle 6.3.3
    - Conclusion: don't install guzzlehttp/guzzle 6.3.2
    - Conclusion: don't install guzzlehttp/guzzle 6.3.1
    - Conclusion: remove guzzlehttp/psr7 1.3.1
    - Installation request for guzzlehttp/guzzle 6.3.* -> satisfiable by guzzlehttp/guzzle[6.3.0, 6.3.1, 6.3.2, 6.3.3].
    - Conclusion: don't install guzzlehttp/psr7 1.3.1
    - guzzlehttp/guzzle 6.3.0 requires guzzlehttp/psr7 ^1.4 -> satisfiable by guzzlehttp/psr7[1.4.0, 1.4.1, 1.4.2].
    - Can only install one of: guzzlehttp/psr7[1.4.0, 1.3.1].
    - Can only install one of: guzzlehttp/psr7[1.4.1, 1.3.1].
    - Can only install one of: guzzlehttp/psr7[1.4.2, 1.3.1].
    - Installation request for guzzlehttp/psr7 (locked at 1.3.1) -> satisfiable by guzzlehttp/psr7[1.3.1].

并不顺利,packages之间有冲突。问题是安装 guzzle 6.2 的时候, guzzlehttp/psr7 1.3.1 作为依赖包被安装进来,而 guzzle 6.3 又依赖于 guzzlehttp/psr7 1.4 。由于这里只更新 guzzle 6.3 导致冲突,解决办法是要同时将 guzzlehttp/psr7 更新到 1.4。在 composer.json 中加入

"guzzlehttp/psr7": "1.4.*",

然后同时更新 guzzlehttp/guzzle 和 guzzlehttp/psr7

$ composer update guzzlehttp/guzzle guzzlehttp/psr7
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing guzzlehttp/psr7 (1.3.1)
  - Installing guzzlehttp/psr7 (1.4.2)
    Downloading: 100%         

  - Removing guzzlehttp/guzzle (6.2.2)
  - Installing guzzlehttp/guzzle (6.3.3)
    Downloading: 100%         

Writing lock file
Generating autoload files
Generating optimized class loader
The compiled class file has been removed.

更新成功。再执行程序,发现 guzzle count 的问题也解决了。

前一篇: 从 PHP 7 升级到 PHP 7.2
后一篇: Laravel 结合数据库做PHP单元测试,并且 migrate 和 seed 只做一次


添加评论

昵称:


验证码