[英]PHP struggling to decode JSON
我有通过cURL调用脚本的脚本。 看起来像这样
Route::get('login-redirect', function() {
if (Input::has('error')) {
return Input::get('error_description');
}
if (Input::has('code')) {
$fields = array(
'grant_type' => 'password',
'username' => 'admin@local.com',
'password' => 'passwohrd',
'client_id' => 'testclient'
);
$fieldstring = http_build_query($fields, "\n");
$url = "http://apitest.local/api/v1/get-token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldstring);
$result = curl_exec($ch);
$json = json_decode($result);
curl_close($ch);
$fields = array('access_token' => '3c1e6b099f172fc01304403939edf8e56904ab61');
$fieldstring = http_build_query($fields, "\n");
$url = "http://apitest.local/api/v1/me";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldstring);
$result = curl_exec($ch);
curl_close($ch);
dd($result);
}
如果我做dd($json)
,返回的json看起来像这样
{"content":null,"error":true,"error_description":"Invalid username and password combination"}int(1)
我觉得通过json_decode
运行它之后,我应该只能输出$json->error
但是没有。
JSON是在以下类中生成的,但是我在这里看不到任何奇怪的东西,我做错了,还是我误解了json_decode?
<?php
namespace Shaunpersad\ApiFoundation\Http;
use App;
use Response;
class ErrorResponse
{
public static function make($message = '', $status = 200, array $headers = array(), $options = 0)
{
$response = App::make(
'api_response_array',
array(
'content' => null,
'error' => true,
'error_description' => $message
)
);
return Response::json($response, $status, $headers, $options);
}
}
首先,您没有CURLOPT_RETURNTRANSFER-curl_exec将输出缓冲区直接返回到屏幕。
第二,看起来您在某个地方有var_dump,但我看不到哪里:)
第三-您没有提出任何直接问题。
编辑
好吧,我已经读了几次并且在下面回答。 dd()函数确实是一个var_dump包装器,但是它将var_dump数据转储为json格式afaics。
更新
如其他答案所述,由于尚未设置CURLOPT_RETURNTRANSFER
,因此实际上并未收到输出。 因此curl_exec()
将回显对DOM的响应,并在您的curl
请求成功运行时返回true( 1
)。
您可以通过在curl请求中的某处设置以下内容来运行以下内容:
curl_setop(CURLOPT_RETURNTRANSFER, true);
dd()
是一个laravel function
,这就是文档所说的内容:
转储给定的变量并结束脚本的执行。
我认为这只是一个看起来更漂亮的var_dump()
的包装函数( 因为我不使用laravel,所以我不知道它的确切输出。 )。
您想要的是解码从cUrl
返回的$result
。 这样的事情就足够了:
$data = json_decode($result);
echo $data->error_description;
成功解码的对象如下所示:
stdClass Object
(
[content] =>
[error] => 1
[error_description] => Invalid username and password combination
)
您现在可以像这样测试您的布尔error
值:
if($data->error) {
//....true
} else {
//....false
}
输出的结果不是来自dd($json)
:
// this part has been output by curl_exec():
{"content":null,"error":true,"error_description":"Invalid username and password combination"}
// only this part comes from dd($json):
int(1)
原因如下:
// no CURLOPT_RETURNTRANSFER, so curl_exec() outputs result and returns true:
$result = curl_exec($ch);
// thus $result = true;
// so here $json = 1, since this is what json_decode(true) will return
$json = json_decode($result);
// then you did dd($json), so it just appended var_dump(1) to the output:
{"content":null,"error":true,"error_description":"Invalid username and password combination"}int(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.