好的,事情就是这样,我整天都在搜索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 方法,就像其他方法一样,但它唯一呼应的是每次都是“Array”。
刚才我在 $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>
希望你能帮助我,谢谢。