Created
March 11, 2016 14:31
-
-
Save lukasfarina/65a51e5c7ff97e2f23c9 to your computer and use it in GitHub Desktop.
Classe para acessar OpenGraph do Facebook usando app e secret id e pegar os últimos posts de uma página.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* FacebookMicroSDK | |
* @Author: Lucas Farina | |
* @Author URI: http://www.lucasfarina.com.br | |
* @Version 0.0.1 | |
* */ | |
class FacebookMicroSDK { | |
/* | |
* Initialize Variables | |
* */ | |
private $fbid; | |
private $fbsecret; | |
private $accessToken; | |
function __construct() { | |
$this->fbid = "920685888038410"; | |
$this->fbsecret = "b4f41c61af9327373a353bfbc61881ff"; | |
$this->accessToken = $this->getAccessToken($this->fbid, $this->fbsecret); | |
} | |
/* | |
* Get Page ID | |
* @Return Integer | |
* @Params $pageNickname, $accessToken | |
* */ | |
public function getPageId($pageNickname) { | |
// GET | |
$url = "https://graph.facebook.com/?ids=http://facebook.com/{$pageNickname}&fields=id&{$this->accessToken}"; | |
$request = file_get_contents($url); | |
$arrayDecoded = json_decode($request, true); | |
if(array_key_exists("http://facebook.com/{$pageNickname}",$arrayDecoded)) | |
return $arrayDecoded["http://facebook.com/{$pageNickname}"]["id"]; | |
else | |
return null; | |
} | |
/* | |
* Get LastPosts | |
* @Return Json | |
* @Params UserID or PageID and an valid accessToken | |
* */ | |
public function loadFB($pageID){ | |
$url = "https://graph.facebook.com/v2.5/".$pageID."/feed?limit=3"; | |
// Update by MC Vooges 11jun 2014: Access token is now required: | |
$url.= '&'.$this->accessToken; | |
$request = file_get_contents($url); | |
//return the data as an object | |
return json_decode($request, true); | |
} | |
/* | |
* Get Access Token | |
* @Return String | |
* @Params $appID, $appSecret | |
* */ | |
private function getAccessToken($appID, $appSecret) { | |
$token = 'https://graph.facebook.com/oauth/access_token?client_id='.$appID.'&client_secret='.$appSecret.'&grant_type=client_credentials'; | |
return file_get_contents($token); // returns 'accesstoken=APP_TOKEN|APP_SECRET' | |
} | |
} | |
/* | |
* Example Call the MicroSDK | |
* $myPosts = FacebookMicroSDK->loadFB(FacebookMicroSDK->getPageId("PAGENICK")); | |
* */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment