Requires a valid access token if market=from_token is used.
https://developer.spotify.com/web-api/search-item/
public search ( string $query, string | array $type, array | object $options = [] ) : array | object | ||
$query | string | The term to search for. |
$type | string | array | The type of item to search for. |
$options | array | object | Optional. Options for the search. - string market Optional. Limit the results to items that are playable in this market, for example SE. - int limit Optional. Limit the number of items. - int offset Optional. Number of items to skip. |
리턴 | array | object | The search results. Type is controlled by SpotifyWebAPI::setReturnAssoc(). |
/** * Search for a track and build the response objects * * @param string $string * * @return Track */ public function findTrack(string $string) { $result = $this->spotifyWebApi->search($string, 'track'); if (count($result->tracks->items) === 0) { throw NoTracksFoundException::emptyResult(); } $firstItem = $result->tracks->items[0]; $images = $this->valueObjectBuilder->buildImages($firstItem->album->images); $album = $this->valueObjectBuilder->buildAlbumSimplified($firstItem->album, $images); $artists = $this->valueObjectBuilder->buildArtistsSimplified($firstItem->artists); $track = $this->valueObjectBuilder->buildTrack($firstItem, $album, $artists); return $track; }
<?php use SpotifyWebAPI\SpotifyWebAPI; use SpotifyWebApiExtensions\GuzzleClientFactory; use SpotifyWebApiExtensions\GuzzleRequestAdapter; use Doctrine\Common\Cache\FilesystemCache; require_once './vendor/autoload.php'; $guzzleAdapter = new GuzzleRequestAdapter(GuzzleClientFactory::create(new FilesystemCache(__DIR__ . '/cache'))); $api = new SpotifyWebAPI($guzzleAdapter); print_r($api->search('Nothing else matters', ['track'], ['market' => 'DE']));