Skip to content

Instantly share code, notes, and snippets.

@lawebfabric
Last active December 1, 2018 15:51
Show Gist options
  • Save lawebfabric/31a05c7d4d9a2acbe19648b1eed3d35c to your computer and use it in GitHub Desktop.
Save lawebfabric/31a05c7d4d9a2acbe19648b1eed3d35c to your computer and use it in GitHub Desktop.
MODX Snippet facebook Page Graph using API
<?php
//-----------------------------------------------------------
// fbPageGraph
//-----------------------------------------------------------
/*
* @author: Steeve from lawebfabric
* @website: http://www.lawebfabric.com
* @tutorial modx website: http://www.tutocms.fr/
* @description: Returns the number of fans for a facebook fanpage (via the graph api)
* @version 1.0.0 - 2018-11-22
*
* PROPERTIES
* page_id - the facebook id of your fanpage
* access_token - the app access token
*
* EXAMPLE USAGE
* [[!fbPageGraph]] OR [[!fbPageGraph? &pageURL=`https://www.facebook.com/nike/`]]
*
* ATTENTION
* If you copy this to a new snippet in MODX you need to create 2 properties
* access_token = access token from Facebook
* https://developers.facebook.com/tools/explorer/ SELECT APP AND GET ACCESS TOKEN FOR THE APP
* PAGEID?fields=cover,fan_count,picture.type(large),posts{created_time,description,full_picture,picture,message,is_published,is_expired,is_hidden,name,place,status_type,type},photos
* PAGEID?fields=about,category,cover,description,emails,fan_count,hours,link,location,picture{url,is_silhouette,cache_key,height,width},phone,name,videos,website
* PAGEID?fields=posts{created_time,description,full_picture,picture,message,is_published,is_expired,is_hidden,name,place,status_type,type}
* page_id = you Facebook page ID you can find it here https://findmyfbid.com/
* OR YOU CAN USE FACEBOOK PAGE URL like for Nike : https://www.facebook.com/nike/
* FACEBOOK REFERENCE PAGE:
* https://developers.facebook.com/docs/graph-api/reference/page/
* https://developers.facebook.com/docs/graph-api/reference/v3.2/page/feed
*
* fbPageGraph is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* fbPageGraph is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
*/
// Get the script properties page_id and access_token
// To get your Facebook page ID : https://findmyfbid.com/
$page_id = $scriptProperties['page_id'];
// To get an access token from your APP : https://developers.facebook.com/tools/explorer/
$access_token = $scriptProperties['access_token'];
// Get the script properties pageURL
$page_url = $modx->getOption('pageURL', $scriptProperties);
if(!isset($page_url)){
$page_url = explode('/', $page_url);
$page_url = $page_url[3];
}
// Verify if page id is set in the snippet properties
if(empty($page_id) && !isset($page_url)) {
$modx->log(modX::LOG_LEVEL_ERROR, '[fbPageLikes] missing required properties page_id OR script propertie pageURL!');
return;
}
// Verify if access token is set in the snippet properties
if(empty($access_token)) {
$modx->log(modX::LOG_LEVEL_ERROR, '[fbPageLikes] missing required properties access_token!');
return;
}
if(empty($page_id)){
$page_id = $page_url;
}
$url = 'https://graph.facebook.com/'.$page_id.'?access_token='.$access_token.'&fields=fan_count';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
$data = json_decode($result,true);
return $data['fan_count'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment