Cycs
Cycs

Reputation: 239

PHP struggling to decode JSON

I have script that calls at script via cURL. It looks like this,

Route::get('login-redirect', function() {

if (Input::has('error')) {

    return Input::get('error_description');
}
if (Input::has('code')) {

   $fields = array(
        'grant_type' => 'password',
        'username' => '[email protected]',
        '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);

}

The json returned looks like this, if I do dd($json)

{"content":null,"error":true,"error_description":"Invalid username and password combination"}int(1)

I feel like after running it through json_decode I should be able to just output $json->error but no.

The JSON gets made in the following class, but I cannot see anything odd here either, I am doing incorrect, or do I misunderstand 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);
    }
} 

Upvotes: 1

Views: 520

Answers (3)

Darren
Darren

Reputation: 13128

Update

As stated in the other answers, you're not actually receiving the output because you haven't set CURLOPT_RETURNTRANSFER. So curl_exec() will echo out the response to the DOM and return true (1) as your curl request ran successfully.

You'll be able to run the below stuff by setting this in your curl request somewhere:

curl_setop(CURLOPT_RETURNTRANSFER, true);

dd() is a laravel function and this is what the documentation says:

Dump the given variable and end execution of the script.

I'd presume it is just a wrapper function for a prettier looking var_dump() (As I don't use laravel, I wouldn't know its exact output.).

What you want is to decode the $result that is returned from your cUrl. Something like this should suffice:

$data = json_decode($result);

echo $data->error_description;

The successfully decoded object looks like this:

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

Example


You can even test your boolean error value like this now:

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

Upvotes: 0

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81137

What you've got as an output is not from 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)

Here's why:

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

Upvotes: 0

Lashus
Lashus

Reputation: 399

First of all, you do not have CURLOPT_RETURNTRANSFER - your curl_exec returns output buffer directly to the screen.

Second of all, it looks like you have var_dump somewhere and I cannot see where :)

Third of all - you didn't asked any direct question.

Edit

Okay i've read it few time and answer below. The dd() function is truly a var_dump wrapper but it is dumping var_dump data into json format afaics.

Upvotes: 1

Related Questions