Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP努力解码JSON_Php_Laravel_Json - Fatal编程技术网

PHP努力解码JSON

PHP努力解码JSON,php,laravel,json,Php,Laravel,Json,我的脚本通过cURL调用script。看起来是这样的, Route::get('login-redirect', function() { if (Input::has('error')) { return Input::get('error_description'); } if (Input::has('code')) { $fields = array( 'grant_type' => 'password', 'username'

我的脚本通过cURL调用script。看起来是这样的,

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)

{“内容”:null,“错误”:true,“错误描述”:“无效用户名和密码组合”}int(1)

我觉得在运行了
json\u decode
之后,我应该能够只输出
$json->error
,但是没有

JSON是在下面的类中生成的,但是我在这里也看不到任何奇怪的东西,我做的不正确,或者我误解了JSON_解码

<?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\u RETURNTRANSFER
。因此,当
curl
请求成功运行时,
curl\u exec()

通过在某个地方的curl请求中设置此选项,您可以运行以下内容:

curl_setop(CURLOPT_RETURNTRANSFER, true);

dd()
是一个函数,文档中是这么说的:

转储给定变量并结束脚本的执行

我假设它只是一个包装器函数,用于外观更漂亮的
var\u dump()
(因为我没有使用laravel,所以我不知道它的确切输出)

您需要解码从
cUrl
返回的
$result
。这样的东西应该足够了:

$data = json_decode($result);

echo $data->error_description;
成功解码的对象如下所示:

stdClass Object
(
    [content] => 
    [error] => 1
    [error_description] => Invalid username and password combination
)


您甚至可以像现在这样测试布尔值
错误
值:

if($data->error) {
    //....true
} else {
    //....false
}

首先,您没有CURLOPT_RETURNTRANSFER—您的curl_exec将输出缓冲区直接返回到屏幕

其次,看起来您在某个地方有var_转储,我看不到在哪里:)

第三,你没有问任何直接的问题

编辑


好的,我已经读了几遍了,下面回答。dd()函数实际上是一个var_dump包装器,但它将var_dump数据转储到json格式的afaics中。

作为输出的不是来自
dd($json)

原因如下:

// 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)
如果
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)