简体   繁体   中英

Curl GET Request to the spotify Authorization API

I need some help with my Curl GET Request to the Spotify API. The API has three different ways/endpoints to get an authorization.

I read some articles, to find the correct syntax to send the request. But i always get an error. If i post the url into my brwoser it works perfectly, also with the redirect uri. But it doesnt work with the Curl GET Request.

It sounds stupid, but i spend the last three days with this Problem.

My code:

<?php
$client_id = 'myClientID';
$redirect_url = 'http://mywebsite/first/page.php';
$scope = 'user-read-private%20user-read-email';

$data = array(
    'client_id' => $client_id,
    'response_type' => 'code',
    'redirect_uri' => $redirect_url,
    'state' => stateHash(), // Create a random hash
    'scope' => $scope,
    'show_dialog' => 'true'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://accounts.spotify.com/authorize' . http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
$result=curl_exec($ch);
echo $result;

The error from the API Shows me this: 在此处输入图片说明

or i got an "1" as response.

I hope that i get some nice tips :)

There is a package for Spotify web API try using that

composer require jwilsson/spotify-web-api-php

Before using the Spotify Web API, you'll need to create an app at Spotify's developer site.

Simple example displaying a user's profile:

require 'vendor/autoload.php';

$session = new SpotifyWebAPI\Session(
    'CLIENT_ID',
    'CLIENT_SECRET',
    'REDIRECT_URI'
);

$api = new SpotifyWebAPI\SpotifyWebAPI();

if (isset($_GET['code'])) {
    $session->requestAccessToken($_GET['code']);
    $api->setAccessToken($session->getAccessToken());

    print_r($api->me());
} else {
    $options = [
        'scope' => [
            'user-read-email',
        ],
    ];

    header('Location: ' . $session->getAuthorizeUrl($options));
    die();
}

For more instructions and examples, check out the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM