php Facebook graph api 照片上传到粉丝专页相册
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3047031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Facebook graph api photo upload to a fan page album
提问by Odyss3us
I have gotten the photo upload function to work with this code,
我已经得到了照片上传功能来处理这个代码,
<?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);
}
}
?>
I hope this helps, a friend and I smashed our heads against a wall for quite some time to get this working!
我希望这会有所帮助,我和一个朋友把头撞在墙上很长一段时间才能让它发挥作用!
Anyways, here is my question, how can I upload a image to a fan page? I am struggling to get this working, when I upload the image all I get is the photo id but no photo in the album.
无论如何,这是我的问题,如何将图像上传到粉丝页面?我正在努力让它工作,当我上传图像时,我得到的只是照片 ID,但相册中没有照片。
So basically, when the user clicks the upload button on our application, I need it to upload the image they created to our fan page's album with them tagged on it.
所以基本上,当用户单击我们应用程序上的上传按钮时,我需要将他们创建的图像上传到我们的粉丝页面的相册,并在上面贴上标签。
Anyone know how I can accomplish this?
有谁知道我怎么能做到这一点?
采纳答案by gsharma
This seems to be a bug with the Graph API. See this http://forum.developers.facebook.com/viewtopic.php?id=59063(can't post links, yet)
这似乎是 Graph API 的一个错误。请参阅此http://forum.developers.facebook.com/viewtopic.php?id=59063(尚不能发布链接)
回答by Hyman
Follow the simple Get login url with
按照简单的获取登录网址
$data['url'] = $this->facebook->getLoginUrl(array('scope'=>'publish_stream,read_stream,user_likes,user_photos,manage_pages'));
$data['url'] = $this->facebook->getLoginUrl(array('scope'=>'publish_stream,read_stream,user_likes,user_photos,manage_pages'));
this will let you post photo and add album in fanpage. Get access token for
这将让您发布照片并在粉丝专页中添加相册。获取访问令牌
$accounts = $this->facebook->api('/'.$PAGE_ID.'?fields=access_token', 'get', array('access_token' => $access_token));
$page_access_token = $accounts['access_token'];
Create album with other detail
用其他细节创建相册
$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);
Upload photo to a particualar fanpage album
将照片上传到特定的粉丝专页相册
$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'] );
And change the path as you wish Happy coding
并随心所欲地更改路径 快乐编码
This code works aweesome in this site....why downvoted.?You can post photos to fanpage if you are owner of that page.I have a working copy of that application.
这段代码在这个网站上工作得很好....为什么被低估了。?如果你是那个页面的所有者,你可以将照片发布到粉丝专页。我有一个该应用程序的工作副本。

