Comment/like-count from a facebook post - facebook

Any ideas how to get the likes/comment count from a Facebook post - fast?
My idea was to get the stats of every single post, but it takes too long.
$post = $facebook->api("/".$array['PostID']."/?fields=comments,likes", array());
$likes_count = count($post['likes']['data']);
$comments_count = count($post['comments']['data']);
I also tried to do it with a batch request like:
$buildQuery .= '{"method":"GET","relative_url":"'.$array['PostID'].'/?fields=comments,likes"}';
The problem here is, that it doesn't work, because batch requests supports only POST mode.
What should I try? Any ideas?

Related

Downloading from google cloud storage with php

Is there a way to achieve downloading via. the google-php-api? I have tried the following:
using the medialink and trying to curl the object (Returns "Login Required")
reading the guzzle response stream (comes back empty even though all the headers have the correct data)
I am able to see everything but the body of the file via. the API.
Edit:
I am of course able to download the file via the medialink, taken it is set to public - however that will not work for this situation.
The solution is as follows...
You must make an authorized HTTP request, to do this you must:
$object = $service->objects->listObjects(BUCKET, OBJECT);
$http = $client->authorize();
$request = new GuzzleHttp\Psr7\Request('GET', $object->getMediaLink());
$response = $http->send($request);
$body = $response->getBody()->read($object->getSize());
The above is a small snippet but the jist of what you need to get the contents of a file.

Facebook Graph API Scheduled Posts created but not published

I'm using django-facebook's Open Facebook API to try to defer a post with an image to a facebook page. The (relevant part of the) code is:
graph_api = OpenFacebook(integration.long_lived_page_token)
message = "something"
picture = "http://example.com/image.jpg"
d = datetime.datetime.strptime('07-25-2014 06:40 pm UTC', '%m-%d-%Y %I:%M %p %Z')
timestamp = timestamp = int(time.mktime(d.timetuple()))
try:
facebook_response = graph_api.set('/{0}/feed'.format(
facebook_page_id), message=message,
link=picture, picture=picture,
scheduled_publish_time=timestamp, published=0)
except:
...
This seems to work fine (I get the post id on facebook_response) but when the time comes to post this scheduled post nothing happens. When I check at the facebook page I see an error saying 'Sorry, something went wrong publishing this scheduled post' and offers to delete it or post it then. So, I can't seem to figure out what am I doing wrong.
I found a similar question but it does not offer a good answer.
Oh, I forgot to say when I remove timestamp and published=0 parameters (i.e. I post on that exact moment), the post gets created and published on the page's feed.

How to delete a notification from a game request

I am trying to delete an application notification on application load.
So when I invite my friend he gets a notification and requests "ok" and if he clicks on it then it should be deleted. How? I have tried multiple ways but haven't succeeded.
$REQIDs=$_REQUEST['request_ids'];
$user_profile = $facebook->api('/me');
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=XXXXXXXX&client_secret=XXXXXXX&grant_type=client_credentials";
$access_token = file_get_contents($token_url);
if($REQIDs){
$requests = explode(',',$REQIDs);
foreach($requests as $request_id){
$deleted = file_get_contents("https://graph.facebook.com/$request_id?$access_token&method=delete");
//not working ->> {"error":{"type":"GraphMethodException","message":"Unsupported delete request."}}
$deleted = file_get_contents("https://graph.facebook.com/".$user_profile['id']."_".$request_id."?".$access_token);
//not working ->> false
$deleted = $facebook->api($request_id."?".$access_token, "DELETE");
//not working ->> Fatal error: Uncaught GraphMethodException: Unsupported delete request.
}
}
Anyone have some ideas or see the error?
Take a look at this topic here >>> Topic. It explains pretty fully how to handle apprequests.
Your code should look like this:
$reqId = $_GET['request_ids']; // Get the id of the current request
$requests = $facebook->api('/me/apprequests/?request_ids='.$reqId); //Get the request. Not sure if this is correct for specific request
$itemData = $requests[data][0][data]; //Get the data that was originally sent in the request
//Some code here to do whatever with the data
$delete_url = "https://graph.facebook.com/".$reqId."?access_token=".$token."&method=delete";
$result = file_get_contents($delete_url); //Delete the request so there is only one there next time.
This is supposed to do the work.
Good luck!
Facebook will pass you the request ids when your user clicks through. You can then make a call to this url to delete the request:
https://graph.facebook.com/[request_ids]?access_token=[your_app_accessToken]&method=delete
Note that if your application has sent a few requests to a single user, when they click the Notification Jewel in their FB header menu, your code will be sent all request ids in a comma separated string. You will need to code around this fact. I think I'm right in saying that the order of the ids in this string is oldest -> newest so to delete the most recent, you need to parse out the last id in the string and delete it.

Are data acquired from Facebook GRAPH API real-time?

I am creating a Facebook application that will download all the photos in an album. The application is created for my personal use and at the same time, learn the Facebook API and JSON.
I can already retrieve the URL of the photos inside an album by calling this url:
http://graph.facebook.com/[album id]/photos?fields=source
The album that I'm trying to download contains 5400+ photos so I tried increasing the limit by adding the limit parameter:
http://graph.facebook.com/[album id]/photos?fields=source&limit=1000
Here's the problem:
The results being returned are only until 2010-07-30T11:20:11+0000. When I tried to modify the query by using the until parameter like so:
http://graph.facebook.com/[album id]/photos?fields=source,link&limit=1000&until=2010-06-01
the data responded correctly. However, if I changed the date to something like 2010-08-05, the latest photo returned will have a created_date of 2010-07-30T11:20:11+0000.
The last photo returned is photo #5000 out of 5695.
Here's my question:
Is the data acquired from Facebook GRAPH Api real-time (or a Monthly update, 2010-07-30)? Or there's just a limit on the number of photos returned on album (like 5000)?
Thanks!
EDIT
There is a 5000 object limit in Facebook. If you know how to break the limit, go here:
Breaking the 5000 object limit in Facebook API
Thanks!
There is indeed 5000 limit on returned objects. You would need to run multiple FQL queries on photo table ordering and limiting the results (<5000) to get all the data (check photo table page for examples). (doesn't work)
When I look at the photos in the graph query you linked
https://graph.facebook.com/119403264763178/photos
I see paging information at the bottom. So, hacking together a quick test
$request = 'http://graph.facebook.com/119403264763178/photos?fields=source&limit=1000';
$response = json_decode( file_get_contents( $request ), true );
$totalCount = 0;
while ( count( $response['data'] ) )
{
echo 'Fetching ' . urldecode( $response['paging']['next'] ) . '<br>';
$totalCount += count( $response['data'] );
$response = json_decode( file_get_contents( $response['paging']['next'] ), true );
}
echo $totalCount;
It would seem that even following the paging data, you can still only retrieve 5000 records.
I'd suggest hitting up the FB forums or opening a bug ticket.

Powershell - Increase the timeout for retrieving XML from a URL

I'm trying to retrieve an XML stream from a URL. For most URLs my code below works fine. But, I have a couple URLs that timeout. The URLs in question do work from Internet Explorer.
$webclient=New-Object "System.Net.WebClient"
[xml]$data=$webclient.DownloadString($url)
So, I went searching for a way to increase the timeout period. From what I've read, I believe I cannot do this using System.Net.WebClient. I think I need to use System.Net.WebRequest instead, but I cannot get it to work. The code I've been working on is below:
$myHttpWebRequest = [system.net.WebRequest]::Create($url)
$myHttpWebRequest.Timeout = 600000
$myHttpWebResponse = $myHttpWebRequest.GetResponse()
$sr = New-Object System.IO.StreamReader($response.GetResponseStream())
[xml]$xml = [xml]$sr.ReadToEnd()
The URLs I'm trying to access are internal to my company, so I can't post them. But, they do work in IE and the actual URL should be irrelevant.
Ideas?
EDIT: Preliminary testing shows that adding $myHttpWebRequest.AuthenticationLevel = "None" works. Thanks Scott Saad.
By default the WebRequest.AuthenticationLevel is set to MutualAuthRequested, therefore it will wait for some type of authentication response. Therefore, a timeout is probably being exceeded while waiting for the authentication to occur. It didn't look like you were messing with the Credentials so unless you require authentication, you probably won't need this. Try something like the following after you create your WebRequest:
$myHttpWebRequest.AuthenticationLevel = "None"
I hope this helps solve the problem.