ここに私の問題があります:
Spotifyはユーザーの保存したすべてのトラックを返しません。返送するトラックの数に制限があります - 50(ここではAPIです)。guzzで複数のリクエスト
すべてのユーザーの保存されたトラックを返す解決策が見つかりました(do-whileループ)。それはたくさんのリクエストをします(私の場合は17回〜814トラックでした)しかし、私のページは6秒から8秒まで読み込まれます。
私は約Concurrent requestsを読んで、私は私の場合には要求の既知量ではありませんので、私の状況ではこれと非同期要求を使用する方法がわかりません。ループは、返されるトラック(アイテム)の数が0の場合にのみ終了します。私の問題を助けてくれますか?
<?php
namespace AppBundle\Service;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class SpotifyRequester
{
protected $client;
protected $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
$this->client = new Client();
}
public function getSavedTracks()
{
$token = $this->getToken(); // true token
$offset = 0;
do {
$response = $this->client->request('GET',
'https://api.spotify.com/v1/me/tracks?limit=50&offset=' . $offset, [
'headers' => [
'Authorization:' => 'Bearer ' . $token,
'Accept:' => 'application/json',
'Content-Type:' => 'application/json',
]
]);
// Response from current request
$content = json_decode($response->getBody()->getContents(), true);
$offset += count($content['items']);
}
while (count($content['items']) != 0);
// Count of tracks
return $offset;
}
}
これは、jsonの合計カウントを返します(1つのレコードに制限されている可能性があります)1つの最初の要求です(トラックカウントを公開するためのSpotifyの終了ポイントはありません)。これから、合計を表示するのに必要なページ数と合計に基づいて必要なリクエスト数を計算することができます。 –