0

Facebook PHP API 教程之后,我编写了一个函数,将链接发布到我的 facebook 页面。

这个页面我得到了如何生成页面访问令牌

正如您从提要页面描述中看到的那样,有一个backdated_time参数(仅适用于页面帖子,不适用于用户帖子)允许您选择过去的时间来回溯帖子的日期。

这是我的函数的主体:

require_once(dirname(__FILE__) . "/facebook-php-sdk/facebook-php-sdk-v4-4.0-dev/autoload.php");

use Facebook\FacebookSession;

use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;

function publishUrlToPage($url, $message = null, $backdated_time = null)
{       
    $client_id = 'xxxxxxx';
    $client_secrets = 'xxxxxxxx';

    $long_live_access_token = "xxxxx";

    // Configures and app ID and secret that will be used by default throughout the SDK (but can be overridden whenever necessary using parameters to other methods.
    FacebookSession::setDefaultApplication($client_id, $client_secrets);

    // You can also create a FacebookSession with an access token you've acquired through some other means, by passing it to the constructor.
    $session = new FacebookSession($long_live_access_token);

    // To validate the session:
    try
    {
        $session->validate();
    }
    catch (FacebookRequestException $ex)
    {
        // Session not valid, Graph API returned an exception with the reason.
        throw $ex;
    }
    catch (\Exception $ex)
    {
        // Graph API returned info, but it may mismatch the current app or have expired.
        throw $ex;
    }

    /*
     * ####################### POST #######################
     */

    $array = array(
        'link' => $url
    );

    // https://developers.facebook.com/docs/graph-api/reference/v2.1/page/feed

    if( ! is_null($message) )
        $array["message"] = $message;

    if( ! is_null($backdated_time) )
        $array["backdated_time"] = $backdated_time->format(DateTime::ISO8601);

    $request = new FacebookRequest($session, 'POST', '/me/feed', $array);

    try
    {
        $response = $request->execute()->getGraphObject();

        return $response->getProperty('id');
    }
    catch(FacebookRequestException $e)
    {
        throw $e;
    }   
}

所有功能都可以正常工作,使用 url 或使用 url+消息。

backdated_time似乎不起作用。

作为一个值,我使用了这样的 ISO 8601 2014-08-14T00:00:00+00:00,正如这篇文章中所建议的那样

当我用这个值打电话时,我得到这个错误:

["statusCode":"Facebook\FacebookRequestException":private]=>
  int(500)
  ["rawResponse":"Facebook\FacebookRequestException":private]=>
  string(87) "{"error":{"message":"An unknown error has occurred.","type":"OAuthException","code":1}}"
  ["responseData":"Facebook\FacebookRequestException":private]=>
  array(1) {
    ["error"]=>
    array(3) {
      ["message"]=>
      string(30) "An unknown error has occurred."
      ["type"]=>
      string(14) "OAuthException"
      ["code"]=>
      int(1)
    }
  }

还尝试了不同的日期格式

$array["backdated_time"] = 1408046503;  
$array["backdated_time"] = "2014-08-14T00:00:00";
$array["backdated_time"] = "2014-08-14 00:00:00";

但仍然不起作用。

我也直接从Graph API Explorer尝试过,并且日期参数(具有相同的值)可以正常工作(参见附图,此处为完整尺寸)

Facebook 图形 API 示例

这可能是与 PHP SDK 严格相关的问题吗?


编辑

经过几次尝试,我将问题限制在以下情况。使用参数linkbackdated_time一起,请求不起作用。使用 Graph API Explorer 也无法正常工作。

在此处输入图像描述

好在问题与 PHP SDK 没有严格的关系,所以我可以直接使用 Graph API Explorer 来发现问题

不好的是,现在我遇到了将这两个参数结合起来的问题

4

0 回答 0