Facebook Ads API Cron Job - php

We are trying to use the facebook ad reporting api to update a 3rd party app to keep up-to-date on our daily spending on facebook. We would like for these values to update automatically via a cron job but this doesn't appear to work. We have to go to the page and refresh it, for it to refresh the api data. Is it possible to get our script to update automatically? Here is our access token code.
I thought it was possible to get a long lived token and save it to the database and use that but we are unable to retrieve the accessToken because its :protected in the return json.
use FacebookAds\Api;
use FacebookAds\Object\AdSet;
use FacebookAds\Object\AdGroup;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\Fields\AdGroupFields;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdAccountFields;
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSDKException;
use Facebook\Entities\AccessToken;
// Initialize the SDK
FacebookSession::setDefaultApplication( $app_id, $app_secret );
// Create the login helper and replace REDIRECT_URI with your URL
// Use the same domain you set for the apps 'App Domains'
// e.g. $helper = new FacebookRedirectLoginHelper( 'redirect' );
$helper = new FacebookRedirectLoginHelper( $redirect_uri );
// Check if existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// Create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// Validate the access_token to make sure it's still valid
try {
if ( ! $session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// Catch any exceptions
$session = null;
}
} else {
// No session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
echo $ex->message;
}
}
// Check if a session exists
if ( isset( $session ) ) {
// Save the session
$_SESSION['fb_token'] = $session->getToken();
$access_token = $_SESSION['fb_token'];
$long_session = $session->getLongLivedSession();
//print_r($long_session);
$longtoken = $long_session->getToken();
// Create session using saved token or the new one we generated at login
$session = new FacebookSession( $long_session->getToken() );
} else {
// No session
// Get login URL
$loginUrl = $helper->getLoginUrl( $permissions );
//echo 'Log in';
header('Location: '.$loginUrl.'');
}
// Initialize a new Session and instanciate an Api object
Api::init($app_id, $app_secret, $access_token);
// The Api object is now available trough singleton
$api = Api::instance();
// Get the GraphUser object for the cusrrent user:
try {
$me = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
// echo $me->getName();
} catch (FacebookRequestException $e) {
// The Graph API returned an error
} catch (\Exception $e) {
// Some other error occurred
}

I read two questions in your post, the first is that you would like to have a background process (invoked by cron) that reads your daily spending and second that you don't have a long-lived user access token so your script isn't working correctly.
If I understand your goals correctly, it seems that these two issues are a bit conflated which I recommend separating. Follow the Facebook access token documentation for getting a long-lived token and use it directly in your script. Skip PHP session management and just put the long-lived token in a database, simple config file, or directly into the script.
And then if you're not already, have this script invoked by cron or another scheduled background processing system. It can pull your daily spending data and provide it to the 3rd party app.

Related

Why does "use" not work when included in php?

I'm updating my apps to work with the new PHP SDK, but I'm having this issue:
I want to do the Facebook stuff in a separate php file to keep things clean.
For example, this is my fb.php page:
<?php
require_once('sdk/autoload.php');
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication(...,...);
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
//logged in
} else {
//user is not logged in
?>
<script>window.location="...link to login page...";</script>
<?php
exit;
}
A page which uses the Facebook session, could look like this (testing.php):
<?php
require_once('fb.php');
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();
echo "Welcome, ".$graphObject['first_name'];
This returns this error:
Fatal error: Class 'FacebookRequest' not found
The code works fine when I put use Facebook\FacebookRequest; in testing.php , but I don't want to do that, since that was the point of putting all the Facebook session stuff in a separate fb.php file...
What is the reason for this, and is there a solution how to keep all my Facebook stuff in a separate file?
Thanks!
File in which you are including fb.php is in different scope so can't use that imports.
You have to specify them again.
You can read more about that in PHP manual.

Facebook SDK V4 Error

I am trying to implement Login with Facebook to my website.
My Code works perfectly fine on Localhost. But when I upload it to server. It gives error like this:
Facebook\FacebookSDKException Object ( [message:protected] => couldn't connect to host [string:Exception:private] => [code:protected]
You can try DEMO: http://dkclan.co/facebook_user_authentication.php
Here is my Code:
<?php
// include required files form Facebook SDK
require 'facebook-php-sdk/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'abc','xyz' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://dkclan.co/facebook_user_authentication.php' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo 'Logout';
} else {
// show login url
echo 'Login';
}
Any idea why I am getting this error?
Most likely your provider blocks outgoing CURL calls (the Facebook SDK uses CURL for calls to the Facebook API).
See this thread with the same error: https://stackoverflow.com/questions/9425406/facebook-api-exception-object-error-on-facebook-app
It makes sense as it works on your local server. The only thing you can do is ask your provider to allow calls to the Facebook API.

How to make a Facebook Login in MVC using PHP SDK v4?

What i want ?
I want to make a proper Facebook login in MVC
My directory tree (simplified) :
index.php
/controllers
/users.class.php
/core
/libs
/facebook-sdk (contains Facebook SDK files)
/models
/user.class.php
/views
/login.php
/user
/index.php
My problem :
I know how to make a one-file Facebook Login, i know how to make a Form Login in MVC, but i'm new to Facebook PHP SDK Framework.
My one page code (simplified) :
// Autoload the required files
require_once(ROOT.'core/libs/facebook-sdk/autoload.php');
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
...
// Initialize the SDK
FacebookSession::setDefaultApplication('APP_ID', 'APP_SECRET');
// Create the login helper
$helper = new FacebookRedirectLoginHelper('REDIRECT_URL');
// Check if existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// Create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// Validate the access_token to make sure it's still valid
try {
if ( ! $session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// Catch any exceptions
$session = null;
}
} else {
// No session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
echo $ex->message;
}
}
// Check if a session exists
if ( isset( $session ) ) {
// Save the session
$_SESSION['fb_token'] = $session->getToken();
// Create session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
} else {
// No session
// Get login URL
$loginUrl = $helper->getLoginUrl();
echo 'Log in';
}
If i refer to my MVC understanding i have to put all this code in a FacebookAuth function in the Controller (except the login button?) ? Do i need to include the Facebook SDK in all my application? or just for the login?
Thanks you in advance
Your question is about just one specific case, but the real problem is you don't understand MVC well enough. I would suggest you to read an article explaining MVC. You also should use an existing MVC framework if you don't have any good reasons not to. Those frameworks pevent you from reinventing the wheel.
MVC in short:
(M)odel is responsible for you data,
(C)ontroller is responsible for handling that data (e.g. transforming, checking, ..),
(V)iew is just a view, your controller should provide your view with data.
In your case that means the logic for checking a token and retrieving the loginUrl from the Facebook SDK is your controllers' responibility. Displaying the login url has to be done by your view.

Facebook Connect PHP session variable won't set

I am using the Facebook PHP API 4.0 and trying to get the session variable to be set, so I can use the user's Facebook data.
The app, and Facebook approval page have all worked as expected, but the $session variable remains to be unset no matter what I try. I have tried to find the solution from similar questions on Stack with no success. This is the code I am using:
session_start();
//start fb login stuff
require_once( $config['basedir'].'/Facebook/FacebookSession.php' );
require_once( $config['basedir'].'/Facebook/FacebookRedirectLoginHelper.php' );
require_once( $config['basedir'].'/Facebook/FacebookRequest.php' );
require_once( $config['basedir'].'/Facebook/FacebookResponse.php' );
require_once( $config['basedir'].'/Facebook/FacebookSDKException.php' );
require_once( $config['basedir'].'/Facebook/FacebookRequestException.php' );
require_once( $config['basedir'].'/Facebook/FacebookAuthorizationException.php' );
require_once( $config['basedir'].'/Facebook/GraphObject.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication($fb_app, $fb_secret);
$helper = new FacebookRedirectLoginHelper( $config['baseurl'] );
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
echo $ex;
} catch(\Exception $ex) {
// When validation fails or other local issues
echo $ex;
}
if ($session) {
echo 'Session var is finally being seen';
}
//end Facebook login stuff
The $session remains unset because you aren't logging the user in. You should modify the last part of the code to something like:
if ( isset( $session ) ) {
echo 'Session var is finally being seen';
} else {
// show login url
echo 'Login';
}
If the $session is not set, your code will ask the user to login. After they login, the $helper->getSessionFromRedirect(); part of your code should execute and set $session. See my tutorial on how to tackle this.
Edit:
If you are logging the user in from a different page, you need to make sure that $helper = new FacebookRedirectLoginHelper( '{your-url}' ); is the same on both pages. If they differ, then you will get a exception from the API related to redirect_uri mismatch.

facebook SDK 4.0 infinite redirect loop in php

I've a problem with facebook sdk 4.0
After I clear session/cookies it works fine. But sometimes, and I cannot determine when, if I go to the app, it starts an endless redirect loop!
I've put all my code on git, since documentation doesn't provide exact ansswers:
https://github.com/sandrodz/facebook-canvas-app-sample-sdk-4.0/blob/master/index.php
<?php
// Working canvas APP, FB SDK 4.0
session_start();
// Load SDK Assets
// Minimum required
require_once 'Facebook/FacebookSession.php';
require_once 'Facebook/FacebookRequest.php';
require_once 'Facebook/FacebookResponse.php';
require_once 'Facebook/FacebookSDKException.php';
require_once 'Facebook/FacebookCanvasLoginHelper.php';
require_once 'Facebook/GraphObject.php';
require_once 'Facebook/GraphUser.php';
require_once 'Facebook/GraphSessionInfo.php';
require_once 'Facebook/HttpClients/FacebookHttpable.php';
require_once 'Facebook/HttpClients/FacebookCurl.php';
require_once 'Facebook/HttpClients/FacebookCurlHttpClient.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Faceboob\FacebookSDKException;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
// Facebook APP keys
FacebookSession::setDefaultApplication('XXX','XXXXX');
// Helper for fb canvas authentication
$helper = new FacebookCanvasLoginHelper();
// see if $_SESSION exists
if (isset($_SESSION) && isset($_SESSION['fb_token']))
{
// create new fb session from saved fb_token
$session = new FacebookSession($_SESSION['fb_token']);
// validate the fb_token to make sure it's still valid
try
{
if (!$session->validate())
{
$session = null;
}
}
catch (Exception $e)
{
// catch any exceptions
$session = null;
}
}
else
{
// no $_SESSION exists
try
{
// create fb session
$session = $helper->getSession();
}
catch(FacebookRequestException $ex)
{
// When Facebook returns an error
print_r($ex);
}
catch(\Exception $ex)
{
// When validation fails or other local issues
print_r($ex);
}
}
// check if 1 of the 2 methods above set $session
if (isset($session))
{
// Lets save fb_token for later authentication through saved $_SESSION
$_SESSION['fb_token'] = $session->getToken();
// Logged in
$fb_me = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject();
// We can get some info about the user
$fb_location_name = $fb_me->getProperty('location')->getProperty('name');
$fb_email = $fb_me->getProperty('email');
$fb_uuid = $fb_me->getProperty('id');
}
else
{
// We use javascript because of facebook bug https://developers.facebook.com/bugs/722275367815777
// Fix from here: http://stackoverflow.com/a/23685616/796443
// IF bug is fixed this line won't be needed, as app will ask for permissions onload without JS redirect.
$oauthJS = "window.top.location = 'https://www.facebook.com/dialog/oauth?client_id=1488670511365707&redirect_uri=https://apps.facebook.com/usaidgeorgia/&scope=user_location,email';";
}
?>
I went ahead to debug line by line, and these are my findings:
// see if a existing session exists
if (isset($_SESSION) && isset($_SESSION['fb_token']))
{
echo '$_SESSION and $_SESSION["fb_token"] are set';
// create new session from saved access_token
$session = new FacebookSession($_SESSION['fb_token']);
// validate the access_token to make sure it's still valid
try
{
if (!$session->validate())
{
$session = null;
echo 'access_token is not valid';
}
echo 'access_token is valid';
}
catch (Exception $e)
{
// catch any exceptions
$session = null;
echo 'something error happened ' . $e;
}
}
I get error:
$_SESSION and $_SESSION["fb_token"] are setsomething error happened exception 'Facebook\FacebookSDKException' with message 'Session has expired, or is not valid for this app.' in /home2/nakaidze/public_html/mesamoqalaqo_app/Facebook/FacebookSession.php:247 Stack trace: #0 /home2/nakaidze/public_html/mesamoqalaqo_app/Facebook/FacebookSession.php(221): Facebook\FacebookSession::validateSessionInfo(Object(Facebook\GraphSessionInfo), '148867051136570...') #1 /home2/nakaidze/public_html/mesamoqalaqo_app/user-functions.php(56): Facebook\FacebookSession->validate() #2 /home2/nakaidze/public_html/mesamoqalaqo_app/index.php(2): require('/home2/nakaidze...') #3 {main}
The access token you're using in $_SESSION['fb_token'] has expired. By default the access token returned by Facebook lasts for 2 hours and then it expires.
After you get the FacebookSession for the first time, you need to extend the access token it returned and save it in your $_SESSION['fb_token']:
$session = $helper->getSession();
$accessToken = $helper->getAccessToken();
$longLivedAccessToken = $accessToken->extend();
$_SESSION['fb_token'] = (string) $longLivedAccessToken;
Also, when you validate the access token with validate(), it will throw if it's not valid:
// validate the access_token to make sure it's still valid
try
{
$session->validate();
echo 'access_token is valid';
}
catch (FacebookSDKException $e)
{
$session = null;
echo 'Access token is no longer valid, need to get a new token';
}
This might help clarify info about Facebook access tokens.

Categories