'How to move on to more complex API with php
My goal is to use a Spotify API and be able to search for artist and display artist info. This is the Spotify API i am using https://rapidapi.com/Glavier/api/spotify23/
i have succeed to use an quote API and display the quote API with a few lines of code
//Quote API
$arr = json_decode($response, true);
//print array
echo $arr['content'];
the quote API i have tried out is https://rapidapi.com/martin.svoboda/api/quotes15/ when i compare the schemas from the two examples i notice that the Spotify is a lot bigger and more complex. But i think the first step to understand how to use this data is to be able to print out something specific like i did with the quote API
<?php //Spotify API
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://spotify23.p.rapidapi.com/search/?q=tash%20sultana&type=artist&offset=0&limit=10&numberOfTopResults=5",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"X-RapidAPI-Host: spotify23.p.rapidapi.com",
"X-RapidAPI-Key: 23f81fea24mshad5307d5e95069dp142dfdjsn5c912989d838"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$arr = json_decode($response, true); //line 29
var_dump($arr);
}
This gives me ALOT of data and i am hoping that there is a way to figure out the structure of it. is there a better way to print something this large?
Solution 1:[1]
Try my example -
<?php
// Make your API Call
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://spotify23.p.rapidapi.com/search/?q=tash%2520sultana&type=artist&offset=0&limit=10&numberOfTopResults=5',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'X-RapidAPI-Host: spotify23.p.rapidapi.com',
'X-RapidAPI-Key: 23f81fea24mshad5307d5e95069dp142dfdjsn5c912989d838'
),
));
$response = curl_exec($curl);
curl_close($curl);
// Decode your response
$json = json_decode($response, true);
// Here I am accessing $json["episodes"], but you also have singers, albums, etc;
echo "I see " . $json["episodes"]["totalCount"] . " episodes. Let's loop through each data element..<hr><br>";
foreach($json["episodes"]["items"] as $episode){
// Data is what holds all the information for either episode, song, album, singer, etc
$item = $episode["data"];
// I am dumping the data here so you can see the structure, here you can use anything inside of $item;
// For example I can display every episode name by doing: echo $item["name"]; you need to look at $data and identify what you want to use
var_dump($item);
echo "<hr>";
}
?>
I just wrote it looking at your question - there's nothing complex really, just know this JSON returns a bunch of items, inside of them a bunch of data attributes that have many attributes about the parent;
So essentially this response is a nested response, with arrays, in arrays, inside of arrays - it's not as complex as you think, just longer than a payload with a single array; you can't work with an API without identifying its structure right - what you want to use, where to use it, what to do if it doesn't exist, etc; so all I did here was show you what you already know - I identified the arrays I consumed and that's pretty much it;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DharmanBot |
