1

我正在尝试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> 

上传在这里处理:

<?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;
    }
}
4

2 回答 2

2

参考:https ://developers.facebook.com/docs/authentication/pages/

  1. 授予manage_pages您的应用程序的权限。
  2. 获取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" }
?>
于 2012-07-10T16:54:15.783 回答
2

这就是我作为页面发布的方式。我正在使用Facebook 的 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);
于 2012-07-10T20:01:42.447 回答