View source
<?php
namespace Drupal\socialfeed\Services;
use Facebook\Facebook;
use Symfony\Component\HttpFoundation\Response;
class FacebookPostCollector {
protected $fields = [
'permalink_url',
'message',
'created_time',
'picture',
'status_type',
];
protected $appId;
protected $appSecret;
protected $userToken;
private $facebook;
private $pageName;
public function __construct($appId, $appSecret, $userToken = NULL, $pageName = NULL, $facebook = NULL) {
$this->appId = $appId;
$this->appSecret = $appSecret;
$this->userToken = $userToken;
$this->pageName = $pageName;
$this->facebook = $facebook;
$this
->setFacebookClient();
}
public function setFacebookClient() {
if (NULL === $this->facebook) {
$this->facebook = new Facebook([
'app_id' => $this->appId,
'app_secret' => $this->appSecret,
'default_access_token' => $this
->defaultAccessToken(),
'default_graph_version' => 'v4.0',
]);
}
}
public function getPosts($page_name, $post_types, $num_posts = 10) {
$posts = [];
$post_count = 0;
$url = $page_name . $this
->getFacebookFeedUrl($num_posts);
do {
$response = $this->facebook
->get($url);
$url = NULL;
if ($response
->getHttpStatusCode() == Response::HTTP_OK) {
$data = json_decode($response
->getBody(), TRUE);
$posts = array_merge($this
->extractFacebookFeedData($post_types, $data['data']), $posts);
$post_count = count($posts);
if ($post_count < $num_posts && isset($data['paging']['next'])) {
$url = $data['paging']['next'];
}
}
} while ($post_count < $num_posts && NULL != $url);
return array_slice($posts, 0, $num_posts);
}
protected function extractFacebookFeedData($post_types, array $data) {
$posts = array_map(function ($post) {
return $post;
}, $data);
if (TRUE == is_string($post_types)) {
$posts = array_filter($posts, function ($post) use ($post_types) {
return $post['status_type'] === $post_types;
});
return $posts;
}
return $posts;
}
protected function defaultAccessToken() {
$config = \Drupal::service('config.factory')
->getEditable('socialfeed.facebooksettings');
$permanent_token = $config
->get('page_permanent_token');
if (empty($permanent_token)) {
$args = [
'usertoken' => $this->userToken,
'appid' => $this->appId,
'appsecret' => $this->appSecret,
'pageid' => $this->pageName,
];
$client = \Drupal::httpClient();
$request = $client
->request('GET', "https://graph.facebook.com/v4.0/oauth/access_token?grant_type=fb_exchange_token&client_id={$args['appid']}&client_secret={$args['appsecret']}&fb_exchange_token={$args['usertoken']}");
$request = json_decode($request
->getBody()
->getContents());
$long_token = $request->access_token;
$request = $client
->request('GET', "https://graph.facebook.com/v4.0/me?access_token={$long_token}");
$request = json_decode($request
->getBody()
->getContents());
$account_id = $request->id;
$request = $client
->request('GET', "https://graph.facebook.com/v4.0/{$args['pageid']}?fields=id&access_token={$long_token}");
$request = json_decode($request
->getBody()
->getContents());
$page_id = $request->id;
$request = $client
->request('GET', "https://graph.facebook.com/v4.0/{$account_id}/accounts?access_token={$long_token}");
$request = json_decode($request
->getBody()
->getContents());
foreach ($request->data as $response_data) {
if ($response_data->id == $page_id) {
$config
->set('page_permanent_token', $response_data->access_token)
->save();
return $response_data->access_token;
}
}
}
else {
return $permanent_token;
}
}
protected function getFacebookFeedUrl($num_posts) {
return '/feed?fields=' . implode(',', $this->fields) . '&limit=' . $num_posts;
}
}