未设置PHP json_解码变量

未设置PHP json_解码变量,php,json,curl,sails.js,Php,Json,Curl,Sails.js,更新: 老实说,我放弃了,最后在sails.js上做了,我会把这个问题留给别人,以防有人看到我哪里做错了 想知道是否有人能帮我解决一个我似乎无法解决的奇怪问题 我有一个oAuth脚本,它将json数组的输出返回到我的android应用程序 我遇到的问题是所有json变量的索引都未定义,但是如果我取消注释代码中注释的两行,并在浏览器上查看,$hdr行的外观与它应该的完全相同,就在我尝试返回标头中的$hdr时,我收到了: callback://com.herghost.waves?code=&am

更新:

老实说,我放弃了,最后在sails.js上做了,我会把这个问题留给别人,以防有人看到我哪里做错了


想知道是否有人能帮我解决一个我似乎无法解决的奇怪问题

我有一个oAuth脚本,它将json数组的输出返回到我的android应用程序

我遇到的问题是所有json变量的索引都未定义,但是如果我取消注释代码中注释的两行,并在浏览器上查看,$hdr行的外观与它应该的完全相同,就在我尝试返回标头中的$hdr时,我收到了:

callback://com.herghost.waves?code=&account_name=&refresh_token=&expires_in=
以下是我正在使用的脚本:

error_reporting(E_ALL);
$code = $_GET['code'];
$clientId = "";
$clientSecret = "";
$redirectionUrl = rawurlencode("http://waves-app.com/generate_token.php");
$url = sprintf("https://cloud.digitalocean.com/v1/oauth/token?client_id=%s&client_secret=%s&grant_type=authorization_code&code=%s&redirect_uri=%s", $clientId, $clientSecret, $code, $redirectionUrl);

$fields = array();
$fields_string = "";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);

$result = curl_exec($ch);
curl_close($ch);

$json = json_decode($result, TRUE);
//print_r($json);
if (empty($code)) $code = '';
if (empty($name)) $name = '';
if (empty($refresh)) $refresh = '';
if (empty($expires)) $expires = '';

$code = $json['access_token'];
$name = rawurlencode($json["info"]["name"]);
$refresh = $json["refresh_token"];
$expires =  $json["expires_in"];
$hdr = sprintf("Location:callback://com.herghost.waves?code=%s&account_name=%s&refresh_token=%s&expires_in=%s", $code, $name, $refresh,$expires);
//echo $hdr;
header($hdr);
编辑:我现在尝试过的其他事情

1) 在sprintf中设置硬编码字符串

Location:callback://com.herghost.waves?code=1234&account_name=%s&refresh_token=%s&expires_in=%s"
我在android应用程序中获得了正确的代码

2) 手动设置变量并传递

$test = 'test'
header(sprintf("Location:callback://com.herghost.waves?code=%s&account_name=%s&refresh_token=%s&expires_in=%s",
            $test, rawurlencode($json["info"]["name"]), $json["refresh_token"]//, $json["expires_in"]));
我在android应用程序中得到测试返回

我还通过使用检查了是否存在任何卷曲错误

if(curl_errno($ch))
{
    echo 'Curl error: ' . curl_error($ch);
}
没有错误

var_dump肯定在输出结果,为什么我的变量是空的

另一次编辑

所以我试过这个:

$test = '45678';
$test2 = $json["refresh_token"];
$jsonerr = json_last_error();


echo $test2;
if($jsonerr == 0)
{
header("Location:callback://com.herghost.waves?code=" . $test2 . "+" . $test . "&account_name=1&refresh_token=2&expires_in=3");
}
else
{
    header("Location:callback://com.herghost.waves?code=error&account_name=1&refresh_token=2&expires_in=3" . $jsonerr);
}
我的回答是:

callback://com.herghost.waves?code=+45678&account_name=1&refresh_token=2&expires_in=3

从未对一段简单的代码感到如此愤怒

“callback:”不是有效的URL方案。你能显示
var_dump($json)
”的输出吗?@Barmar,返回似乎很好,我的意图在我的应用程序中激发,var_dump显示带有插入值的json数组。我会再次询问,请显示var_dump的输出。您可能误读了它,这就是为什么我们需要亲自查看它,而不是相信您的话,它具有您期望的值。数组(7){[“访问令牌”]=>string(64)“afb653153fd449c744acf5dc9c506c2428e5bc91d07b45cee7e0e68f30108413”[“令牌类型”]=>string(6)“承载者”[“过期”]=>int(2592000)[“刷新令牌”=>string(64)“097377B87FE140A90453456687B08145B7B002DE7DADC88282732814AEE25ED5D088”[“范围”]=>string(10)“读写”[“uid”]=>int(426152)[“信息”=>array(3){[“名称”]=>string(10)“herghostuk”[“电子邮件”=>string(20)“removedmyemail”[“uuid”=>string(40)“D6DE9517C1293B40345435CF51BC609BB371E8A”}:“不是有效的URL方案。你能显示
var_dump($json)
”的输出吗?@Barmar,它似乎可以正常返回,我的意图在我的应用程序中激发,var_dump显示带有插入值的json数组。我会再次询问,请显示var_dump的输出。您可能误读了它,这就是为什么我们需要亲自查看它,而不是相信您的话,它具有您期望的值。数组(7){[“访问令牌”]=>string(64)“afb653153fd449c744acf5dc9c506c2428e5bc91d07b45cee7e0e68f30108413”[“令牌类型”]=>string(6)“承载者”[“过期”]=>int(2592000)[“刷新令牌”=>string(64)“097377B87FE140A90453456687B08145B7B002DE7DADC88282732814AEE25ED5D088”[“范围”]=>string(10)“读写”[“uid”]=>int(426152)[“信息”=>array(3){[“名称”]=>string(10)“herghostuk”[“电子邮件”=>string(20)“removedmyemail”[“uuid”=>string(40)“D6DE9517C1293B40345435CF51BC609BB371E8A”}:“不是有效的URL方案。你能显示
var_dump($json)
”的输出吗?@Barmar,它似乎可以正常返回,我的意图在我的应用程序中激发,var_dump显示带有插入值的json数组。我会再次询问,请显示var_dump的输出。您可能误读了它,这就是为什么我们需要亲自查看它,而不是相信您的话,它具有您期望的值。数组(7){[“访问令牌”]=>string(64)“afb653153fd449c744acf5dc9c506c2428e5bc91d07b45cee7e0e68f30108413”[“令牌类型”]=>string(6)“承载者”[“过期”]=>int(2592000)[“刷新令牌”=>string(64)“097377B87FE140A90453456687B08145B7B002DE7DADC88282732814AEE25ED5D088”[“范围”]=>string(10)“读写”[“uid”]=>int(426152)[“信息”=>array(3){[“名称”]=>string(10)“herghostuk”[“电子邮件”=>string(20)“removedmyemail”[“uuid”=>string(40)”D6DE9517C1293B40345435CF51BC609BB371E8A}