解析 Facebook PHP API 响应 - 重新制作/回收 05/02/2015
Parsing Facebook PHP API Response - Remastered / Recycled 05/02/2015
我的数组;
{
"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"
}
我知道从我的数组中获取 "Bio",但是互联网上几乎没有关于如何从传递的 Facebook API 数组中获取一级信息的方法;
$FB_About_Bio = $graph->getProperty('bio');
但是在调用时 "movies";
$FB_About_Movies = $graph->getProperty('movies');
这会产生一个我相信它本身的数组,例如我不知道如何做一个循环来列出所有电影标题
如何获取所有电影名称?
完整脚本:
<?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>';
}
更新
user9418 在 Whosebug 上提问“How to parse a facebook graph api response" and bhushya answer 对如何完成它给出了一些见解。以下是我的最新进展,而之前我没有得到任何数据;
新代码段;
$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);
}
结果:
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" }
我的完整代码现在与我在页面回显上面实现的代码相同。我现在只需要学习如何循环并只列出每部电影的名称,将每部电影包装在一个标签中!
方案一:使用asArray()方法将图对象转为数组,作为普通数组解析。
$graphArray = $graph->asArray();
$movies = $graphArray['movies']['data'];
foreach ($movies as $movie) {
$name = $movie['name'];
}
选项 2:获取数据 属性 并循环遍历该数组,然后使用 getProperty 方法获取类型、名称和 ID 属性。
$movies = $graph->getProperty('movies');
foreach ($movies as $movie) {
$name = $movie->getProperty('name');
}
已解决的代码段
$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 />';
}
我是怎么解决的
Facebook Graph API 中的许多内容都与此类似,因此我扩展了研究以询问如何获得用户朋友等等。这让我看到了一个名为“Facebook PHP SDK v4: Get List of Pages Liked by User | Part 10”的 YouTube 视频,它足以解释如何解决我的问题!
我的数组;
{
"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"
}
我知道从我的数组中获取 "Bio",但是互联网上几乎没有关于如何从传递的 Facebook API 数组中获取一级信息的方法;
$FB_About_Bio = $graph->getProperty('bio');
但是在调用时 "movies";
$FB_About_Movies = $graph->getProperty('movies');
这会产生一个我相信它本身的数组,例如我不知道如何做一个循环来列出所有电影标题
如何获取所有电影名称?
完整脚本:
<?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>';
}
更新
user9418 在 Whosebug 上提问“How to parse a facebook graph api response" and bhushya answer 对如何完成它给出了一些见解。以下是我的最新进展,而之前我没有得到任何数据;
新代码段;
$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);
}
结果:
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" }
我的完整代码现在与我在页面回显上面实现的代码相同。我现在只需要学习如何循环并只列出每部电影的名称,将每部电影包装在一个标签中!
方案一:使用asArray()方法将图对象转为数组,作为普通数组解析。
$graphArray = $graph->asArray();
$movies = $graphArray['movies']['data'];
foreach ($movies as $movie) {
$name = $movie['name'];
}
选项 2:获取数据 属性 并循环遍历该数组,然后使用 getProperty 方法获取类型、名称和 ID 属性。
$movies = $graph->getProperty('movies');
foreach ($movies as $movie) {
$name = $movie->getProperty('name');
}
已解决的代码段
$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 />';
}
我是怎么解决的
Facebook Graph API 中的许多内容都与此类似,因此我扩展了研究以询问如何获得用户朋友等等。这让我看到了一个名为“Facebook PHP SDK v4: Get List of Pages Liked by User | Part 10”的 YouTube 视频,它足以解释如何解决我的问题!