Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook New PHP SDK For Graph API - Multi Query

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.

like image 263
Rob K. Avatar asked Dec 02 '10 07:12

Rob K.


2 Answers

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.

like image 96
Rob K. Avatar answered Oct 29 '22 19:10

Rob K.


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); }
like image 38
Intellix Avatar answered Oct 29 '22 18:10

Intellix