Novice
Novice

Reputation: 77

How to converts JSON string into a PHP variable?

I was trying to parse the json data from a url using cURL and then json_decode to access the objects but I failed. I already search it how to access but I failed.

this is the links that I visited hoping that I can solve the problem.

http://www.dyn-web.com/tutorials/php-js/json/decode.php
https://stackoverflow.com/questions/12429029/php-get-values-from-json-encode
https://stackoverflow.com/questions/20193575/fetch-json-data-using-php
https://stackoverflow.com/questions/12429029/php-get-values-from-json-encode
https://stackoverflow.com/questions/4433951/decoding-json-after-sending-using-php-curl

this is the result of the var_dump($obj);

array(1) {
  ["login"]=>
  array(2) {
    ["error"]=>
    bool(false)
    ["user"]=>
    array(5) {
      ["br_code"]=>
      int(0)
      ["mem_id"]=>
      int(202)
      ["username"]=>
      string(8) "johndoe"
      ["email"]=>
      string(33) "[email protected]"
      ["created_at"]=>
      string(19) "2017-08-07 15:35:39"
    }
  }
}

and this is my PHP code

<?php
session_start();

$ch = curl_init('http://localhost/sample/login.php');

$username= $_SESSION["USERNAME"];
$password= $_SESSION["PASSWORD"];

$credentials = [
    'username' => $username,
    'password' => $password
];

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $credentials);
curl_setopt($ch, CURLOPT_USERNAME, "{$username}:{$password}");
// execute!
$response = curl_exec($ch);
// close the connection
curl_close($ch);

$obj= json_decode($response, true);

// echo $obj; // Notice: Array to string conversion in

// echo $obj[0]; //  Undefined offset

// echo $obj->user; //Trying to get property of non-object in

var_dump($obj);



?>

Upvotes: 2

Views: 272

Answers (4)

Nikhil Parmar
Nikhil Parmar

Reputation: 499

When you do json_decode($response, true), and you are using second parameter as true, it will decode it into associative array.

Your var_dump also says that it is array. So, you need to access $obj as array not object

if you want to access the user array:

$obj['login']['user']

if you want to access the username/br_code:

$obj['login']['user']['username'];
$obj['login']['user']['br_code'];

more info about json_decode

Upvotes: 1

saniales
saniales

Reputation: 461

json_decode decodes a JSON string into a multi dimensional array you get that error because in PHP objects are not arrays just access with [] and you are fine

$obj['user']['field_requested']

Upvotes: 0

B. Desai
B. Desai

Reputation: 16436

Your data is in login array so try following to get details of user

$user = $obj['login']['user'];
//to print email
echo $user['email'];
//to print username
echo $user['username'];
//so on

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133370

seems an array of array so

eg: for username

var_dump($obj['login']['user']['username']);

and so on for others values

$my_username =  $obj['login']['user']['username']);

Upvotes: 3

Related Questions