how to do fql.multiquery using facebook php sdk 3.

2019-06-03 01:25发布

问题:

I'm in the process of migrating from the 2.x PHP SDK to 3.x. All of the multiquery calls are broken and I haven't been able to figure out how to resolve.

The facebook connect login using oauth javascript works and allows data for the current user to be accessed server side. I assume this verifies my access token is legit.

I've tried the following code variations and get errors each time:

Old way:

        $id = $fb->getUser(); // RETURNS A VALID USER ID
        $fql = '{ "friends" : "SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = '.$id.')", "profiles" : "SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid FROM #friends)"}';

        $response = $fb->api( array('method' => 'fql.multiquery','queries' => $fql));


Gives the error: PHP Fatal error: Uncaught Exception: 102: Requires user session\n thrown



Assumed New Way of doing it:

        $id = $fb->getUser(); // RETURNS A VALID USER ID
        $fql = '{ "friends" : "SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = '.$id.')", "profiles" : "SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid FROM #friends)"}';

        $response = $fb->api( array('method' => 'fql.query','query' => $fql));

Gives the error: PHP Fatal error: Uncaught Exception: 601: Parser error: unexpected '{' at position 0

Any help understanding how to do a multiquery using the new PHP SDK is appreciated.

回答1:

For your particular query, use the new fql Graph API end-point and in only one query:

$user = $facebook->getUser();
if ($user) {
  try {
    $fql = urlencode("SELECT uid, name, pic_square FROM user WHERE is_app_user = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = $user)");
    $response = $facebook->api("/fql?q={$fql}");
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}

Also use the is_app_user instead of the deprecated has_added_app one!



回答2:

Just pass queries as associative array:

  $common_fields_fql = 'SELECT uid, email, contact_email, first_name, last_name, birthday_date, pic_square, sex, hometown_location, current_location, is_app_user FROM user WHERE uid ';
  $fql = array(
    'friends' => 'SELECT uid2 FROM friend WHERE uid1 = me()',
    'friends_details' => $common_fields_fql . 'IN (SELECT uid2 FROM #friends)',
    'me' => $common_fields_fql . '= me()',
    'family' => 'SELECT uid, birthday, name, relationship FROM family WHERE profile_id = me()',
  );
  $details = $facebook->api(array(
    'method'   => 'fql.multiquery',
    'queries'  => $fql,
  ));