Spotify 传递变量 + 重定向 URL


Spotify pass Variables + redirect URL

对于我正在推广的机器人,我遇到了一个问题:

摘要和功能

当用户(在聊天应用程序中)编写!add spotify:track:someHash时,我正在调用一个java函数,然后调用PHP脚本。

仅供参考:我正在使用这个 git 项目。我遵循了授权内容的文档,当我在我的源代码中硬编码它们时,我可以添加我的曲目。

让我向您展示重要的片段:

if (spotifyTrack.startsWith("spotify")) {
    // this might be important, maybe need to change this?
    URL url = new URL("http://www.example.com/spot/index.php");
    URLConnection con = url.openConnection();
    con.setDoOutput(true);
    PrintStream ps = new PrintStream(con.getOutputStream());
    ps.print("track=" + spotifyTrack);
    con.getInputStream();
    ps.close();
    sendMessage.setText("Song added");
}

比方说,我的变量spotifyTrack包含这样的东西: spotify:track:06faVvKZVHivuKgL8NUMQr

所以,我调用我的索引.php

require 'vendor/autoload.php';
// I'm calling another script here, add.php, you see why down below
$session = new SpotifyWebAPI'Session('My ID','My Secret','http://www.example.com./spot/add.php');
$api = new SpotifyWebAPI'SpotifyWebAPI();
$scopes = array(
    'playlist-read-private',
    'user-read-private',
    'playlist-modify-public'
);
$authorizeUrl = $session->getAuthorizeUrl(array(
    'scope' => $scopes
));
header('Location: ' . $authorizeUrl);
die();   
// Start using the API!

这是第二个脚本:添加.php

require 'vendor/autoload.php';
$session = new SpotifyWebAPI'Session('My Id', 'MY Secret','http://www.example.com/spot/add.php');
$api = new SpotifyWebAPI'SpotifyWebAPI();
$scopes = array(
    'playlist-read-private',
    'user-read-private',
    'playlist-modify-public'
);
$authorizeUrl = $session->getAuthorizeUrl(array(
    'scope' => $scopes
));
// Request a access token using the code from Spotify
$session->requestAccessToken($_GET['code']);
$accessToken = $session->getAccessToken();
// Set the access token on the API wrapper
$api->setAccessToken($accessToken);
$track = explode(":", $_POST['track']);
// Start using the API!
$api->addUserPlaylistTracks('my user', 'my playlist id', array(
$track[2],
));

如您所知,有些东西是多余的,并不是最好的方法。但是,我被困住了,因为我不知道下一步该怎么做。您必须指定重定向 URI,所以我添加了 index.php 并添加了.php

问题是:如果我从我的java程序调用索引.php并将索引中的URL更改为index.php.php,它将以无限重定向循环结束。但是,调用脚本 add.php 将导致丢失POST-data

据我了解:我必须调用索引.php,需要重定向才能获取代码参数。(猜测OAuth相关?

我的问题:如何在重定向(使用 header())后保留 POST 数据而不会丢失我的请求?

如果您需要将数据

转发到另一个页面,则可以使用会话:

session_start();
$_SESSION['post_data'] = $_POST;

@cKarsten解决方案在我看来是头顶,因为$_POST['track']的值只需要一次,并且在重定向到的页面中。此外,数据被不必要地存储。

我宁愿通过将$_POST['track']的值作为获取参数传递给app.php来修改index.php。分别在应用程序中.php $_POST['track']必须更改为$_GET['track']

我演示它。以下不安全,但显示了我会这样做的方式。修改后的代码在上一行中用"//ADD:"标记。

require 'vendor/autoload.php';
// I'm calling another script here, add.php, you see why down below
$session = new SpotifyWebAPI'Session('My ID','My   Secret','http://www.example.com./spot/add.php');
$api = new SpotifyWebAPI'SpotifyWebAPI();
$scopes = array(
'playlist-read-private',
'user-read-private',
'playlist-modify-public'
);
$authorizeUrl = $session->getAuthorizeUrl(array(
'scope' => $scopes
));
// ADDED: if $authorizeUrl contains at least one get-paramater use "&". Otherwise "?". In this sample I am using "&".
$authorizeUrl .= "&track=".$_POST['track'];
header('Location: ' . $authorizeUrl);
die();   
// Start using the API!

不要忘记更新add.php .