我已经阅读了很多关于这个主题的其他主题,但我没有找到有用的东西。
我想做什么
当我在我的网站上添加文章时,我想在Twitter和Facebook上发布更新。它适用于Twitter,但我遇到了Facebook的问题。
我下载了使用OAuth的facebook.php。
我的问题
当我发布一个简单的文本时,它工作正常,它显示为页面所需的发布。但是,当我想发布带有缩略图,链接,标题和说明的文本时,它就像我的个人帐户将此更新发布到我的页面墙上一样。
这是我的简单文本代码(我在上面请求了acces_token):
$post = array('access_token' => $token, 'message' => 'My message');
try{
$res = $facebook->api('/mypage/feed','POST',$post);
print_r($res);
} catch (Exception $e){
echo $e->getMessage();
}
这是错误的代码:
$post = array('access_token' => $token,
'message' => 'My message',
'picture' => 'http://www.website.com/picture.jpg',
'link' => 'http://www.website.com',
'caption' => 'test caption',
'description' => 'test description',
'from' => array('name' =>'Page name', 'id' => 'page id'),
);
try{
$res = $facebook->api('/mypage/feed','POST',$post);
print_r($res);
} catch (Exception $e){
echo $e->getMessage();
}
Facebook API没有很好的文档,但我到处搜索不要问你这个问题..但我找不到任何解决方案。
非常感谢帮助我。
本杰明
答案 0 :(得分:1)
希望您拥有以下权限(publis_stream,manage_pages,offline_access)和access_token,请尝试以下代码
<?php
/**
* Edit the Page ID you are targeting
* And the message for your fans!
*/
$page_id = 'PAGE_ID';
$message = "I'm a Page!";
/**
* This code is just a snippet of the example.php script
* from the PHP-SDK <http://github.com/facebook/php-sdk/blob/master/examples/example.php>
*/
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'app_id',
'secret' => 'app_secret',
));
// Get User ID
// $user supposed to be page admin
$user = $facebook->getUser();
if ($user) {
try {
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => $message ,
'name' => 'My Wall Post Header/Title Here',
'caption' => 'Small caption here',
'link' => 'http://www.mywebsite.org',
'description' => 'Wall Post Details Here',
'picture' => "http://www.mywebsite.org/images/logo.gif",
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}
?>
答案 1 :(得分:0)
您需要在网页上实施一些Open Graph og:tags
,以便Facebook知道要拍摄的图片,说明和标题。
https://developers.facebook.com/docs/opengraphprotocol/
他们看起来像这样 -
<meta property="og:title" content="The Rock"/>
<meta property="og:type" content="movie"/>
<meta property="og:url" content="http://www.imdb.com/title/tt0117500/"/>
<meta property="og:image" content="http://ia.media-imdb.com/rock.jpg"/>
<meta property="og:site_name" content="IMDb"/>
<meta property="fb:admins" content="USER_ID"/>
<meta property="og:description"
content="A group of U.S. Marines, under command of
a renegade general, take over Alcatraz and
threaten San Francisco Bay with biological
weapons."/>
一旦您实现了所需的元标记,您就可以使用方便的Facebook URL Debugger来测试您的工作。它会告诉你是否有问题,它们到底是什么,以及如何解决它们。
答案 2 :(得分:0)
这可能不是它,但是,确保您没有使用解析为内部域名的图片URL(即通过更新本地主机文件)。我经过大量的测试后发现(虽然它在JS中使用FB.UI调用),如果域名指向内部,即使它也可以在外部使用(这是让我感到震惊的一点) ),你可能会得到错误。这似乎只对Picture参数有用。
祝你好运!