2012-11-26 27 views
0

我試圖利用Facebook的實時更新API ...我有以下代碼:Facebook的實時資訊提供

<?php 

define('VERIFY_TOKEN', '*'); 
$method = $_SERVER['REQUEST_METHOD']; 

if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' && $_GET['hub_verify_token'] == VERIFY_TOKEN) { 
    echo $_GET['hub_challenge']; 
} else if ($method == 'POST') { 

    $hostname = "*"; 
    $username = "*"; 
    $dbname = "*"; 
    $password = "*"; 

    mysql_connect($hostname, $username, $password) OR DIE('Unable to connect to database! Please try again later.'); 
    mysql_select_db($dbname) or die(mysql_error()); 

    $post_body = file_get_contents("php://input"); 
    $object = json_decode($post_body, true); 

    foreach ($object->entry as $update) { 
     // For each entry in notification, insert a row of information into the table "FaceBook" 
     $changed_fields = $update->changed_fields[0]; 
     $result = mysql_query("INSERT INTO FaceBook (uid, id, time, changed_fields) VALUES('$update->uid', '$update->id', '$update->time', '$changed_fields')") 
       or die(mysql_error()); 
    } 
    mysql_close(); 
} 
?> 

現在,當更新發生,Facebook是發送POST請求,但在foreach循環沒有被輸入...我試圖檢查$對象是否爲空,但它返回2個元素(使用計數)..

任何想法?

回答

0

還有一些問題。

每個對象都有多個條目。我相信每個條目都可以有一些改變。所以「變化」也是一個像條目一樣的數組。

試試這個:

foreach($object->entry as $update){ 

    foreach($update->changes as $change){ 

     //access information about the change that occured on the page 

    } 
} 
相關問題