Facebook New PHP SDK For Graph API - Multi Query - facebook

I'm at my wits end in what the queries parameter value should look like. So to sumbit a multiquery accross the graph API library, the following would be the code method to perform it, as far as I can tell.
$param = array(
'method' => 'fql.multiquery',
'queries' => $multiQuery,
'callback' => '');
$queryresults = $facebook->api($param);
Using this method in the new PHP SDK library from Facebook, has any one made this work? If so can you drop an example on how you build the complete value of the $multiQuery variable?
I've been struggeling with this for a few days and I'm only finding exmaples with the old PHP library.

Why is it always after banging your head for days, you ask a question, and 5 minutes later, you come up with the answer your self.
So here was MY lovely experience.
Since in PHP you can use a "/' character to start a text string, I got my self stuck in the flip flopping of the double quote character and single quote character. It dawned on me that the queries defined in a multi query are, duh, wrapped by double quotes.
So lesson learned? If you have a where clause that uses a string value in a multi query, make sure for pete's sake you use SINGLE QUOTES around the string value your filtering on.
BAD BAD - This is what I did. note the double quotes around myvalue and myothervalue. NAUGHTY!
$multiQuery = {
"query1":"select something from something where somecolumn = "myvalue"",
"query2":"select something from something where somecolumn = "myothervalue""
};
GOOD Example - Now look at myvalue and myothervalue.
$multiQuery = {
"query1":"select something from something where somecolumn = 'myvalue'",
"query2":"select something from something where somecolumn = 'myothervalue'"
};
So now I can...
$multiQuery = {
"query1":"select something from something where somecolumn = 'myvalue'",
"query2":"select something from something where somecolumn = 'myothervalue'"
};
$param = array(
'method' => 'fql.multiquery',
'queries' => $multiQuery,
'callback' => '');
$queryresults = $facebook->api($param);
And if any of you are wondering what is the actual type of the $multiQuery variable is (for newbie like me), it's just a string data type. It's not an array, nothing more nifty than text.

Considering an array of node id's with their respective url's as values you'll have
/**
*A JSON-encoded dictionary of the queries to perform. The array contains a set of key/value pairs.
*Each key is a query name, which can contain only alphanumeric characters and optional underscores.
*Each key maps to a value containing a traditional FQL query.
*/
$fql = '{';
foreach ($path as $key1 => $value1) {
$fql .= '"' . $key1 . '":"SELECT share_count, like_count, comment_count, total_count FROM link_stat WHERE url=\'' . $value1 . '\'",';
}
$fql .= '}';
$param = array(
'method' => 'fql.multiquery',
'queries' => $fql,
'callback' => ''
);
try {
$fqlresult = $facebook->api($param);
} catch (FacebookApiException $e) {
watchdog('Facebook Query', 'Parsing error on node #node | #error', array('#node' => $key1, '#error' => $e), WATCHDOG_DEBUG); }

You can try this:
$multiQuery= array ("query1" => "query #1 goes here","query2" => "query #2 goes here");
$param = array(
'method' => 'fql.multiquery',
'queries' => $multiQuery,
'callback' => '');
$queryresults = $facebook->api($param);

Related

WordPress API passing email argument issue

Using a WordPress REST API custom endpoint, I am attempting to get user data (or at least the user id) with the following code in the functions.php file:
function getUser(WP_REST_Request $request) {
global $wpdb;
$email = $request->get_param( 'email' );
$query = "SELECT * FROM wp_users WHERE user_email = $email";
$result = $wpdb->get_results($query);
return $result;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'myapi/v1', '/getcustomer/(?P<email>[^/]+)', array(
'methods' => 'GET',
'callback' => 'getUser'
) );
} );
Testing the function with the endpoint /wp-json/myapi/v1/getcustomer/joe#anymail.com it returns with empty brackets [ ]. Am I missing something here? Any help would be greatly appreciated.
There are multiple issues with your code:
You should encode your user emails or send it via POST method.
Your current query is open to SQL Injection
Your value must be enclosed in quotes. Now it translates to .. WHERE user_email = joe#anymail.com and that is SQL syntax error.
So your code should look like this:
$query = "SELECT * FROM wp_users WHERE user_email = %s";
$result = $wpdb->get_results($wpdb->prepare($query, $email));

Does tweet_mode=extended work with the Twitter statuses/user_timeline API?

There is no mention of tweet_mode at https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html
I am wondering if perhaps I am using the wrong API to be able to take advantage of tweet_mode?
In my application, I supplied the tweet_mode=extended argument and it had no effect. My code...
// Load the Tweets.
$args = array(
'screen_name' => $username,
'exclude_replies' => 'true',
'include_rts' => 'true',
'tweet_mode' => 'extended',
'count' => $numitems,
);
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$tweets = $connection->get('statuses/user_timeline', $args);
if (!empty($tweets)) {
foreach ($tweets as $tweet) {
$text = $tweet->full_text;
// etcetera
Yes, you can use tweet_mode with the statuses/user_timeline API. Retweets are a special case, though. Check the retweeted_status object, as described at https://dev.to/kehers/formatting-tweets-a-look-at-extended-tweets-retweets-and-quotes-n5j
In short, if a tweet is a retweet, the extended tweet must be accessed at $tweet->retweeted_status->full_text. Thus, it's necessary in your code to check if each tweet object has a retweeted_status property.

Error in FQL Query

This is My Code
$ids = join(", ",$man);
if ( !isset($_SESSION[$appID.'_FQLResult']) ) {
$FQLQuery = "SELECT uid, sex, pic_square FROM user WHERE uid in ($ids)";
$FQLResult = $facebook->api(array( 'method' => 'fql.query', 'query' => $FQLQuery, 'access_token'=>$fbme['access_token'] ));
$_SESSION[$appID.'_FQLResult'] = $FQLResult;
} else {
$FQLResult = $_SESSION[$appID.'_FQLResult'];
}
echo $ids;
echo $FQLResult;
There is an error
when i echo $ids it shows the value of $ids but when i echo $FQLResult is shows only "array" written whats wrong in this fql query?
how i can fix this?
I think there is something wrong in this query
$FQLQuery = "SELECT uid, sex, pic_square FROM user WHERE uid in ($ids)";
Use implode() directly instead of join()
After a successful API call, you should be expecting an array, so use print_r($FQLResult);
Always, if you are not sure of the type of the variable use var_dump()
You may want to use the new Graph API end-point (/fql?q=YOUR_QUERY), read here
Serialize your array before adding to sessions
Watch our of the session size limits!

Facebook fql.query always an empty result

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

FQL: Order photos by likes

Is there anyway to retrieve photos ordered by the number of likes in FQL?
PLEASE NOTE: I have never played with FQL before, but I need this exact solution yesterday, so I gave it a go!
Try adding references to "like_info" in the first line in the previous answer (maybe like_info is new since you asked this?):
$fql = "SELECT like_info, object_id,src_small,link FROM photo WHERE aid = '2389563453799923709' ORDER BY like_info created DESC";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$photos = $facebook->api($param);
This may well give you answers returned in order from most to least liked. At least, it worked for me! :)
Please note: This solution is no longer require as like_info has been added to photo
So, I'm guessing this is impossible to do nicely? Here's the ugly solution:
$fql = "SELECT object_id,src_small,link FROM photo WHERE aid = '2389563453799923709' ORDER BY created DESC";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$photos = $facebook->api($param);
if (count($photos) > 0) {
for ($i = 0; $i < count($photos); $i++) {
$objectId = $photos[$i]['object_id'];
$like_count = $facebook->api('/'.$objectId.'/likes');
$photos[$i]['likes'] = count($like_count['data']);
}
}
function cmp($a, $b) {
if ($a['likes'] == $b['likes'])
return 0;
return $a['likes'] > $b['likes'] ? -1 : 1;
}
usort($photos, 'cmp');