Skip to content

Instantly share code, notes, and snippets.

@sr2ds
Last active May 7, 2016 20:00
Show Gist options
  • Save sr2ds/b8385da39512ef8903b3b601782b0c5b to your computer and use it in GitHub Desktop.
Save sr2ds/b8385da39512ef8903b3b601782b0c5b to your computer and use it in GitHub Desktop.
Get Facebook Group Posts
/*
* @Author: David Silva
* @Description: This class makes the connection with facebook and get the posts of the specified group
* @Required: Facebook PHP SDK, Class Model for pass parameters
*
*\
class FBOverFlow {
function pegaPosts($grupoId) {
require_once __DIR__ . '/Facebook/autoload.php';
require_once 'Model/Posts.php';
$posts = new Posts();
$fb = new Facebook\Facebook([
'app_id' => 'APPID',
'app_secret' => 'APPSECRET',
'default_graph_version' => 'v2.5',
]);
$fb->setDefaultAccessToken('FacebookToken');
try {
$response = $fb->get('/'.$grupoId.'/feed?limit=30');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$body = $response->getBody();
$totalArray = count(json_decode($body)->data);
$cont = json_decode($body);
$conteudo = $cont->data;
$paginacao = $cont->paging;
for ($i = 0; $i < $totalArray; $i ++) {
echo "<br>Postagem: ".$conteudo[$i]->id ."<br>";
echo "<br>Mensagem: ".$conteudo[$i]->message ."<br>";
echo "<br>Horario: ".$conteudo[$i]->updated_time ."<br><br>";
if ($conteudo[$i] != null) {
print_r($posts->inserePost($conteudo[$i]->id, $grupoId, $conteudo[$i]->message, $conteudo[$i]->updated_time));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment