Laravel Guzzle 使用踩雷及指南
今天在使用Guzzle时遇到了几个问题,在这里记录一下。
1.报错HttpFoundationFactory not found
Class 'Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory' not found
遇到这个问题是因为Guzzle的使用是需要依赖这个包的,但是我们没有,解决方法非常直接:
composer require symfony/psr-http-message-bridge
如果之前没有使用镜像,那么问题就直接解决了,但是如果使用过镜像那就出现了第二个问题
2.添加依赖包时报错Could not find package
[InvalidArgumentException]
Could not find package symfony/psr-http-message-br
idge.
Did you mean one of these?
symfony/psr-http-message-bridge
loophp/unaltered-psr-http-message-bridge-bundl
e
出现这个问题的原因是因为使用镜像,解决办法非常直接:
composer config -g --unset repos.packagist
- -
下面记录一下guzzle的基本用法:
//新建client配置base_uri,可以直接在这后面拼接比较方便
$client = new Client([
'base_uri' => 'http://127.0.0.1:8000/',
'timeout' => 2.0
]);
try {
return $client->request('GET', '/api/v4/alarms', [
'auth' => ['admin', 'public']
]);
} catch (GuzzleException $e) {
return array([
'msg' => 'error'
]);
}
阅读 67
0 条评论
得票时间