简体   繁体   中英

how to get only specific variable value from json response(facebook response)

I want only a specific value(say "name") from my generated output. I used this code var_dump($request_object_details); to generate the below output.

What output I am getting:-

array(6) { ["id"]=> string(26) "514484461930096_1446926141"   ["application"]=> array(2) { ["name"]=> string(18) "Pocket Financetest" ["id"]=> string(15) "270339389736782" } ["to"]=> array(2) { ["name"]=> string(12) "Prince Singh" ["id"]=> string(10) "1446926141" } ["from"]=> array(2) { ["name"]=> string(14) "Ashutosh Singh" ["id"]=> string(15) "100003645579131" } ["message"]=> string(16) "My Great Request" ["created_time"]=> string(24) "2013-01-17T03:45:36+0000" } 

I used the below code to get the above output:

         <?php
        require_once('phps/fbsdk/src/facebook.php');
        $config = array
        (
          'appId' => '270339389736782',
          'secret' => '667e1795cc1f308f312d49d6b1c17cb8',
        );
        $facebook = new Facebook($config);
        //get the request ids from the query parameter
         $request_ids = explode(',', $_REQUEST['request_ids']);

         //build the full_request_id from request_id and user_id
          function build_full_request_id($request_id, $user_id) {
          return $request_id . '_' . $user_id;
         }  

         //for each request_id, build the full_request_id and delete request
     foreach ($request_ids as $request_id)
      {
         $uid = $facebook->getUser();

         $full_request_id = build_full_request_id($request_id, $uid); 
        $request_object_details = $facebook->api("/$full_request_id");
        var_dump($request_object_details);//This is giving me the output

        try {
         $delete_success = $facebook->api("/$full_request_id",'DELETE');
         if ($delete_success) {
            echo "Successfully deleted " . $full_request_id;}
         else {
           echo "Delete failed".$full_request_id;}
        }
        catch (FacebookApiException $e) {
        echo "error=".$e;}
        }
     ?>

Your question is not clear but still I am posting some sort of code for you.

Use json_decode

Do like this : It will return object

$obj = json_decode($request_object_details);
$result= $obj->id;

OR

You can use json_decode and convert it into the array:

$request_object_details = $facebook->api("/$full_request_id");
$result=json_decode($request_object_details, true);

The second parameter will make decoded json string into an associative arrays.

Now you can directly use :

echo $result['id'];
echo $result['application'];

and so on...

Hope it'll help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM