Facebook图谱API上传照片到粉丝页相册

7
我已经使用这段代码使照片上传功能正常工作,
<?php

include_once 'facebook-php-sdk/src/facebook.php';
include_once 'config.php';//this file contains the secret key and app id etc...

$facebook = new Facebook(array(
    'appId'  => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET_KEY,
    'cookie' => true,
    'domain' => 'your callback url goes here'
));

$session = $facebook->getSession();

if (!$session) {

    $url = $facebook->getLoginUrl(array(
               'canvas' => 1,
               'fbconnect' => 0,
               'req_perms'=>'user_photos,publish_stream,offline_access'//here I am requesting the required permissions, it should work with publish_stream alone, but I added the others just to be safe
           ));

    echo 'You are not logged in, please <a href="' . $facebook->getLoginUrl() . '">Login</a> to access this application';

} else{

    try {

        $uid = $facebook->getUser();
        $me = $facebook->api('/me');
        $token = $session['access_token'];//here I get the token from the $session array
        $album_id = 'the id of the album you wish to upload to eg: 1122';

        //upload your photo
        $file= 'test.jpg';
        $args = array(
        'message' => 'Photo from application',
        );
        $args[basename($file)] = '@' . realpath($file);

        $ch = curl_init();
        $url = 'https://graph.facebook.com/'.$album_id.'/photos?access_token='.$token;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
        $data = curl_exec($ch);

        //returns the id of the photo you just uploaded
        print_r(json_decode($data,true));

    } catch(FacebookApiException $e){
        echo "Error:" . print_r($e, true);
    }
}
?>

我希望这可以帮助,我和一个朋友曾经头痛了相当长的一段时间才使这个工作正常运行!无论如何,这是我的问题,我如何在粉丝页面上上传图片?我正在努力让它工作,但当我上传图片时,我只得到了照片ID,而没有照片在相册中。
因此,当用户在我们的应用程序上点击上传按钮时,我需要将他们创建的图像上传到我们的粉丝页面相册,并在其中标记他们。有人知道我该如何实现这一点吗?
2个回答

4

跟着以下简单步骤获取登录网址:

$data['url'] = $this->facebook->getLoginUrl(array('scope'=>'publish_stream,read_stream,user_likes,user_photos,manage_pages'));

这将允许您在粉丝页上发布照片和添加相册。

获取访问令牌:

$accounts = $this->facebook->api('/'.$PAGE_ID.'?fields=access_token', 'get', array('access_token' => $access_token));
$page_access_token = $accounts['access_token'];

创建带有其他详细信息的相册。
            $album_details = array(
                 'message'=> 'Test album',
               'name'=> $today ,//should be unique each time,
            'access_token'=>$page_access_token
            );
            $album_created = $this->facebook->api('/'.$PAGE_ID.'/albums', 'post', $album_details);

上传照片到特定的粉丝专页相册
$photo = $this->facebook->api('/'.$album_id.'/photos', 'POST', array(
                                         'source' => "@"."@".realpath(BASEPATH."../".$photopath),
                                         'message' => 'new photo',
                        'access_token' => $page_access_token,
                        'no_story' => 0
                                         )
                                      );

                //print_r( $album_created['id'] );

将路径更改为您所需的内容 愉快的编码

这段代码在这个网站上运行得非常好……为什么会被踩?如果您是该页面的所有者,您可以将照片发布到粉丝页面。我有一个可用的应用程序副本。


1
可能是因为他们不希望所有用户都成为该页面的所有者 ;) - dwery
亲爱的,你能否给我发送一个可用的副本来创建相册并上传多张照片。需要帮助。谢谢。 - Desert_king

2

啊,该死...还有其他的尝试将照片链接到粉丝页面的方法吗?上传照片时是否有办法对它们进行标记? - Odyss3us
目前还没有基于页面进行身份验证的方法。但是FB将在周二发布一个修复程序来解决这个问题。我希望这个修复程序也能创建上传照片到页面的权限。 http://bugs.developers.facebook.com/show_bug.cgi?id=10005(请查看特定的评论#85和#98) - gsharma
他们更新了代码。现在可以为粉丝页面创建相册,但是你猜怎么着 - 你不能上传图片 :) 请参见评论#121、122、128 http://bugs.developers.facebook.com/show_bug.cgi?id=10005 - gsharma
2
好吧,这真是太棒了,他们又失败了!我似乎比他们更了解他们自己的API,许多开发人员因为这样的问题而苦苦挣扎,我真诚地希望Facebook能尽快整顿自己,这真是不可接受的! - Odyss3us
我刚刚尝试了一下,似乎仍然存在问题。有没有什么解决方法? - sesmic
我已经解决了这个问题,请检查答案。我给上面的回答投了反对票,因为它是错误的线索。 - jack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接