PHP json_decode变量未设置

时间:2015-06-08 21:51:28

标签: php json curl sails.js

更新:

说实话,我放弃了,最后在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

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

0 个答案:

没有答案
相关问题