好的,所以这就是事情,我一整天都在搜索facebook文档和谷歌搜索,没有任何功能结果。
我的目标是创建一个允许我检索和发送Facebook消息的应用程序,就像facebook messenger应用程序一样,我已经在heroku中托管了我的半功能代码,并且能够请求权限从用户那里读取数据登录到应用程序。
我可以使用facebook php sdk和以下代码行检索登录用户的一些信息(在本例中是我自己的帐户):
//the facebook object is created
$facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'sharedSession' => true,
'trustForwarded' => true,
));
$user_id = $facebook->getUser();
if ($user_id) {
try {
// Fetch the viewer's basic information
$basic = $facebook->api('/me');
} catch (FacebookApiException $e) {
// If the call fails we check if we still have a user. The user will be
// cleared if the error is because of an invalid accesstoken
if (!$facebook->getUser()) {
header('Location: '. AppInfo::getUrl($_SERVER['REQUEST_URI']));
exit();
}
}
// This fetches some things that you like . 'limit=*" only returns * values.
// To see the format of the data you are retrieving, use the "Graph API
// Explorer" which is at https://developers.facebook.com/tools/explorer/
$likes = idx($facebook->api('/me/likes?limit=4'), 'data', array());
// This fetches 4 of your friends.
$friends = idx($facebook->api('/me/friends?limit=4'), 'data', array());
// And this returns 16 of your photos.
$photos = idx($facebook->api('/me/photos?limit=16'), 'data', array());
直到这里我有三个阵列“喜欢”“朋友”和“照片”,我可以通过将它们置于foreach循环并使用idx()方法(我不确定它的功能是什么)来轻松回应)像这样:
<section id="samples" class="clearfix">
<h1>Examples of the Facebook Graph API</h1>
<div class="list">
<h3>A few of your friends</h3>
<ul class="friends">
<?php
foreach ($friends as $friend) {
// Extract the pieces of info we need from the requests above
$id = idx($friend, 'id');
$name = idx($friend, 'name');
?>
<li>
<a href="https://www.facebook.com/<?php echo he($id); ?>" target="_top">
<img src="https://graph.facebook.com/<?php echo he($id) ?>/picture?type=square" alt="<?php echo he($name); ?>">
<?php echo he($name); ?>
</a>
</li>
当我尝试获取用户收件箱中的所有邮件时出现问题(请注意我有权获取它们),我使用以下代码来询问它们:
$messages = $facebook->api('/me/inbox');
但它似乎返回了很多嵌套数组,我无法从中获取任何可用的数据。 我一直在尝试使用foreach方法,就像其他方法一样,但每次回声的唯一方法就是“数组”。
刚才我在$ messages对象上使用了var_dump方法,它显示了数组的嵌套层次结构,但是,我不知道如何从那里获取信息,这里有一些回显的字符串:
array(25) { [0]=> array(6) { ["id"]=> string(13) "[idnumbersIcantshow]" ["to"]=> array(1) { ["data"]=> array(2) { [0]=> array(2) { ["name"]=> string(12) "MyName" ["id"]=> string(10) "idnumbersIcantshow" } [1]=> array(2) { ["name"]=> string(9) "TheOtherPersonName" ["id"]=> string(10) "idnumbersIcantshow" } } } ["updated_time"]=> string(24) "2012-12-31T05:35:28+0000" ["unread"]=> int(0) ["unseen"]=> bool(false) ["comments"]=> array(2) { ["data"]=> array(25) { [0]=> array(4) { ["id"]=> string(16) "idnumbersIcantshow" ["from"]=> array(2) { ["name"]=> string(9) "TheOtherPersonName" ["id"]=> string(10) "idnumbersIcantshow" } ["message"]=> string(24) "Message That Was Sent That I wont show" ["created_time"]=> string(24) "2012-10-13T04:11:24+0000" } [1]=> array(4) { ["id"]=> string(16) "idnumbersIcantshow" ["from"]=> array(2) { ["name"]=> string(9) "TheOtherPersonName" ["id"]=> string(10) "idnumbersIcantshow" } ["message"]=> string(53) "Another Message Sent which I wont show either" ["created_time"]=> string(24) "2012-10-13T04:11:41+0000" } [2]=> array(4) { ["id"]=> string(16) "idnumbersIcantshow" ["from"]=> array(2) { ["name"]=> string(12) "MyName" ["id"]=> string(10) "idnumbersIcantshow" } ["message"]=> string(45) "More private bla bla bla" ["created_time"]=> string(24) "2012-10-13T04:12:24+0000" } [3]=> array(4) { ["id"]=> string(16) .... and it keeps going with all my conversations
如何将这些信息转换为如下所示的非常简单的变量:
&conversationParticipantsArray
$messagesArray
{
$from
&message
}
我想主要检索那些数据。这是我不太有用的foreach代码:
foreach($messages as $message)
{
?>
<div id="messages">
<?php echo var_dump($message);?>
</div>
希望你能帮助我,谢谢你。