file_get_contents() doesn't return FQL results even if the fql itself in url works - facebook

Type this query in url bar, it works. I can get the results on the browser window.
https://graph.facebook.com/fql?q=SELECT+actor_id+FROM+stream+WHERE+post_id+IN+(SELECT+post_id+FROM+stream+WHERE+filter_key+in+(SELECT+filter_key+FROM+stream_filter+WHERE+uid=me()+AND+name='Links')+AND+attachment.caption+in+('xxxx.com','xxxx.com')+AND+type=80+AND+created_time%3C=now()+LIMIT+20)+ORDER+BY+created_time+DESC+&access_token=xxxx...
But php code below doesn't. file_get_contents() returns empty array.
$fql_query_url = 'https://graph.facebook.com/'
. 'fql?q=' . str_replace(' ', '+', $query1)
. '&access_token=' . $access_token;
$fql_query_result = file_get_contents($fql_query_url);
The query mentioned above is dump of $fql_query_url.
App already has got permissions below.
read_stream
read_insights
Does anyone know anything?

I finally solved it by myself.
attachment.caption in xxxx
was the cause of no results regardless of that it works when you type query in url bar.

Related

Facebook page tab app session across subpages PHP SDK 4

See the full original question further down
Using the latest Facebook PHP SDK 4.4.0, in my main app page I can do the following to get a user id etc.
<?php
FacebookSession::setDefaultApplication(APP_ID, SECRET);
$helper = new FacebookRedirectLoginHelper( PAGE_URL );
$pageHelper = new FacebookPageTabHelper();
$session = $pageHelper->getSession();
echo '<p>You are currently viewing page: '. $pageHelper->getPageId() . '</p>';
// get user_id
echo '<p>User Id: ' . $pageHelper->getUserId() . '</p>';
// **depcrecated** get like status - use for likegates
echo '<p>You have '. ( $pageHelper->isLiked() ? 'LIKED' : 'NOT liked' ) . ' this page</p>';
// get admin status
echo '<p>You are '. ( $pageHelper->isAdmin() ? 'an ADMIN' : 'NOT an ADMIN' ) . '</p>';
?>
This does not work on sub pages of my app ... Why is the session (and amongst other things, the signed request) lost? How can I get them back and how can I get methods such as getUserId() from the the FacebookPageTabHelper to continue to work on sub pages?
full original question
I'm fairly new to Facebook app development and I'm having problems with session management and I just can't seem to be able to wrap my head around it. Of course it doesn't help that the official documentation is almost useless.
My problem is that the page session get lost when moving away from the apps main page to a subpage within the Facebook page tab app iframe.
I use the following PHP code to obtain the session and user id on the main (initial) app page and it works great:
<?php
FacebookSession::setDefaultApplication(APP_ID, SECRET);
$helper = new FacebookRedirectLoginHelper( PAGE_URL );
$pageHelper = new FacebookPageTabHelper();
$session = $pageHelper->getSession();
?>
But it doesn't work on sub pages :( when a user clicks on a menu item (or any other link inside the app/iframe), the session goes bye bye. Which is not ideal as I need the user id of the user to track whether or not that user has completed certain actions. Of course I could send the ID along with every request, but there must be a way to have a persisting session, no?
Is there a way to retrieve the session on a sub page in PHP? If so, how? Or do I have to load additional content using javascript? And how would that work, if I can't keep the session between requests and therefore have no way of identifying which user a request came from? How do others handle this?
What I'd like to avoid is to write my own user session management, which would solve the problem but is simply not in the budget and I was hoping I could work with what Facebook already had on offer. Especially since my app doesn't require user information/permissions of any kind.
Thanks a lot in advance for any info on this topic, greatly appreciated, going in circles here.
Edit to clarify: I thought of just saving the Facebook session in a PHP session cookie, but how would I use that to reconnect with Facebook after changing the page?
I finally managed to solve this problem. I'm not sure whether this is considered the right way or can even be a recommended way of doing this, but it works and since time is of the essence, I don't have much of a choice.
If anybody has any further ideas or suggestions, please comment.
Here's how I did it:
// store the signed request
if(isset($_REQUEST['signed_request'])) {
$_SESSION['signed_request'] = $_REQUEST['signed_request'];
} elseif($_SESSION['signed_request']) {
$_REQUEST['signed_request'] = $_GET['signed_request'] = $_POST['signed_request'] = $_SESSION['signed_request'];
}
// assign the stored signed request to REQUEST, GET and POST vars (the unsavory bit, imo)
$_REQUEST['signed_request'] = $_GET['signed_request'] = $_POST['signed_request'] = $_SESSION['signedRequest'];
FacebookSession::setDefaultApplication(APP_ID, APP_SECRET);
$accessToken = APP_ID . '|' . APP_SECRET;
$this->session = new FacebookSession($accessToken);
$pageHelper = new FacebookPageTabHelper();
$isAdmin = ($this->pageHelper->getPageData('admin')) ? $this->pageHelper->getPageData('admin') : 0;
// get pade id
echo '<p>You are currently viewing page: '. $pageHelper->getPageId() . '</p>';
// get user_id
echo '<p>User Id: ' . $pageHelper->getUserId() . '</p>';
// get admin status
echo '<p>You are '. ( $isAdmin ? 'an ADMIN' : 'NOT an ADMIN' ) . '</p>';

how to append access token in fql query

I am using this code for fql query to get user insights for the page he admins.
$fql = "SELECT metric, value FROM insights WHERE object_id=". $url . " AND metric='page_fans_country' AND end_time=1398665793 AND period=2592000";
$apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);
$lc_json=file_get_contents($apifql);
$lc = json_decode($lc_json);
It throws error 104 and this error code says I must use access token while I have read_insights permission also.
you can get access token by using facebook object and just embed it to your fql query
<?php
$token = $facebook->getAccessToken();
$apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql)."&access_token=".$token;
?>
You should use the
GET https://graph.facebook.com/fql?q=...
endpoint as described here (https://developers.facebook.com/docs/technical-guides/fql/#read). This should do the trick:
$apifql="https://graph.facebook.com/fql?q=".urlencode($fql)."&access_token=".$facebook->getAccessToken();

How to get facebook username from my friends list.?

I want to get the userNames of people in my friends list . How can i achieve that ?
I am using
https://graph.facebook.com/me/friends&access_token=...
This is returning me a json response with all the Name and IDnumber ? I want somehow to get the Username ( Not userID ) ?
There is one way that I access each ID and then get the userName but that will take so long considering I have 500 friends. Is there some shorter way / easier way to do that ?
Ask for the username in the fields parameter:
https://graph.facebook.com/me/friends?fields=username&access_token=...
I can't seem to find anything in the API to get a list of user profiles. But one possible solution is to use batch requests. You can combine each individual user profile request into one large batch. It's still cumbersome, but better than making a separate HTTP request for each friend.
http://developers.facebook.com/blog/post/2011/03/17/batch-requests-in-graph-api/
For example:
$batched_request = '[
{"method":"GET","relative_url":"friend_id_1"},'.'{"method":"GET","relative_url":"friend_id_2"}
]';
$post_url = "https://graph.facebook.com/" . "?batch=" . $batched_request
. "&access_token=" . $access_token . "&method=post";

sphotos.xx.fbcdn.net Issue With Full Size Profile Pictures

I'm stuck with what is causing this issue. If i'm logged in as my account, the code below does not show user: 100000920350966 but if i logout & back into my wife's account I can now see that users profile picture.
Error I get logged into my FB account:
http://sphotos.xx.fbcdn.net/hphotos-snc6/230962_10150332269178009_784368008_9751025_7802946_n.jpg
An example of one that does work:
http://a4.sphotos.ak.fbcdn.net/hphotos-ak-ash4/419763_10151360491990078_877370077_23594484_2739927_n.jpg
the code I am using is the PHP SDK and this piece for getting an image:
function randomUser() {
$query = mysql_query("SELECT the_facebook_id FROM users WHERE the_facebook_id != '$facebook_id' ORDER BY RAND() LIMIT 0,1");
$result = mysql_fetch_array($query);
mysql_close();
return $result; }
$theuser = randomuser();
$albums = $facebook->api('/' . $theuser[0] . '/albums');
foreach($albums['data'] as $album){
if ($album['type'] == 'profile'){
$photos = $facebook->api('/' . $album['id'] . '/photos');
$random_pic = $photos['data'][0]['source'];
}
}
echo "<br /><br />The User: " . $theuser[0] . "<br />";
echo "Photo Source: " . $photos['data'][0]['source'] . "<br />";
echo "THE PHOTO HEIGHT: " . $photos['data'][0]['height'] . "<br />";
echo "THE PHOTO WIDTH: " . $photos['data'][0]['width'] . "<br />";
I honestly have no idea what when in my account I can't see particular profile pictures but in my wife's I can see all of them. All help is greatly appreciated!
Cheers
Interesting case you've got there.
I can't tell you what's wrong, but I have a guess which is divided to two options.
The urls in question are for static content and so the content is not depended on the current user.
However, that url is of a cdn, which means that the copy of the content you get is different (probably) from the copy I get, which explains how is it that you're getting an error while I'm able see the picture for the same url.
The two options:
The cdn you are directed to is not available at all and so all images you ask from it will result in page not found error. Try to ping sphotos.xx.fbcdn.net and see what you get.
The cdn you are directed to does not have a copy of the image you are trying to get, maybe for some reason an error occurred when the file was supposed to propagate to the server.
This is just a guess of course, in any case I recommend you to file a bug report and have the facebook team look it up, it might take some time for them to check it out/do something about it though.

Facebook ID integers being returned oddly

I'm making FQL calls using the following url and then making a curl call.
$url = 'https://api.facebook.com/method/fql.query?access_token='.$access_token.'&query='.rawurlencode($query).'&format=JSON';
and I then pass the returned data through a json_decode call
I've got this query:
SELECT name,page_id,page_url FROM page WHERE page_id IN (SELECT page_id FROM page_admin WHERE uid= $uid )
which returns a list of the names and pages for which the specified UID is an administrator.
On some PHP installs (and I've not been able to narrow it down) the page_id is turned from a long integer into a scientific notation - so 174311849258492 is returned as 1.7431184925849E 14 which of course breaks things.
As I can't reproduce this on my server I'm not sure where the conversion is happening. Digging around I've found a suggestion that doing this:
json_decode( preg_replace('/:(\d+,)/', ':"${1}",', $response ) );
will fix it
But why do some json_decodes cast into scientific notation for no apparent reason?
If you are using your own curl calls then you can simply append &format=JSON-STRINGS onto the end of the url which returns all items as strings.
As Danny pointed out its a 32/64 bit issue. FB assume that everything is 64 bit and pass back an integer for this value rather than a string.
So what you need to do is take the integers and convert them to strings BEFORE pulling them from the JSON array. Page IDs and Group IDs are passed as integers (Facebook user IDs are passed as strings)
The code to do this is:
$response = curl_exec($ch);
$err_no=curl_errno($ch);
curl_close($ch);
$response=preg_replace('/"gid":(\d+)/', '"gid":"$1"', $response );
$response=json_decode( preg_replace('/"page_id":(\d+)/', '"page_id":"$1"', $response ) );
if (isset($response->message)) {
throw new Exception ($response->message);
}
return( $response);