I have managed to pull my Facebook Feed into a hash - now how to manipulate this data and print it out 'nicely'?
The output can be seen here: http://4playtheband.co.uk/twitter/example.php
An example post is :
Array
(
[data] => Array
(
[0] => Array
(
[id] => 100001172604294_157498814333844
[from] => Array
(
[name] => Martin Carlin
[id] => 100001172604294
)
[picture] => http://platform.ak.fbcdn.net/www/app_full_proxy.php?app=291549705119&v=1&size=z&cksum=c37c749664ac69ae07a1f0ffcd1348e5&src=http%3A%2F%2Fcityvillefb2.static.zgncdn.com%2Fhashed%2F33a4a96920be7e6e3aeefe90b0915d1a.png
[link] => http://apps.facebook.com/cityville/Reward.php?frHost=31058426480&frId=637143ed6a1be4e9ca42d9a5bd444066&frType=municipal_material_checkerflag&sendKey=bfdfe9442a98ce31102cf6f2eff52a2a%24%24ccF%28NVPZ%2A4jCM0YUzNKZxMdo6KHTAgZEZnQs5qUewzHYkAPHFoaJRaDyAG%29P9zeEXyVBDlaqIH7JYL%28wITH4BTXkXFfU3C9%28Ht&sig=8a0b72d997dc15d8fa76dbb9a627b55d&sendkey=bfdfe9442a98ce31102cf6f2eff52a2a%24%24ccF%28NVPZ%2A4jCM0YUzNKZxMdo6KHTAgZEZnQs5qUewzHYkAPHFoaJRaDyAG%29P9zeEXyVBDlaqIH7JYL%28wITH4BTXkXFfU3C9%28Ht
[name] => Martin has an extra Checker Flag to help finish your Go Karts!
[description] => Martin is working hard to build a new Go Karts in CityVille and could use your help. You'll earn bonus Coins for lending a hand, too!
[icon] => http://photos-f.ak.fbcdn.ne开发者_如何学运维t/photos-ak-snc1/v27562/71/291549705119/app_2_291549705119_3378.gif
[actions] => Array
(
[0] => Array
(
[name] => Send Checker Flag,...
[link] => http://apps.facebook.com/cityville/Reward.php?frHost=31058426480&frId=637143ed6a1be4e9ca42d9a5bd444066&frType=municipal_material_checkerflag&sendKey=bfdfe9442a98ce31102cf6f2eff52a2a%24%24ccF%28NVPZ%2A4jCM0YUzNKZxMdo6KHTAgZEZnQs5qUewzHYkAPHFoaJRaDyAG%29P9zeEXyVBDlaqIH7JYL%28wITH4BTXkXFfU3C9%28Ht&sig=8a0b72d997dc15d8fa76dbb9a627b55d&sendkey=bfdfe9442a98ce31102cf6f2eff52a2a%24%24ccF%28NVPZ%2A4jCM0YUzNKZxMdo6KHTAgZEZnQs5qUewzHYkAPHFoaJRaDyAG%29P9zeEXyVBDlaqIH7JYL%28wITH4BTXkXFfU3C9%28Ht
)
)
[type] => link
[application] => Array
(
[name] => CityVille
[id] => 291549705119
)
[created_time] => 2011-09-07T08:30:54+0000
[updated_time] => 2011-09-07T08:30:54+0000
[comments] => Array
(
[count] => 0
)
)
[1] => Array
( // etc
What I'm thinking of is perhaps just printing out the first 10 of these posts. I have a lot of crappy cityville things on my feed just now as you can see but eventually this will be used for an actual company page so I'm not sure 'normal' statuses still have as many elements to them?
Also, is there a way to set a limit on how many I can retrieve - no point in Facebook deciding only to give me 25 when I only want the 10 most recent, code below:
<?php
include_once "src/facebook.php";
$appId = '11111111111';
$secret = '1111111111';
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $secret,
'cookie' => true,
));
//Facebook Authentication part
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'user_status,publish_stream,user_photos'
)
);
if ($user) {
try {
//$user_profile = $facebook->api('/me');
$user_feed = $facebook->api('/me/feed');
//$user_home = $facebook->api('/me/home');
echo $user_profile['name'];
echo "<pre>";
//print_r ($user_profile);
print_r ($user_feed);
//print_r ($user_home);
echo "</pre>";
} catch (FacebookApiException $e) {
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
?>
Any help is much appreciated, I'm not very experienced with complicated Arrays such as this one and somebody out there will more than likely have had to format this themselves I'm sure.
Thanks,
UPDATE
Thanks for the helpful answers so far but I've just noticed that comments and likes are included in the array once they are made and was wondering on how to pull these aswell, only trouble being is they are in yet another array nested within the overall structure of the post.
[0] => Array
(
[id] => 100001172604294_225960714119680
[from] => Array
(
[name] => Martin Carlin
[id] => 100001172604294
)
[message] => need to post something to see if this thing I'm trying to make work will pick it up. Apologies it's not entertaining, the odds will get better as the day goes on (probably).
[comments] => Array
(
[data] => Array
(
[0] => Array
(
[id] => 100001172604294_225960714119680_2800008
[from] => Array
(
[name] => xxxxxxx
[id] => 11111111
)
[message] => worst comment ever!
[created_time] => 2011-09-07T09:10:49+0000
[likes] => 1
)
)
[count] => 1
)
)`
If you only want the first 10, you could do something like this
for ($i=0; $i<10, $i++) {
// print the naem of the poster
echo "posted by: ".$user_feed['data'][$i]['from']['name']."<br>";
// print description of every post ...
echo $user_feed['data'][$i]['description'];
}
精彩评论