Facebook fql.query always an empty result - facebook

I'm writing an app to query insights for several dozen applications; no matter which of these insights table queries I try, I always get an empty result set. No errors. Other tables (eg page, application,) do work fine. Can anyone spot what I'm missing?
header('Content-type: text/plain');
//WORKS:
$q = 'SELECT app_id, api_key, canvas_name, display_name FROM application WHERE app_id="111"';
//DOESN'T WORK:
$q = "SELECT metric, value FROM insights WHERE object_id='111' AND metric='page_active_users' AND period='604800' AND end_time='1318615472'";
//CODE:
require_once '../../scg_common/facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => '111',
'secret' => '222', 'cookie' => true,
));
$facebook->getAccessToken();
$facebook->getSignedRequest();
$params = array(
'method' => 'fql.query', 'query' => $q,
//'callback' => null, 'format' => '', 'access_token' => '',
);
try{
$result = $facebook->api($params); }
catch(Exception $o){
var_dump($o); }
var_dump($result);

Have you tried the examples listed on the documentation yet? I mean the exact string?
http://developers.facebook.com/docs/reference/fql/insights/
If those don't work, then it might be because you don't have the required permission, which is read_insights.
Let me know how it turns out.

The parameter end_time needs to be a valid PST midnight timestamp (per the documentation). My own testing shows the helper function end_time_date() does not work, so you need to pass in the timestamp. Once I fixed that, I started getting query results.
Here are some related bugs:
https://developers.facebook.com/bugs/228141840589755
https://developers.facebook.com/bugs/232510993491615

Related

How do I output the campaign status from the ad account insight level (Facebook marketing api)

I am trying to output a list of all campaigns in an ad account with a few insights with it.
I was able to get all the data I wanted except the campaign effective status, for some reason its not being called out and is showing an error.
The Facebook quick start compiler shows that 'delivery' is the proper keyword to get the status.
Any help would be appreciated.
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\AdsInsights;
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
$access_token = 'EAAXYydBVCvUBAHBsZCegoqntZ234234ZD';
$ad_account_id = 'act_123';
$app_secret = '1123123';
$app_id = '123123123';
$api = Api::init($app_id, $app_secret, $access_token);
$api->setLogger(new CurlLogger());
$fields = array(
'campaign_id',
'delivery',
'clicks',
'campaign_name',
);
$params = array(
'level' => 'campaign',
'filtering' => array(),
'breakdowns' => array(),
'time_range' => array('since' => '2017-08-08','until' => '2019-09-26'),
);
return (new AdAccount($ad_account_id))->getInsights(
$fields,
$params
)->getResponse()->getContent();

Trying to post to facebook page with app but get "The user hasn't authorized the application to perform this action"

I am trying to write a script that will post an update to a facebook page timeline. I created an app and created a key for the app. I am using the code below:
require_once HOME_PATH . '/include/facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => true,
'scope' => 'publish_stream',
));
$user_id = $facebook->getUser();
$access_token = $facebook->getAccessToken();
$attachment = array(
'access_token' => $access_token,
'message' => 'this is my message',
'name' => 'name',
'link' => ROOT_URL . $blog_data['url'] . '.htm',
'caption' => $blog_data['title'],
'description' => $blog_data['title'],
);
if ($image = get_first_image($blog_data['body'])) {
$attachment['picture'] = $image;
}
$facebook->api(FACEBOOK_BLOG_PAGE . '/feed', 'post', $attachment);
i think this key is connected to the app and not to my user, so it sounds like i would have to grant the permission within facebook, but i have seen some other posts telling me that i need to do this in code. The examples aren't very clear.
I know similar questions have been asked but i haven't seen any clear answers yet. Can anyone please clarify this?
If you want to get user_id by this piece of code: $user_id = $facebook->getUser();
The first thing you need, is to login an user through a URL returned by $facebook->getLoginUrl(array('scope' => array('perm', 'perm2'))).
Also, notice, where I put permissions, which I require. Not into the Facebook class constructor, but into the method getLoginUrl.
And lastly, for posting into a page, you need publish_actions permission (not publish_stream).

how to set album privacy settings using facebook graph api

Is there any way to change the Facebook album privacy settings with graph api?
I'm trying to find out, but all I could found is how to get the privacy settings using fql, but not to set.
I'm creating the album as follow
$postdata = http_build_query(array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
) $context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false, $context));
$albumid = $result->id;
Now if I add privacy=>"value", it gives $albumid=null.
I'm not sure where I need to add privacy parameter.
When you create an album, you can send these parameters in post request.
name, message, location and privacy.
Value of privacy field can be set like this,
privacy={value: "CUSTOM"} (send this as post parameter)
The value field may specify one of the following strings:
EVERYONE, ALL_FRIENDS, NETWORKS_FRIENDS, FRIENDS_OF_FRIENDS, CUSTOM .
As facebook docs sucks, there's no mention about it on albums object page.
However, you can read it on post object.
Edit: (after comments)
In php sdk you can do something like this,
$ret_obj = $facebook->api('me/albums', 'POST',
array(
'privacy' => '{value: "CUSTOM"}',
'location' => 'India'
));
The document of creating an album is put in https://developers.facebook.com/docs/reference/api/user/#albums
Privacy setting is a json-style string. So you could create an array() and use json_encode() to generate it.
with php sdk it is also possible as php style #kaur
$ret_obj = $facebook->api('/me/albums/', 'POST', array(
'source' => '#' . $photo,
'message' => 'Picture uploaded',
'location' => 'Goran',
'privacy'=> array('value'=>'EVERYONE'), //'privacy'=> '{value: "EVERYONE"}', //worked too!! SELF, ALL_FRIENDS, EVERYONE
)
);

Get friendlist for selected location

HI,
I am using face api to fetch friend list.Please see this code below:
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '170864786279481',
'secret' => 'cd4b835feb73c358eeb4c4df5c293a42',
'cookie' => true, // enable optional cookie support
));
$session = $facebook->getSession();
$friendsLists = $facebook->api('/me/friends/');
This code gives me all friends of user But I need friends for selected location only.For example I want to get only those friends who are living in city "New york" only.Is it possible in this facebook api.Please guide me.
Regards
Deepak
You should use FQL's friend and user tables.
FQL Usage example:
$query = "SELECT ...";
$params = array(
'access_token' => $session['access_token'],
'query' => $query
);
$url = "https://api.facebook.com/method/fql.query?" . http_build_query($params);
$data = simplexml_load_file($url);
// do something with the response, you should catch errors too.

how to use fql query

i am new at facebook application development i want to know how fql query works i used it in my application but i want to know where fql query results are displayed or how they can be displayed if you can't explain plz send me the link because the official documentation didn't helped me either.
Connect to FB using the PHP SDK. (available here)
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once 'library/facebook/src/facebook.php';
Check if the user has logged in to FB
$facebook = new Facebook(array(
'appId' => 'YOUR API ID',
'secret' => 'YOUR APP SECRET',
'cookie' => true, // enable optional cookie support
));
FQL
try {
$me = $facebook->api('/me');
$currentPage=1;
$currentPost=1;
$currentDate="";
$list="";
$arrDate=array();
$fql = "YOUR FQL STATEMENT HERE";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);//Here you have your FQL result set
foreach($fqlResult as $row){
//Do something to the result
}
} catch (FacebookApiException $e) {
error_log($e);
}
You can use the Facebook Graph Explorer
https://developers.facebook.com/tools/explorer
And select under the Access token field, "FQL query".
Now you can try FQL queries and see the result of them.