1. I am working with PHP and CURL to connect to the Spotify API.
2. I don't want to use third party api for that purpose. I want to understand how exactly it works and what I am doing wrong or what I need to do to achieve my goal.
I am trying to understand why I am not getting the same result with these operations (accessing the Spotify website with my uri+parameters or redirecting to the Spotify site from my script and through CURL). Apparently, the Spotify site has some security mechanism that prevents me from getting the expected response and that is what I want to know.
Currently, I am working on my own simple code to get song metadata with the Spotify API. This is supposed to work, as I've followed the Spotify developer prompts step by step.
I know there are apis for that but my intention is to do it myself and in that way, also learn. I have not found answers on the internet, so I turn to this site one more time.
1. I have managed to configure my application in the Spotify dashboard.
2. I have managed to obtain the code for the authentication and also the access/refresh token and even, I have managed to obtain the metadata of the songs.
Everything works well. The problem is that the first step (to redirect and get the auth code via curl), it only works when I do a redirect (from my script) to the Spotify site, with the required parameters (
redirect_uri) or when I manually access the site (via the browser's address bar). I end up being redirected to my callback site and I get the token and other data, as expected.
According to the Spotify documentation, it is possible to connect and get the data with curl but if it requires a redirect uri... and that uri contains the parameter I need to continue... how am I supposed to get it with curl? Something doesn't make sense to me.
I need to understand how it works and why I am not succeeding. I will share some of my code for the authentication step:
<?php // Paramethers $pArray = array("response_type"=>'code', "client_id"=>'HERE_MY_ID', "state"=>'HERE_A_RANDOM_CODE_STORED_WITH_PHP_SESSION', "redirect_uri"=>'HERE_THE_EXACT_URI_PROVIDED_IN_SPOTIFY_DASHBOARD'); $params = http_build_query($pArray); // Spotify URL to get the code $url = "https://accounts.spotify.com/authorize/?".$params; // CURL Init/Config $c = curl_init(); curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); // Result $result = curl_exec($c); // To view the connection info $info = curl_getinfo($c); echo '<pre>'; print_r($info); echo '</pre>'; // Close CURL curl_close($c) ?>
Array ( [url] => https://accounts.spotify.com/login?continue=https%3A%2F%2Faccounts.spotify.com%2Fauthorize%2F%3Fresponse_type%3Dcode... [content_type] => text/html;charset=utf-8 [http_code] => 200 [header_size] => 4565 [request_size] => 586 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 1 [total_time] => 0.552959 [namelookup_time] => 0.016364 [connect_time] => 0.056155 [pretransfer_time] => 0.150696 [size_upload] => 0 [size_download] => 1421 [speed_download] => 2574 [speed_upload] => 0 [download_content_length] => 1421 [upload_content_length] => -1 [starttransfer_time] => 0.551039 [redirect_time] => 0.350278 [redirect_url] => [primary_ip] => x.x.x.x [certinfo] => Array ( ) [primary_port] => xxx [local_ip] => xxx.xxx.x.xx [local_port] => xxxxx [http_version] => 3 [protocol] => 2 [ssl_verifyresult] => 0 [scheme] => HTTPS [appconnect_time_us] => 150484 [connect_time_us] => 56155 [namelookup_time_us] => 16364 [pretransfer_time_us] => 150696 [redirect_time_us] => 350278 [starttransfer_time_us] => 551039 [total_time_us] => 552959 )
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.