Guzzle Laravel 中多个 HTTP 请求的问题
problem with multiple HTTP requests in Laravel with Guzzle
我使用的是 Guzzle 版本 6.3.3。我想从外部 API 发出多个 HTTP 请求。下面显示的代码非常适合我。这只是一个请求。
public function getAllTeams()
{
$client = new Client();
$uri = 'https://api.football-data.org/v2/competitions/2003/teams';
$header = ['headers' => ['X-Auth-Token' => 'MyKey']];
$res = $client->get($uri, $header);
$data = json_decode($res->getBody()->getContents(), true);
return $data['teams'];
}
但现在我想一次发出多个请求。在 Guzzle 的文档中我找到了如何做,但它仍然不能正常工作。这是我尝试使用的代码。
$header = ['headers' => ['X-Auth-Token' => 'MyKey']];
$client = new Client(['debug' => true]);
$res = $client->send(array(
$client->get('https://api.football-data.org/v2/teams/666', $header),
$client->get('https://api.football-data.org/v2/teams/1920', $header),
$client->get('https://api.football-data.org/v2/teams/6806', $header)
));
$data = json_decode($res->getBody()->getContents(), true);
return $data;
我收到错误:
Argument 1 passed to GuzzleHttp\Client::send() must implement interface Psr\Http\Message\RequestInterface, array given called in TeamsController.
如果我删除每个 URI 后的 $header
,则会出现此错误:
resulted in a '403 Forbidden' response: {"message": "The resource you are looking for is restricted. Please pass a valid API token and check your subscription fo (truncated...)
我尝试了几种使用 API 键设置 X-Auth-Token
的方法。但我仍然遇到错误,而且我不知道使用 Guzzle 设置错误的其他方法。
希望有人能帮帮我:)
Guzzle 6 使用与 Guzzle 3 不同的方法,因此您应该使用如下内容:
use function GuzzleHttp\Promise\all;
$header = ['headers' => ['X-Auth-Token' => 'MyKey']];
$client = new Client(['debug' => true]);
$responses = all([
$client->getAsync('https://api.football-data.org/v2/teams/666', $header),
$client->getAsync('https://api.football-data.org/v2/teams/1920', $header),
$client->getAsync('https://api.football-data.org/v2/teams/6806', $header)
])->wait();
$data = [];
foreach ($responses as $i => $res) {
$data[$i] = json_decode($res->getBody()->getContents(), true);
}
return $data;
查看同一主题 (, ) 的不同问题以查看更多用法示例。
我使用的是 Guzzle 版本 6.3.3。我想从外部 API 发出多个 HTTP 请求。下面显示的代码非常适合我。这只是一个请求。
public function getAllTeams()
{
$client = new Client();
$uri = 'https://api.football-data.org/v2/competitions/2003/teams';
$header = ['headers' => ['X-Auth-Token' => 'MyKey']];
$res = $client->get($uri, $header);
$data = json_decode($res->getBody()->getContents(), true);
return $data['teams'];
}
但现在我想一次发出多个请求。在 Guzzle 的文档中我找到了如何做,但它仍然不能正常工作。这是我尝试使用的代码。
$header = ['headers' => ['X-Auth-Token' => 'MyKey']];
$client = new Client(['debug' => true]);
$res = $client->send(array(
$client->get('https://api.football-data.org/v2/teams/666', $header),
$client->get('https://api.football-data.org/v2/teams/1920', $header),
$client->get('https://api.football-data.org/v2/teams/6806', $header)
));
$data = json_decode($res->getBody()->getContents(), true);
return $data;
我收到错误:
Argument 1 passed to GuzzleHttp\Client::send() must implement interface Psr\Http\Message\RequestInterface, array given called in TeamsController.
如果我删除每个 URI 后的 $header
,则会出现此错误:
resulted in a '403 Forbidden' response: {"message": "The resource you are looking for is restricted. Please pass a valid API token and check your subscription fo (truncated...)
我尝试了几种使用 API 键设置 X-Auth-Token
的方法。但我仍然遇到错误,而且我不知道使用 Guzzle 设置错误的其他方法。
希望有人能帮帮我:)
Guzzle 6 使用与 Guzzle 3 不同的方法,因此您应该使用如下内容:
use function GuzzleHttp\Promise\all;
$header = ['headers' => ['X-Auth-Token' => 'MyKey']];
$client = new Client(['debug' => true]);
$responses = all([
$client->getAsync('https://api.football-data.org/v2/teams/666', $header),
$client->getAsync('https://api.football-data.org/v2/teams/1920', $header),
$client->getAsync('https://api.football-data.org/v2/teams/6806', $header)
])->wait();
$data = [];
foreach ($responses as $i => $res) {
$data[$i] = json_decode($res->getBody()->getContents(), true);
}
return $data;
查看同一主题 (