CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2011
    Posts
    1

    Exclamation PHP - Parsing JSON from FB Graph

    Hello everyone, I am new to parsing and properly referencing JSON objects or associative arrays in PHP with the Facebook Graph API.

    Here is an example of JSON i will be working with:
    https://graph.facebook.com/comments/...layer_embedded

    Code:
    $request_url ="https://graph.facebook.com/comments/?ids=" . $purl;
    $requests = file_get_contents($request_url);
    $fb_response = json_decode($requests);
    How can I implement a foreach function to cycle through and echo the id's of each element in the data array?

    Thanks in advance!

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: PHP - Parsing JSON from FB Graph

    It's just a bunch of nested objects with an array thrown in there.

    PHP Code:
    foreach ($fb_response as $key => $response) {
      foreach (
    $fb_response->$key as $data) {
        foreach (
    $data as $item) {
          echo 
    'ID: ' $item->id '<br />';
          echo 
    'From ID: ' $item->from->id '<br />';
          echo 
    'From Name: ' $item->from->name '<br />';
          echo 
    'Message: ' $item->message '<br />';
          echo 
    'Timestamp: ' $item->created_time '<br /><br />';
        }
      }

    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured