Facebook图形api照片上传到粉丝页面相册

时间:2010-06-15 16:21:07

标签: php facebook

我已经使用照片上传功能来处理此代码,

<?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 个答案:

答案 0 :(得分:4)

按照简单的使用

获取登录网址
  

$ data ['url'] =   $这 - &GT; facebook-&GT; getLoginUrl(阵列( '范围'=&GT; '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'] );

并根据需要更改路径 快乐的编码

此代码在此网站中运行起来很有用....为什么downvoted。如果您是该页面的所有者,可以将照片发布到fanpage。我有该应用程序的工作副本。

答案 1 :(得分:2)

这似乎是Graph API的一个错误。请参阅此http://forum.developers.facebook.com/viewtopic.php?id=59063(无法发布链接)