Skip to content

Instantly share code, notes, and snippets.

@fjallstrom
Last active March 6, 2017 19:59
Embed
What would you like to do?
<?php
# fetch and cache artist id by doing a search
$spotresults = @json_decode(shell_exec('curl "https://api.spotify.com/v1/search?q='.$artistNameFromScrape.'&type=artist" -H "Accept: application/json"'));
$artistmeta = json_encode($spotresults->artists->items[0]);
$artistid = (string)$spotresults->artists->items[0]->id;
$artistname = (string)$spotresults->artists->items[0]->name;
$result = $db->prepare("INSERT INTO artists(artist, url, parent_artist, json) VALUES(?,?,?,?)");
$result->execute(array($artistname, $val, 0, $artistmeta));
$insert_id = $db->lastInsertId();
# fetch and cache related artists
if($artistid){
$spotresults = @json_decode(shell_exec('curl "https://api.spotify.com/v1/artists/'.$artistid.'/related-artists" -H Accept: application/json'));
if($spotresults->artists){
foreach($spotresults->artists as $currartist){
$result = $db->prepare("INSERT INTO artists(artist, url, parent_artist, json) VALUES(?,?,?,?)");
$result->execute(array($currartist->name, '', $insert_id, json_encode($currartist)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment