I noticed when I visit photo printing websites, they ask you if you would like to import your photos from Facebook. How do they do it? Does Facebook provide API to import us
get_albums returns an array of albums, do a var_dump to see what it returns.
$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;
}
See: http://www.joeyrivera.com/2010/facebook-graph-api-app-easy-w-php-sdk/
Click this below link and go through my comment. Perhaps it will help someone :)
How to import photos from facebook into a website using php?
When I did it they logged in on my behalf (I put in my user/password) and navigated photo albums by expected HTML.
There were some legal things I had to "accept" and was notified of what they would do with my details.
/**
* 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;
}
Sure you can retrieve photos from users that have added you application and agreed to share such information. First you retrieve the albums using the photos_getAlbums API call, then you can loop over album ids and call photos_get to retrieve photos for the albums.
/**
* 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;
}