我正在使用Facebook PHP SDK v5来发布我管理的页面。发布指向页面的链接时,下面的代码非常有用。但是,当我尝试修改此代码以删除帖子时,它会一直返回错误:
图表返回了页面请求错误:未知路径组件:/ xxxxxxx_xxxxxxxxx
xxxxxxx_xxxxxxxxx是我要删除的postID
我无法弄清楚如何调整我的工作代码来删除帖子而不是发布它们。有谁知道如何使这项工作?
将帖子发布到网页的工作代码:
<?php
function facebook(){
include_once $_SERVER['DOCUMENT_ROOT']."/Facebook/autoload.php";
$permissions = ['manage_pages', 'publish_pages'];
$callback = "https://www.my-domain.com/my-callback-url.html?fb=redirect";
$pageID = 'xxxxxxxxxxxx';
$linkToPost = 'https://www.test-post.com';
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.10',
]);
$helper = $fb->getRedirectLoginHelper();
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$accessToken = $helper->getAccessToken();
}
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$res['error'] = 'Error: Graph returned a session error: ' . $e->getMessage();
return $res;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$res['error'] = 'Error: Facebook SDK returned a session error: ' . $e->getMessage();
return $res;
}
if (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
// getting short-lived access token
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// setting default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// getting basic info about user
try {
$profile_request = $fb->get('/me');
$profile = $profile_request->getGraphNode();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$res['error'] = 'Error: Graph returned a profile request error: ' . $e->getMessage();
session_destroy();
return $res;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$res['error'] = 'Error: Facebook SDK returned a profile request error: ' . $e->getMessage();
return $res;
}
$response = $fb->get('/me/accounts', (string) $accessToken);
foreach ($response->getDecodedBody() as $allPages) {
foreach ($allPages as $page ) {
if (isset($page['id']) && (string)$page['id'] == $pageID) {
$pageAccessToken = (string) $page['access_token'];
break;
}
}
}
try{
$post = $fb->post('/'.$pageID.'/feed', array('link' => $linkToPost), $pageAccessToken);
$post = $post->getGraphNode()->asArray();
$res['success'] = "Successfully Posted To Facebook. The ID of this post is: ".$post['id'];
return $res;
}catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$res['error'] = 'Error: Graph returned a page request error: ' . $e->getMessage();
session_destroy();
return $res;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$res['error'] = 'Error: Facebook SDK returned a page request error: ' . $e->getMessage();
return $res;
}
}else{
$loginUrl = $helper->getLoginUrl($callback, $permissions);
$res['login'] = $loginUrl;
return $res;
}
}
?>
我尝试修改上面的工作代码以删除帖子。不工作:
基本上我改变了:
$post = $fb->post('/'.$pageID.'/feed', array('link' => $linkToPost), $pageAccessToken);
要:
$post = $fb->delete('/'.$pageID.'/feed/'.$postIDtoDelete ,array(), $pageAccessToken);
代码:
<?php
function facebook(){
include_once $_SERVER['DOCUMENT_ROOT']."/Facebook/autoload.php";
$permissions = ['manage_pages', 'publish_pages'];
$callback = "https://www.my-domain.com/my-callback-url.html?fb=redirect";
$pageID = 'xxxxxxxxxxxx';
$postIDtoDelete = 'xxxxxxx_xxxxxxxxx';
$linkToPost = 'https://www.test-post.com';
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.10',
]);
$helper = $fb->getRedirectLoginHelper();
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$accessToken = $helper->getAccessToken();
}
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$res['error'] = 'Error: Graph returned a session error: ' . $e->getMessage();
return $res;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$res['error'] = 'Error: Facebook SDK returned a session error: ' . $e->getMessage();
return $res;
}
if (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
// getting short-lived access token
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// setting default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// getting basic info about user
try {
$profile_request = $fb->get('/me');
$profile = $profile_request->getGraphNode();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$res['error'] = 'Error: Graph returned a profile request error: ' . $e->getMessage();
session_destroy();
return $res;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$res['error'] = 'Error: Facebook SDK returned a profile request error: ' . $e->getMessage();
return $res;
}
$response = $fb->get('/me/accounts', (string) $accessToken);
foreach ($response->getDecodedBody() as $allPages) {
foreach ($allPages as $page ) {
if (isset($page['id']) && (string)$page['id'] == $pageID) {
$pageAccessToken = (string) $page['access_token'];
break;
}
}
}
try{
$post = $fb->delete('/'.$pageID.'/feed/'.$postIDtoDelete ,array(), $pageAccessToken);
$res['success'] = "Successfully Deleted Posted From Facebook. Response is:".$post;
return $res;
}catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
$res['error'] = 'Error: Graph returned a page request error: ' . $e->getMessage();
session_destroy();
return $res;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
$res['error'] = 'Error: Facebook SDK returned a page request error: ' . $e->getMessage();
return $res;
}
}else{
$loginUrl = $helper->getLoginUrl($callback, $permissions);
$res['login'] = $loginUrl;
return $res;
}
}
?>
答案 0 :(得分:1)
没关系,我只需编辑:
$post = $fb->delete('/'.$pageID.'/feed/'.$postIDtoDelete ,array(), $pageAccessToken);
要:
$post = $fb->delete('/'.$postIDtoDelete ,array(), $pageAccessToken);
现在正常工作