Sandhya Gor
Sandhya Gor

Reputation: 1460

JSON_DECODE() not working?

I know its a very silly question but still I have to ask this coz its not working.

I am calling an API...

    $service_url = 'http://localhost:8888/ffmobile/signup';
    $curl = curl_init($service_url);
    $curl_post_data = array('email' => $this->params()->fromPost('email'), 'password' => $this->params()->fromPost('password'), 'userName' => $this->params()->fromPost('uname'));
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $curl_response = curl_exec($curl);
    curl_close($curl);

when I am printing the $curl_response..the output is as following

{"Error":{"name":"Email Unavailable","message":"Email is already taken. Please choose different email.","code":202},"status":"False","requestId":null}

The output is proper but when I am using it like following..

$response = json_decode($curl_response, TRUE);

and I am printing the $response it does not print anything.

What can be the problem ?

Can anybody help me?

Upvotes: 2

Views: 877

Answers (3)

Adrian Cid Almaguer
Adrian Cid Almaguer

Reputation: 7791

Try with this example:

<?php

$json = '{
    "Error": {
        "name": "Email Unavailable",
        "message": "Email is already taken. Please choose different email.",
        "code": 202
    },
    "status": "False",
    "requestId": null
}';

$info = json_decode($json, true);

echo "Error - Name: {$info['Error']['name']}<br>"; 
echo "Error - Message: {$info['Error']['message']}<br>"; 
echo "Error - Code: {$info['Error']['code']}<br>"; 
echo "Status: {$info['status']}<br>"; 
echo "RequestId: {$info['requestId']}<br>"; 
?>

Output :

Error - Name: Email Unavailable
Error - Message: Email is already taken. Please choose different email.
Error - Code: 202
Status: False
RequestId: 

Upvotes: 1

blablabla
blablabla

Reputation: 1478

There could be a curl error. If this is the case, curl_exec returns false. Also check for json_decode errors.

Source: http://php.net/manual/en/function.curl-error.php

$service_url = 'http://localhost:8888/ffmobile/signup';
$curl = curl_init($service_url);
$curl_post_data = array('email' => $this->params()->fromPost('email'), 'password' => $this->params()->fromPost('password'), 'userName' => $this->params()->fromPost('uname'));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$curl_response = curl_exec($curl);
if($curl_response === false)
{
    echo 'Curl error: ' . curl_error($curl);
}
else
{
    $response = json_decode($curl_response, TRUE);
    curl_close($curl);
    if( is_null($response) ) {
        echo 'json_decode error: ' . json_last_error();
    } else {
        // everything OK, do something with the response
    }
}

Upvotes: 0

BobbyTables
BobbyTables

Reputation: 4685

To check if you decode works fine you can run this code:

$json = '{"Error":{"name":"Email Unavailable","message":"Email is already taken. Please choose different email.","code":202},"status":"False","requestId":null}';     
print_r(json_decode($json, TRUE));

If theres is an error decoding in your code, json_decode will return null, you can check for this, and run json_last_error()

Upvotes: 0

Related Questions