2009-08-19 7 views
5

Ho notato che quando visito siti Web di stampa fotografica, ti chiedono se desideri importare le tue foto da Facebook. Come lo fanno? Facebook fornisce API per importare le foto degli utenti?Come importare foto da Facebook?

Principalmente uso PHP.

risposta

0

Quando l'ho fatto, hanno effettuato l'accesso per mio conto (ho inserito il mio utente/password) e ho spostato gli album di foto secondo l'HTML previsto.

C'erano alcune cose legali che dovevo "accettare" e mi è stato comunicato cosa avrebbero fatto con i miei dettagli.

1

Certo, è possibile recuperare le foto dagli utenti che hanno aggiunto la propria applicazione e accettato di condividere tali informazioni. Per prima cosa recuperi gli album usando la chiamata API photos_getAlbums, quindi puoi eseguire il loop sugli id ​​degli album e chiamare photos_get per recuperare le foto per gli album.

 
    /** 
    * 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; 
} 

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; 
} 
2

get_albums restituisce un array di dischi, fare un var_dump per vedere cosa restituisce.


$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/