gpt4 book ai didi

php - Facebook graph api 照片上传到粉丝专页相册

转载 作者:可可西里 更新时间:2023-11-01 12:54:52 25 4
gpt4 key购买 nike

我已经获得了使用此代码的照片上传功能,

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

我希望这能有所帮助,我和一个 friend 用头撞墙花了很长时间才让这个工作成功!

无论如何,这是我的问题,我怎样才能将图片上传到粉丝页面?我正在努力让它工作,当我上传图片时,我得到的只是照片 ID,但相册中没有照片。

所以基本上,当用户点击我们应用程序上的上传按钮时,我需要将他们创建的图像上传到我们的粉丝页面的相册中,并在上面标记他们。

有人知道我怎样才能做到这一点吗?

最佳答案

按照简单的获取登录 url

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

这将使您可以在粉丝专页中发布照片和添加相册。获取访问 token

$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'] );

然后根据需要更改路径快乐编码

这段代码在这个网站上运行得很好....为什么投反对票。?如果您是粉丝专页的所有者,您可以将照片张贴到粉丝专页。我有该应用程序的工作副本。

关于php - Facebook graph api 照片上传到粉丝专页相册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3047031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com