phpでSpotifyのAPIを使ったWebサイトを制作しています。
以下のリンクからSpotify APIのライブラリをダウンロードしたものを使っています。
認証を受けたユーザのよく聴く曲やアーティストを取得したいです。リファレンスを参照するとgetMyTop()というメソッドが定義されていたので、リファレンス通りにコードを記述したのですが、エラーが出てしまい、どうしてもその原因がわかりません。関数の呼び出し方に問題があるのでは、と疑っていましたが、ひとりで考えてみて限界になったので質問させていただきます。
php
1<?php 2//パス:C:\TECHBASE\__WatchOut__\XAMPP\htdocs\php_test\iNTO\iNTO.php 3 4require_once('../vendor/autoload.php'); 5require_once('spotify-web-api-php-master/src/SpotifyWebAPI.php'); 6require_once('profile.php'); 7 8$me = new Profile(); 9 10$me->setBio('洋楽がすき<br>'); 11 12?> 13 14<html> 15 16<head> 17 <title>iNTO</title> 18 <link rel="stylesheet" href="iNTO.css" type="text/css"> 19</head> 20 21<body> 22 <div class="upper"> 23 <h1><a href="http://localhost/php_test/iNTO/iNTO.php">Reload</a></h1> 24 <h2>iNTO</h2> 25 </div> 26 27 28 <div class="wrapper"> 29 <div class="Pic"> 30 <?php 31 //プロフィール画像取得 32 $img = file_get_contents('https://profile-images.scdn.co/images/userprofile/default/43516f5c98c8801ea2ceb67a696d51ce67e9671f'); 33 $enc_img = base64_encode($img); 34 $imginfo = getimagesize('data:application/octet-stream;base64,' . $enc_img); 35 echo '<img class="profilePic" src="data:' . $imginfo['mime'] . ';base64,'.$enc_img.'">'; 36 //// 37 ?> 38 </div> 39 40 <div class="userName"> 41 <strong><?php echo $me->getUserName() ?></strong> 42 </div> 43 44 <div class="followAndFollower"> 45 <?php 46 echo "フォロワー:" . $me->getFollow()."<br>"; 47 echo "フォロー:" .$me->getFollow(); 48 ?> 49 </div> 50 51 <div class="bio"> 52 <?php echo $me->getBio() ?> 53 </div> 54 55 <h3>公開中のプレイリスト</h3> 56 <ul> 57 <?php foreach($me->getPlaylists()->items as $playlist): ?> 58 <li><?php echo $playlist->name ?></li> 59 <?php endforeach ?> 60 </ul> 61 62 </div> 63 64</body> 65 66</html>
PHP
1<?php 2//パス:C:\TECHBASE\__WatchOut__\XAMPP\htdocs\php_test\iNTO\profile.php 3require_once('../vendor/autoload.php'); 4 5class Profile{ 6 private $userName; 7 private $profilePic; 8 private $follower = 0; 9 private $follow = 0; 10 private $Bio; 11 private $playlists; 12 private $topTracks; 13 private $topArtists; 14 15 public function __construct(){ 16 //Spotifyにユーザ認証 17 //アクセストークンの取得 18 $session = new SpotifyWebAPI\Session( 19 '{client id}', 20 '{client secret}', 21 'http://localhost/php_test/iNTO/iNTO.php' 22 ); 23 $api = new SpotifyWebAPI\SpotifyWebAPI(); 24 if (isset($_GET['code'])) { 25 $session->requestAccessToken($_GET['code']); 26 $api->setAccessToken($session->getAccessToken()); 27 }else{ 28 header('Location: ' . $session->getAuthorizeUrl(array( 29 'scope' => array( 30 'playlist-read-private', // プレイリスト取得 31 'playlist-modify-private', // プレイリスト変更 32 'user-read-private', 33 'playlist-modify' 34 ) 35 ))); 36 die(); 37 } 38 ///////////ここまでアクセストークンの処理 39 40 $profileInfo = $api->me(); //認証したユーザのプロフィール 41 42 //プロフィールから名前を取得 43 $this->userName = $profileInfo->display_name; 44 45 //プレイリスト取得 46 $this->playlists = $api->getMyPlaylists(["limit" => 2]); 47 48 //プロフィール画像をプロフィールから取得 49 $this->profilePic = $profileInfo->images[0]->url; 50 51 //トップトラックを取得 52 $this->topTracks = $api->getMyTop('tracks',['limit' => 3]); 53 54 } 55 56 public function getBio(){ 57 return $this->bio; 58 } 59 60 public function getUserName(){ 61 return $this->userName; 62 } 63 64 public function getPlaylists(){ 65 return $this->playlists; 66 } 67 68 public function getProfilePic(){ 69 return $this->profilePic; 70 } 71 72 public function getFollower(){ 73 return $this->follower; 74 } 75 76 public function getFollow(){ 77 return $this->follow; 78 } 79 80 public function getTopTracks(){ 81 return $this->topTracks; 82 } 83 84 public function setBio($bio){ 85 //$bioは文字列か確かめる 86 $this->bio = $bio; 87 } 88} 89 90?>
エラーメッセージ
Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: An unknown error occurred. in C:\TECHBASE\__WatchOut__\XAMPP\htdocs\php_test\vendor\jwilsson\spotify-web-api-php\src\Request.php:52 Stack trace: #0 C:\TECHBASE\__WatchOut__\XAMPP\htdocs\php_test\vendor\jwilsson\spotify-web-api-php\src\Request.php(242): SpotifyWebAPI\Request->parseBody(NULL, 403) #1 C:\TECHBASE\__WatchOut__\XAMPP\htdocs\php_test\vendor\jwilsson\spotify-web-api-php\src\Request.php(121): SpotifyWebAPI\Request->send('GET', 'https://api.spo...', 'limit=3&offset=...', Array) #2 C:\TECHBASE\__WatchOut__\XAMPP\htdocs\php_test\iNTO\spotify-web-api-php-master\src\SpotifyWebAPI.php(1054): SpotifyWebAPI\Request->api('GET', '/v1/me/top/trac...', Array, Array) #3 C:\TECHBASE\__WatchOut__\XAMPP\htdocs\php_test\iNTO\profile.php(52): SpotifyWebAPI\SpotifyWebAPI->getMyTop('tracks', Array) #4 C:\TECHBASE\__WatchOut__\XAMPP\htdocs\php_test\iNTO\iNTO.php(16): Profile->__construct() #5 {main} thrown in C:\TECHBASE\__WatchOut__\XAMPP\htdocs\php_test\vendor\jwilsson\spotify-web-api-php\src\Request.php on line 52
profile.phpの$this->topTracks = $api->getMyTop('tracks',['limit' => 3]);
を外した場合は正常に動作します。
考えられる原因は何でしょうか。ご回答お待ちしております。
回答2件
あなたの回答
tips
プレビュー