我想实现从我的数据库到我的Facebook墙上的简单邮件发布。使用php-SDK:
require_once 'lib/facebook.php';
$appID = 'xxx';
$secret = 'yyy';
$facebook = new Facebook(array(
'appId' => $appID,
'secret' => $secret,
));
$user = $facebook->getUser();
if (empty($user)) {
header("Location: ". $facebook->getLoginUrl(array(
'req_perms' => 'publish_stream',
)));
exit();
}
$params = array(
'message' => 'Hello, every one!',
);
$post_id = $facebook->api('/'. $user .'/feed', 'post', $params);
我有“Uncaught OAuthException:(#200)用户尚未授权应用程序执行此操作...”。
如果我没有登录,那么在运行此脚本时,会重定向到facebook登录。 Сonfirm没有给出数据的使用。
答案 0 :(得分:4)
这是一个工作片段,但请确保您已下载最新的PHP SDK,即3.0.1 如果您还没有,请从此处下载:https://github.com/facebook/php-sdk/
如果你仔细阅读下面的代码,你几乎可以理解它的作用,所以我认为我不必再向你解释一下了吗?
<?php
// Requires Facebook PHP SDK 3.0.1: https://github.com/facebook/php-sdk/
require ('facebook.php');
define('FACEBOOK_APP_ID',"YOUR-APP-ID");
define('FACEBOOK_SECRET',"YOUR-APP-API-SECRET");
$user = null;
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.
if($user == 0) {
/**
* Get a Login URL for use with redirects. By default, full page redirect is
* assumed. If you are using the generated URL with a window.open() call in
* JavaScript, you can pass in display=popup as part of the $params.
*
* The parameters:
* - redirect_uri: the url to go to after a successful login
* - scope: comma separated list of requested extended perms
*/
$login_url = $facebook->getLoginUrl($params = array('scope' => "publish_stream"));
echo ("<script> top.location.href='".$login_url."'</script>");
} else {
try {
$params = array(
'message' => "Hurray! This works :)",
'name' => "This is my title",
'caption' => "My Caption",
'description' => "Some Description...",
'link' => "http://stackoverflow.com",
'picture' => "http://i.imgur.com/VUBz8.png",
);
$post = $facebook->api("/$user/feed","POST",$params);
echo "Your post was successfully posted to UID: $user";
}
catch (FacebookApiException $e) {
$result = $e->getResult();
}
}
?>