FQL Query for facebook

2019-06-04 19:40发布

问题:

Iam getting all my friends as an array using

$graph_url = "https://api.facebook.com/method/fql.query?access_token=" . $access_token .
 "&query=SELECT+uid,name,work.position.name+FROM+user+WHERE+uid+IN+
 (SELECT+uid2+FROM+friend+WHERE+uid1+=+me())&format=json";

And I want to put a condition to fetch the friends whose work position's name is 'dev'.I had try with FQL like

$graph_url = "https://api.facebook.com/method/fql.query?access_token=" . $access_token .
 "&query=SELECT+uid,name,work.position.name+FROM+user+WHERE+uid+IN+
(SELECT+uid2+FROM+friend+WHERE+uid1+=+me())+AND+strpos(work.position.name,'dev')>=0&format=json";

But it results me empty array even some of them satisfying the condition.Can anyone suggest me the correct way to approach this..Thanks in Advance.

回答1:

This has been tried to be achieved by several other people as well, see

https://stackoverflow.com/questions/17382739/how-to-use-strpos-in-fql-query

https://stackoverflow.com/questions/18115546/strpos-in-fql-query

I assume that you cannot use strpos() on fields which contain an array and not an object (which you can reference via object.subobject.field).

"Proof" of my suspicion: If you try to search your friends current locations (which is a subobject of the user object, and not an array), it works:

SELECT uid,name,current_location FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) and strpos(current_location.city, 'M') = 0

There, I search for all my friends living in a city starting with "M".

Ultimately, you could also parse the resuls on the client side if FQL cannot solve your problem.

And, by the way, I think you should use another endpoint for your FQL queries:

http://graph.facebook.com/fql?q=...


回答2:

You can use strpos & lower on some subset of data you already have with indexable field.

In your example you don't have indexable field so ur getting empty array.

SELECT uid,name,work.position.name  FROM user WHERE uid IN 
(SELECT uid2 FROM friend WHERE uid1 = me()) AND work.position.name 

This fql statement gives list of friends who has given value for work.position.name, then apply conditions on this json object .