我注意到,当我访问照片打印网站时,他们会询问您是否要从Facebook导入照片。他们是如何做到的呢? Facebook是否提供API来导入用户的照片?
我主要使用PHP。
答案 0 :(得分:2)
/**
* get_albums()
*
* @param long $uid
* @return array
*/
function get_albums($uid=null)
{
if (empty($uid))
$uid = $_REQUEST['fb_sig_user'];
try
{
return $facebook->api_client->photos_getAlbums($uid,null);
}
catch (FacebookRestClientException $ex)
{
return array();
}
}
/**
* get_photos()
*
* @param bool $bool_pids
* @param mixed $aids (array of album ids or null)
* @return array
*/
function get_photos($bool_pids=true, $aids=null, $pids=null)
{
try
{
$p = $facebook->api_client->photos_get(null, $aids, $pids);
}
catch (FacebookRestClientException $ex) { }
if ($bool_pids)
{
$pids = array();
if (!empty($p))
foreach($p as $p0)
$pids[] = $p0['pid'];
return $pids;
}
else
return $p;
}
答案 1 :(得分:2)
get_albums返回一个专辑数组,执行var_dump以查看它返回的内容。
$albums = get_albums($facebook);
foreach($albums as $album)
{
if($album["count"] > 0)
{
//if the album has pictures, then do something with the album
}
}
function get_albums($facebook)
{
$fb_user = getFbUser($facebook);
$myalbums = $facebook->api('/me/albums');
return $myalbums["data"];
}
function getFbUser($facebook)
{
$fb_user = $facebook->getUser(); //gets user id
if(is_null($fb_user))
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
exit;
}
return $fb_user;
}
function getFb()
{
$facebook = new Facebook(array(
'appId' => 'your_appid',
'secret' => 'your_secret',
'cookie' => true,
));
return $facebook;
}
请参阅:http://www.joeyrivera.com/2010/facebook-graph-api-app-easy-w-php-sdk/
答案 2 :(得分:1)
当然,您可以从添加了应用程序并同意共享此类信息的用户检索照片。首先使用photos_getAlbums API调用检索相册,然后您可以循环播放相册ID并调用photos_get来检索相册的照片。
/**
* get_albums()
*
* @param long $uid
* @return array
*/
function get_albums($uid=null)
{
if (empty($uid))
$uid = $_REQUEST['fb_sig_user'];
try
{
return $facebook->api_client->photos_getAlbums($uid,null);
}
catch (FacebookRestClientException $ex)
{
return array();
}
}
/**
* get_photos()
*
* @param bool $bool_pids
* @param mixed $aids (array of album ids or null)
* @return array
*/
function get_photos($bool_pids=true, $aids=null, $pids=null)
{
try
{
$p = $facebook->api_client->photos_get(null, $aids, $pids);
}
catch (FacebookRestClientException $ex)
{
}
if ($bool_pids)
{
$pids = array();
if (!empty($p))
foreach($p as $p0)
$pids[] = $p0['pid'];
return $pids;
}
else
return $p;
}
答案 3 :(得分:0)
当我这样做时,他们代表我登录(我输入了我的用户名/密码)并按预期的HTML导航了相册。
我必须“接受”一些合法的事情,并被告知他们会对我的详细信息做些什么。
答案 4 :(得分:0)
点击以下链接并浏览我的评论。也许它会帮助某人:)
How to import photos from facebook into a website using php?