여기 내 문제가 있습니다 :
Spotify는 모든 사용자의 저장된 트랙을 반환하지 않습니다. 반송 트랙 수에 대한 제한은 50입니다 (여기에 API).구근에 복수 요청
모든 사용자의 저장된 트랙을 반환하는 해결책을 찾았습니다 (사용 된 루프 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;
}
}
당신이 할 수있는 일은 (afaik는 Spotify가 트랙 카운트를 노출하기위한 엔드 포인트를 가지고있는 것처럼 보이지 않습니다.) 하나의 초기 요청 (아마 하나의 레코드로 제한됩니까?)입니다. 그러면 json의 총 카운트가 반환됩니다. 이것으로부터 당신은 전체를 보여줄 필요가있는 전체 페이지 수와 페이지 수에 기초하여 필요한 요청 수를 산출 할 수 있습니다. –