我正在尝试上传图片并使用this tutorial以page
张贴到我的粉丝专页墙上
但是连接似乎不起作用。
任何建议如何获取页面参数,以便我可以将图像发布到页面墙
此代码已从最新的SDK 3.1.1进行了修改
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '***************',
'secret' => '********************************',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$page_id = '***************';
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => "I'm a Page!"
);
$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(array( 'next' => 'http://myDoamin.com/logout_page.php' ));
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body { font-family: 'Lucida Grande', Verdana, Arial, sans-serif; }
h1 a { text-decoration: none; color: #3b5998; }
h1 a:hover { text-decoration: underline; }
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($page_info); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
</body>
</html>
在这里上传handeled:
<?php
$app_id = "***************";
$app_secret = "********************************";
$post_login_url = "http://mydomain.com/upload.php";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url )
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/me/photos?"
. "access_token=" .$access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url .' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message"
type="text" value=""><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
echo '</body></html>';
}
?>
来自SDK
的示例是类似的,当我运行它时,它作为用户工作。但不是作为页面
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
答案 0 :(得分:2)
请参阅:https://developers.facebook.com/docs/authentication/pages/
manage_pages
权限。page_access_token
,将api帖子设为页面标记为页面。 示例: php cURL upload
:独立示例,不需要sdk。
<?php
function GetCH(){
// id of the album to upload
$ablum_id = "182325958494028";
// photo caption
$photo_caption = "testing app ablum upload";
// the page access token if you are uploading as the page
$page_access_token = "Add Your page token here.";
// url to your photo
$photo_url = "http://sphotos.xx.fbcdn.net/hphotos-ash3/544288_10150906170859577_732484576_9506566_417314835_n.jpg";
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/".$ablum_id."/photos?url=".urlencode($photo_url)."&message=".urlencode($photo_caption)."&method=post&access_token=".$page_access_token."");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$sendCH = curl_exec($ch);
curl_close($ch);
return $sendCH;
};
// call our function and upload photo, large photos may be slow
echo GetCH();
// if successful a return is shown
// { "id": "387851721274783", "post_id": "171535666239724_387851741274781" }
?>
答案 1 :(得分:2)
这是我作为一个页面发布的方式。我正在使用Facebook's PHP SDK。
我首先获取用户有权访问的所有页面的列表,然后我向用户显示它们的列表,以便他们可以选择一个(此处未显示的代码)。然后我存储该数据,稍后再使用它作为该页面发布。
我如何获得页面列表:
try {
$result = $facebook->api("/me/accounts");
$userpages = $result['data'];
} catch (FacebookApiException $e) {
$userpages = array();
$fbuser = null;
$facebook->clearPersistentData();
}
现在我遍历它们($settings
是一个包含我用户设置的数组,包括他们选择发布的页面):
foreach ($userpages as $userpage) {
if ($userpage['category'] == "Application")
continue; # My app doesn't work with Application pages.
if ($settings['page']['id'] == $userpage['id']) {
# This is the page I want.
# $userpage['access_token'] has the access token.
}
}
access_token
被添加到我的请求postdata数组中,此处使用了id
。
return $facebook->api("/$id/feed/", "post", $postdata);