2012-05-09 1 views

risposta

17

Ecco il codice PHP. Devi metterlo nel tuo modello.

<ul> 
<?php 
//function to retrieve posts from facebook’s server 
function loadFB($fbID){ 
    $url = "http://graph.facebook.com/".$fbID."/feed?limit=3"; 
    // Update by MC Vooges 11jun 2014: Access token is now required: 
    $url.= '&access_token=YOUR_TOKEN|YOUR_ACCESS_SECRET';// * 

    //load and setup CURL 
    $c = curl_init($url); 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
    //get data from facebook and decode JSON 
    $page = json_decode(curl_exec($c)); 
    //close the connection 
    curl_close($c); 
    //return the data as an object 
    return $page->data; 
} 

/* Change These Values */ 
// Your Facebook ID 
$fbid = "190506416472588"; 
// How many posts to show? 
$fbLimit = 10; 
// Your Timezone 
date_default_timezone_set("America/Chicago"); 


/* Dont Change */ 
// Variable used to count how many we’ve loaded 
$fbCount = 0; 
// Call the function and get the posts from facebook 
$myPosts = loadFB($fbid); 


//loop through all the posts we got from facebook 
foreach($myPosts as $dPost){ 
    //only show posts that are posted by the page admin 
    if($dPost->from->id==$fbid){ 
     //get the post date/time and convert to unix time 
     $dTime = strtotime($dPost->created_time); 
     //format the date/time into something human readable 
     //if you want it formatted differently look up the php date function 
     $myTime=date("M d Y h:ia",$dTime); 
     ?> 
     <ul> 
      <li><?php echo($dPost->message) . $myTime; ?></li> 
     </ul> 
     <?php 
     //increment counter 
     $fbCount++; 
     //if we’ve outputted the number set above in fblimit we’re done 
     if($fbCount >= $fbLimit) break; 
    } 
} 
?> 
</ul> 

Due cose che devi fare per elaborare questo script.

  1. Assicurarsi che il server sia abilitato cURL

  2. Avrete modificare l'ID di Facebook nello script dal sottoscritto.

* È possibile ottenere il token di accesso in questo modo:

$token = 'https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&grant_type=client_credentials'; 
$token = file_get_contents($token); // returns 'accesstoken=APP_TOKEN|APP_SECRET' 
+6

Questo sembra non funzionare più - 'print_r ($ page);' prima della 'return' in' funzione loadFB() 'restituisce un errore che indica è necessario un token di accesso. In particolare, restituisce: 'stdClass Object ([error] => stdClass Object ([message] => È richiesto un token di accesso per richiedere questa risorsa. [tipo] => OAuthException [codice] => 104)) '. – DACrosby

+1

Ho effettuato un aggiornamento relativo al token accesso – Martijn

+0

cosa succede se il risultato è vuoto ?? – user3777827

14
  1. Login facebook
  2. Vai alla facebok sezione sviluppatori "Apps"
  3. Registrati nuova applicazione, è necessario solo per registrare una nuova app, tutti i dati aggiuntivi sono opzionali
  4. Copia il tuo ID app/chiave API e App Secret da quel sam e "Apps" sezione.
  5. Copia facebook.php e base_facebook.php file da repo al server
  6. Usa polymorphic query di api, per richiedere i contenuti muro da account facebook

    require 'facebook.php'; 
    $facebook = new Facebook(array(
        'appId' => 'YOUR_APP_ID', 
        'secret' => 'YOUR_APP_SECRET', 
    )); 
    
    $fbApiGetPosts = $facebook->api('/YOUR_FACEBOOK_ACCOUNT_ID/feed?limit=5'); 
    if (isset($fbApiGetPosts["data"]) && !empty($fbApiGetPosts["data"])) { 
        // display contents of $fbApiGetPosts["data"] array 
    } 
    

    Sostituire YOUR_APP_ID con il tuo ID app, YOUR_APP_SECRET con il tuo segreto app e YOUR_FACEBOOK_ACCOUNT_ID con l'account Facebook di destinazione, si desidera ottenere messaggi da.

La query polimorfica è essenzialmente percorso/URL. Maggiori informazioni all'interno di cui sopra citato facebook api reference docs.

Se il muro del tuo account facebook di destinazione è pubblico, non ti servirà nient'altro che questo, per vederli.

+0

Vedo uno schermo bianco. http://volos-minsk.by/facebook-test-stackoverflow-app.html –

+0

Questa dovrebbe essere la risposta verificata. – Philip

+0

Sembra funzionare bene - eccetto che obbliga l'avvio della sessione e poi ricevi Warning: session_start(): Impossibile inviare cookie di sessione - intestazioni già inviate da (... Questo deve essere chiamato come ajax dalla pagina in cui desideri l'ultimo post mostrato ad esempio? – trainoasis

2

Ho avuto problemi con la risposta di Okky qui, e ho trovato un possibile, anche se non il lavoro ideale intorno.

Utilizza un feed RSS del tuo muro Facebook, quindi analizzalo semplicemente con un lettore RSS di tua scelta.

https://www.facebook.com/feeds/page.php?format=rss20&id=YOUR_UNIQUE_ID

Here is a quick way to get your ID

+0

Feed non valido –

0

Quindi mescolare Okky e risposta Deele, che sia io dare una mano, è necessario terminare con qualcosa che sarà simile a questa. Ho anche aggiungere un tag di ancoraggio al link al post url:

<?php 
$fbApiGetPosts = $facebook->api('/YOUR_FACEBOOK_ACCOUNT_ID/feed?limit=5'); 
if (isset($fbApiGetPosts["data"]) && !empty($fbApiGetPosts["data"])) { 
    //loop through all the posts we got from facebook 
    foreach($fbApiGetPosts["data"] as $dPost){ 
     //only show posts that are posted by the page admin 
     if($dPost["from"]["id"]==$fbid){ 
      //get the post date/time and convert to unix time 
      $dTime = strtotime($dPost["created_time"]); 
      //format the date/time into something human readable 
      //if you want it formatted differently look up the php date function 
      $myTime=date("M d Y h:ia",$dTime); 
      ?> 
       <li><a href="<?php echo($dPost["link"]); ?>"> 
             <?php echo($dPost["message"]) . "<br>" . 
             $myTime; ?></a></li> 
      <?php 
     } 
    } 
} 
?> 
+0

Perché "

+0

Mancava il primo tag aperto e l'ultimo tag di chiusura ... Ho appena modificato la mia risposta per aggiungerli. –