在Facebook PHP API Tutorial之后,我写了一个函数,发布到我的Facebook页面的链接 .
从this page我得到了如何生成页面访问令牌
正如您在feed page description中看到的那样,有一个 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
,如this post中所述
当我使用此值进行调用时,我收到此错误:
["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和日期参数(具有相同的值)工作没有问题(参见附图,here为完整大小)
这可能是与PHP SDK严格相关的问题吗?
编辑
经过多次尝试,我将问题限制在以下情况 . 同时使用参数 link
和 backdated_time
,请求不起作用 . 使用Graph API资源管理器不起作用 .
好处是问题与PHP SDK没有严格的关系,所以我可以直接使用Graph API Explorer来发现问题
不好的是,现在我遇到了这两个参数的结合问题