Parsing Facebook PHP API Response - Remastered / Recycled 05/02/2015

倖福魔咒の 提交于 2019-12-02 23:29:48

问题


My Array;

{
  "bio": "Testing Facebook Bio -> Stupidly forgot to set whilst trying to fetch this information testing my latest Facebook App!", 
  "movies": {
    "data": [
      {
        "name": "Night At The Museum", 
        "id": "251922028320619"
      }, 
      {
        "name": "Little Nicky", 
        "id": "112378985439799"
      }, 
      {
        "genre": "Action / Adventure / Comedy / Family / Sci-Fi", 
        "name": "Back to the Future Trilogy", 
        "id": "141545972523915"
      }, 
      {
        "genre": "Stupid", 
        "name": "Jackass", 
        "id": "21295715752"
      }, 
      {
        "genre": "Comedy", 
        "name": "Mrs. Brown's Boys D'Movie", 
        "id": "217475368404328"
      }, 
      {
        "genre": "Animation, Family", 
        "name": "Madagascar", 
        "id": "149800431712088"
      }, 
      {
        "genre": "Animation", 
        "name": "Frozen", 
        "id": "482368755113431"
      }, 
      {
        "genre": "Animation", 
        "name": "Toy Story", 
        "id": "10498014129"
      }, 
      {
        "genre": "This is a gritty drama that develops into an uplifting story of triumph over adversity. It should appeal to a wide audience,  both male and female.", 
        "name": "Breaking Free  Film", 
        "id": "135991503231501"
      }, 
      {
        "genre": "Animation", 
        "name": "The Lion King", 
        "id": "12393266550"
      }, 
      {
        "genre": "Animation | Comedy | Family ", 
        "name": "Despicable Me", 
        "id": "117067844993952"
      }, 
      {
        "genre": "Action / Adventure", 
        "name": "Thor", 
        "id": "113589202010624"
      }, 
      {
        "name": "Scooby-Doo: The Movie", 
        "id": "306089742863765"
      }, 
      {
        "genre": "Action / fairy tale", 
        "name": "Hansel & Gretel: Witch Hunters", 
        "id": "271965656164363"
      }, 
      {
        "name": "Harry Potter", 
        "id": "156794164312"
      }, 
      {
        "name": "Star Wars", 
        "id": "216676368377759"
      }, 
      {
        "name": "Minion", 
        "id": "136787429687873"
      }, 
      {
        "name": "Night at the Museum 2", 
        "id": "115126478502712"
      }, 
      {
        "genre": "Animation, Holiday, Family", 
        "name": "The Nightmare Before Christmas", 
        "id": "173587329354820"
      }, 
      {
        "name": "Pacific Rim", 
        "id": "439835889373123"
      }, 
      {
        "genre": "Action Adventure", 
        "name": "Oblivion UK", 
        "id": "235958443193536"
      }, 
      {
        "genre": "Animation, Family", 
        "name": "Shrek", 
        "id": "355374000182"
      }, 
      {
        "name": "Scooby Doo: The Movie", 
        "id": "106352129401640"
      }, 
      {
        "genre": "Epic Action-Adventure", 
        "name": "Dracula Untold", 
        "id": "332230740134829"
      }, 
      {
        "name": "Simba", 
        "id": "27665751322"
      }
    ], 
    "paging": {
      "next": "https://graph.facebook.com/v2.2/663878750359892/movies?fields=genre,name&limit=25&offset=25&__after_id=enc_AezyAtvaN1UtcaRwF9kgU5Z5PIv07LU_3Wli-CRCkYEol9BoJJtn86fNGT4v-XsnG-o"
    }
  }, 
  "id": "663878750359892"
}

I know to get the "Bio" from my array, however this is pretty much out there onthe internet how to get the first level information from the Facebook API Array passed on;

$FB_About_Bio = $graph->getProperty('bio');

However when it comes to calling for "movies";

$FB_About_Movies = $graph->getProperty('movies');

This produces an array of itself I believe of which I do not know how to do a loop to list all the movie titles for example

How would I get all the movie names?

Full Script:

<?php

/* INCLUSION OF LIBRARY FILEs*/
    require_once( 'lib/Facebook/FacebookSession.php');
    require_once( 'lib/Facebook/FacebookRequest.php' );
    require_once( 'lib/Facebook/FacebookResponse.php' );
    require_once( 'lib/Facebook/FacebookSDKException.php' );
    require_once( 'lib/Facebook/FacebookRequestException.php' );
    require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php');
    require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
    require_once( 'lib/Facebook/GraphObject.php' );
    require_once( 'lib/Facebook/GraphUser.php' );
    require_once( 'lib/Facebook/GraphSessionInfo.php' );
    require_once( 'lib/Facebook/Entities/AccessToken.php');
    require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
    require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php');
    require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php');

/* USE NAMESPACES */
    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\GraphUser;
    use Facebook\GraphSessionInfo;
    use Facebook\FacebookHttpable;
    use Facebook\FacebookCurlHttpClient;
    use Facebook\FacebookCurl;

/*PROCESS*/

    //1.Stat Session
        session_start();

    //2.Use app id,secret and redirect url
        $app_id = '000000000000000';
        $app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
        $redirect_url='http://rafflebananza.com/Desktop/facebook.php';

    //3.Initialize application, create helper object and get fb sess
        FacebookSession::setDefaultApplication($app_id,$app_secret);
        $helper = new FacebookRedirectLoginHelper($redirect_url);
        $sess = $helper->getSessionFromRedirect();

    //4. if fb sess exists echo name 
        if(isset($sess)){

            //store the token in the php session
                $_SESSION['fb_token']=$sess->getToken();

            //create request object,execute and capture response
                $request = new FacebookRequest($sess,'GET','/me');

            // from response get graph object
                $response = $request->execute();
                $graph = $response->getGraphObject(GraphUser::classname());

            // use graph object methods to get user details
                $FB_id = $graph->getId();
                $FB_First_Name = $graph->getProperty('first_name');
                $FB_Middle_Name = $graph->getProperty('middle_name');
                $FB_Last_Name = $graph->getProperty('last_name');
                $FB_About_Bio = $graph->getProperty('bio');
                $FB_image = 'https://graph.facebook.com/'.$id.'/picture?width=300';
                $FB_email = $graph->getProperty('email');

                $graphArray = $graph->asArray();
                $movies = $graphArray['movies']['data'];
                foreach ($movies as $movie) {
                    $name = $movie['name'];
                }

        // Echo Info To Page:
            echo '<h1>'.$name.'</h1>';
            echo '<table><tr><td><strong>First Name:</strong></td><td><input type="text" value="'.$FB_First_Name.'"></input></td></tr>';
            echo "<tr><td><strong>Middle Name: </strong></td><td>$FB_Middle_Name</td></tr>";
            echo "<tr><td><strong>Last Name: </strong></td><td>$FB_Last_Name <br</td></tr>";
            echo "<tr><td><strong>E-Mail Address:</strong></td><td>$FB_email</td></tr>";
            echo "<tr><td><strong>About You:</strong></td><td>$FB_About_Bio</td></tr></table>";
            echo "<img src='FB_$image' /><br><br>";

        // Logout Button
            echo "<a href='".$logout."'><button>Logout</button></a>";


    }else{
        //else echo login
            echo '<a href="'.$helper->getLoginUrl(array('email')).'" >Login with facebook</a>';
    }

Update

user9418 asked on Stackoverflow "How to parse a facebook graph api response" and bhushya answer has given somewhat of an insight to how it should be done. Below is my latest progress whereas previously I was getting no data whatsoever;

New Snippet;

$user_profile = (new FacebookRequest(
    $sess, 'GET', '/me/movies'
))->execute()->getGraphObject();
$movies =  $user_profile->getProperty('data');

$movies_data = $movies->asArray();//this will do all job for you..
foreach($movies_data as $row){
    var_dump($row);
}

Result:

object(stdClass)#28 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(19) "Night At The Museum" ["created_time"]=> string(24) "2014-12-16T23:05:57+0000" ["id"]=> string(15) "251922028320619" } object(stdClass)#29 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(12) "Little Nicky" ["created_time"]=> string(24) "2014-12-14T02:35:10+0000" ["id"]=> string(15) "112378985439799" } object(stdClass)#30 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(26) "Back to the Future Trilogy" ["created_time"]=> string(24) "2014-10-06T04:12:32+0000" ["id"]=> string(15) "141545972523915" } object(stdClass)#31 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(7) "Jackass" ["created_time"]=> string(24) "2014-08-23T21:51:24+0000" ["id"]=> string(11) "21295715752" } object(stdClass)#32 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(25) "Mrs. Brown's Boys D'Movie" ["created_time"]=> string(24) "2014-08-06T10:16:10+0000" ["id"]=> string(15) "217475368404328" } object(stdClass)#33 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(10) "Madagascar" ["created_time"]=> string(24) "2014-07-09T22:16:05+0000" ["id"]=> string(15) "149800431712088" } object(stdClass)#34 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(6) "Frozen" ["created_time"]=> string(24) "2014-05-27T20:47:31+0000" ["id"]=> string(15) "482368755113431" } object(stdClass)#35 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(9) "Toy Story" ["created_time"]=> string(24) "2014-04-15T01:39:17+0000" ["id"]=> string(11) "10498014129" } object(stdClass)#36 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(19) "Breaking Free Film" ["created_time"]=> string(24) "2014-02-20T14:16:06+0000" ["id"]=> string(15) "135991503231501" } object(stdClass)#37 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(13) "The Lion King" ["created_time"]=> string(24) "2014-02-19T08:32:34+0000" ["id"]=> string(11) "12393266550" } object(stdClass)#38 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(13) "Despicable Me" ["created_time"]=> string(24) "2014-02-19T08:32:29+0000" ["id"]=> string(15) "117067844993952" } object(stdClass)#39 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(4) "Thor" ["created_time"]=> string(24) "2013-11-29T02:22:14+0000" ["id"]=> string(15) "113589202010624" } object(stdClass)#40 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(21) "Scooby-Doo: The Movie" ["created_time"]=> string(24) "2013-10-17T13:41:51+0000" ["id"]=> string(15) "306089742863765" } object(stdClass)#41 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(30) "Hansel & Gretel: Witch Hunters" ["created_time"]=> string(24) "2013-10-15T21:22:05+0000" ["id"]=> string(15) "271965656164363" } object(stdClass)#42 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(12) "Harry Potter" ["created_time"]=> string(24) "2013-10-09T21:29:08+0000" ["id"]=> string(12) "156794164312" } object(stdClass)#43 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(9) "Star Wars" ["created_time"]=> string(24) "2013-08-30T20:12:14+0000" ["id"]=> string(15) "216676368377759" } object(stdClass)#44 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(6) "Minion" ["created_time"]=> string(24) "2013-07-22T12:25:27+0000" ["id"]=> string(15) "136787429687873" } object(stdClass)#45 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(21) "Night at the Museum 2" ["created_time"]=> string(24) "2013-06-14T01:18:02+0000" ["id"]=> string(15) "115126478502712" } object(stdClass)#46 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(30) "The Nightmare Before Christmas" ["created_time"]=> string(24) "2013-06-08T11:11:36+0000" ["id"]=> string(15) "173587329354820" } object(stdClass)#47 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(11) "Pacific Rim" ["created_time"]=> string(24) "2013-05-16T19:30:24+0000" ["id"]=> string(15) "439835889373123" } object(stdClass)#48 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(11) "Oblivion UK" ["created_time"]=> string(24) "2013-02-14T23:02:31+0000" ["id"]=> string(15) "235958443193536" } object(stdClass)#49 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(5) "Shrek" ["created_time"]=> string(24) "2012-10-03T07:01:57+0000" ["id"]=> string(12) "355374000182" } object(stdClass)#50 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(21) "Scooby Doo: The Movie" ["created_time"]=> string(24) "2012-09-20T11:55:28+0000" ["id"]=> string(15) "106352129401640" } object(stdClass)#51 (4) { ["category"]=> string(5) "Movie" ["name"]=> string(14) "Dracula Untold" ["created_time"]=> string(24) "2012-09-17T16:15:38+0000" ["id"]=> string(15) "332230740134829" } object(stdClass)#52 (4) { ["category"]=> string(15) "Movie character" ["name"]=> string(5) "Simba" ["created_time"]=> string(24) "2012-09-06T22:23:22+0000" ["id"]=> string(11) "27665751322" }

My full code now being the same with this implemented above my echoing to the page. I just need to learn now how to loop through and list only the name of each movie wrapping each one in a tag!


回答1:


Option 1: Convert the graph object to an array using the asArray() method and parse as a normal array.

$graphArray = $graph->asArray();
$movies = $graphArray['movies']['data'];
foreach ($movies as $movie) {
    $name = $movie['name'];
}

Option 2: get the data property and loop through that array, then get the genre, name, and id properties using the getProperty method.

$movies = $graph->getProperty('movies');
foreach ($movies as $movie) {
    $name = $movie->getProperty('name');
}



回答2:


Resolved Snippet

$FB_User_Interests_Movies = (new FacebookRequest(
    $sess, 'GET', '/me/movies'
))->execute()->getGraphObject()->asArray();
foreach($FB_User_Interests_Movies['data'] as $key) {
    echo $key->name.'<br />';
}

How I resolved

Many things are in the Facebook Graph API alike this so I expanded research to ask how to get user friends and whatnot. This lead me to a YouTube video titled "Facebook PHP SDK v4: Get List of Pages Liked by User | Part 10" which was enough to explain how to achieve my question!



来源:https://stackoverflow.com/questions/28267526/parsing-facebook-php-api-response-remastered-recycled-05-02-2015

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!