Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php header('Content-Type: application/json');
- session_start();
- // ini_set('display_errors', 1);
- // ini_set('display_startup_errors', 1);
- // error_reporting(E_ALL);
- require 'vendor/Spotify-Web-Api-Php/autoload.php';
- require 'configuration/config.php';
- $session = new SpotifyWebAPI\Session(
- '63df7e2e6d5041b188f0d4d01471d9cc',
- '010530cbd2c34a418624ffd26f3bc2a6',
- 'admin/spotify/callback.php'
- );
- // TEST LE LIEN
- function get_http_response_code($url) {
- $headers = get_headers($url);
- return substr($headers[0], 9, 3);
- }
- $reponse = $db->query("SELECT spotify_refresh FROM configuration");
- while ($donnees = $reponse->fetch())
- {
- $session->refreshAccessToken($donnees['spotify_refresh']);
- $_SESSION['accessToken'] = $session->getAccessToken();
- }
- // RECUPERE LES INFOS DE LA POCHETTE
- function search($searchTerm){
- $opts = [
- "http" => [
- "header" => "Authorization: Bearer ".$_SESSION['accessToken']
- ]
- ];
- $context = stream_context_create($opts);
- $string = $searchTerm;
- if ( preg_match("~\b&\b~",$string) ){
- $str = str_replace("&", "&", $searchTerm, $searchTerm);
- }
- $url = "https://api.spotify.com/v1/search?q=". urlencode($searchTerm)."&type=track&market=FR&limit=1";
- $result = file_get_contents($url, false, $context);
- //print_r($result);
- if($result !== false){
- //print_r($result);
- //echo $result['tracks']['items'][0]['albums']['images'][0]['url'];
- return json_decode($result, true);
- }
- return false;
- }
- function propre($string) {
- $string = preg_replace('#\((.+)\)#U', '', $string);
- $string = preg_replace('#\[(.+)\]#U', '', $string);
- $string = preg_replace('#\FT(.+)\-#U', '', $string);
- $string = str_replace(';', ' ', $string);
- $string = str_replace('_', ' ', $string);
- $string = htmlspecialchars($string);
- return $string;
- }
- function limitString($chaine) {
- $max = 25;
- if (strlen($chaine) >= $max) {
- $chaine = substr($chaine, 0, $max);
- $espace = strrpos($chaine, " ");
- $chaine = substr($chaine, 0, $espace)."...";
- }
- return $chaine;
- }
- if(!empty($_GET['url'])):
- $url = $_GET['url'];
- if($_GET['type'] == "/stream"): // SHOUTCAST
- if (!$data = file_get_contents($url.'/stats?sid=1&json=1')) {
- $error = error_get_last();
- echo "HTTP request failed. Error was: " . $error['message'];
- }
- $shoutcast = json_decode($source);
- $track = propre($shoutcast->{'songtitle'});
- elseif($_GET['type'] == "/live"): // ICECAST
- $source = file_get_contents($url.'/status-json.xsl');
- $icecast = json_decode($source);
- if (!$data = file_get_contents($url.'/status-json.xsl')) {
- $error = error_get_last();
- echo "HTTP request failed. Error was: " . $error['message'];
- }
- $track = propre($icecast->{'icestats'}->{'source'}->{'title'});
- else:
- $track = propre($icecast->{'icestats'}->{'source'}[0]->{'title'});
- endif;
- endif;
- $recherche = search($track);
- $image = $pochette_img;
- //print_r($recherche['tracks']);
- //$image=$recherche['results']['tracks']['items'][0]['album']['images'][0]['url'];
- if(isset($recherche['tracks']['items'][0]['album']['images'][0]['url'])){
- $image=$recherche['tracks']['items'][0]['album']['images'][0]['url'];
- }
- $decoupe = explode(" - ", $track);
- if(!$decoupe[0] or !$decoupe[1]):
- $artist = $track;
- endif;
- $artist = limitString($decoupe[0]);
- $titre = limitString($decoupe[1]);
- $tableau = ['track'=>$track, 'title'=>$titre, 'artist'=>$artist, 'picture'=>$image];
- $tableau_json = json_encode($tableau);
- echo($tableau_json);
- endif;
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement