skip to Main Content

I send a curl request on facebook api and i am getting this response.

'{
    "id": "137392510101234",
    "email": "[email protected]",
    "first_name": "Open",
    "last_name": "User",
    "link": "https://www.facebook.com/app_scoped_user_id/137392510108522/",
    "name": "Open Graph Test User"
}'

Now i am trying to read the value of id. I used below code for it.

$jsonid='{
    "id": "137392510101234",
    "email": "[email protected]",
    "first_name": "Open",
    "last_name": "User",
    "link": "https://www.facebook.com/app_scoped_user_id/137392510108522/",
    "name": "Open Graph Test User"
}';

 $jsonArrayToken = json_decode($jsonid,true);
 echo $jsonArrayToken['id'];

But i cant read the whole value of id.
i am getting only first four digits “1373”.
i want whole id “137392510101234”.

Is there any mistake? please suggest me.

3

Answers


  1. working fine check here:

    eval link :

    https://eval.in/734713

    May be problem is some where else not in this code

    Login or Signup to reply.
  2. json_decode provides an object, not an array. You should access the data like following :

    <?php
        $jsonid='{
            "id": "137392510108522",
            "email": "[email protected]",
            "first_name": "Open",
            "last_name": "User",
            "link": "https://www.facebook.com/app_scoped_user_id/137392510108522/",
            "name": "Open Graph Test User"
        }';
    
        $arrayid = json_decode($jsonid); // json_decode provide an OBJECT, not an array
        $id = $arrayid->id;
    
        echo $id;
    ?>
    
    Login or Signup to reply.
  3. I just copied your code added var_export, no problem found.

    <?php
    header('content-type: text/plain');
    $jsonid='{
        "id": "137392510108522",
        "email": "[email protected]",
        "first_name": "Open",
        "last_name": "User",
        "link": "https://www.facebook.com/app_scoped_user_id/137392510108522/",
        "name": "Open Graph Test User"
    }';
    
     $jsonArrayToken = json_decode($jsonid,true);
     echo $jsonArrayToken['id'] . "nn";
     var_export($jsonArrayToken);
    
    ?>
    

    Response:

    137392510108522
    
    array (
      'id' => '137392510108522',
      'email' => '[email protected]',
      'first_name' => 'Open',
      'last_name' => 'User',
      'link' => 'https://www.facebook.com/app_scoped_user_id/137392510108522/',
      'name' => 'Open Graph Test User',
    )
    

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search