9

我试图实现 FB 实时更新。我想我已经订阅了,但没有得到任何更新......这是我的代码:

用于获取 App 访问令牌

function authAsApp() {
    $config = array ('appId' => FB_APPID, 'secret' => FB_SECRET);
    $fb = new Facebook($config);
    Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;


    $args = array('grant_type' => 'client_credentials',
            'client_id' => APP_ID,
            'client_secret' =>  APP_SECRET);

    $ch = curl_init();
    $url = 'https://graph.facebook.com/oauth/access_token';
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    $returnValue = curl_exec($ch);
    curl_close($ch);

    $appAccessToken = substr($returnValue,  strpos($returnValue,"=")+1);

    var_dump($appAccessToken);

    return $appAccessToken;
}

订阅

function subscribeTo($access_token) {
    $param = array('access_token' => $access_token,
            'object' => 'user',
            'fields' => 'name, feed, likes',
            'callback_url' => 'http://mysite.com/realTimeVerification.php',
            'verify_token' => VERIF_STR
    );
    $config = array ('appId' => FB_APPID, 'secret' => FB_SECRET);
    $fb = new Facebook($config);
    Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;

    try {

    $subs = $fb->api('/'.APP_ID.'/subscriptions', 'POST', $param);
    var_dump($subs);
    } catch (\FacebookApiException $e) {
        echo $e->getCode()." ".$e->getType()." ".$e->getMessage()." ".$e->getFile()." ".$e->getLine()."\n";
    }
}

在名为 realTimeVerification.php 的回调文件中,我有

<?php
define('UPD_FILE', 'updates.log');
$method = $_SERVER['REQUEST_METHOD'];
global $log_file;

if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' && $_GET['hub_verify_token'] == 'BRComm') {
   echo $_GET['hub_challenge'];
   exit;
}
else if ($method == 'POST')
{
    $log_file=UPD_FILE;
    $updates = json_decode(file_get_contents("php://input"), true); 

    logToFile("updates =".print_r($updates));
}
function logToFile($message){
    global $log_file;

    $hdl = fopen($log_file, 'a') or die ("couldn't open log file");
    fwrite($hdl,$message."\n");
    fclose($hdl);
}
?>

订阅后,我在 Graph API Explorer 中检查它并获得:

{
  "data": [
    {
      "object": "user", 
      "callback_url": "http://mysite.com/realTimeVerification.php", 
      "fields": [
        "feed", 
        "name"
      ], 
      "active": true
    }
  ]
}

所以订阅了。但是,当我对已验证应用程序的用户进行更改时,日志中没有任何更改。我究竟做错了什么?

4

0 回答 0