Facebook API Changes? PHP Get Page Count Error - php

Facebook API Broken?
Ive got this code ive been using for a while now that looks at a facebook page and reports back how many fans the page has. However over the past few days its stopped working and im now getting the following error. Can anyone shed any light on why this is happening?
Error:
Warning: file_get_contents(https://graph.facebook.com/themeparkguide?access_token=2007*********3|9b5***********fcd&fields=fan_count): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /homepages/28/d541****/htdocs/*********project/facebook_stats.php on line 4
0
Code:
<?php
function fbLikeCount($id,$appid,$appsecret){
$json_url ='https://graph.facebook.com/'.$id.'?access_token='.$appid.'|'.$appsecret.'&fields=fan_count';
$json = file_get_contents($json_url);
$json_output = json_decode($json);
//Extract the likes count from the JSON object
if($json_output->fan_count){
return $fan_count = $json_output->fan_count;
}else{
return 0;
}
}
echo fbLikeCount('coregenie','___APPID___','___APPSECRET___');
//https://stackoverflow.com/questions/37572559/facebook-graph-api-read-followers-count/42336057
?>

It works in the API Explorer: https://developers.facebook.com/tools/explorer/?method=GET&path=themeparkguide%3Ffields%3Dfan_count&version=v2.12
So i assume with some error logging, you would get the error that your App is inactive. Try with a different or new App instead. This should not be affected by the recent changes.
Side note: You should not use that script on every page load, better cache the result for some time. If a lot of users visit the page in a short time, you will hit the rate limit.

The Facebook API has changed .
The fan_count can be retrieved using an user token only after aprovement by Facebook Platform, completing the submission process .
The fan_count can be retrieved using page access tokens, to get fan count for that page .
Attention: Graph Explorer can get the page information if yours pages, so, be carefull on testing your apps, and use TEST USERS for testing insead your profile, ok ,
After test submit it for aprooval, and you be done !

Related

Why do I receive an error 500 when trying to access Paysafe Api server?

The Paysafe API was working perfectly fine in localhost, I was able to complete payment to Netbanx. I started to integrate the system on the website. I have a page for billing information, then a page for card payment where I use paysafe.js to create a token.
Then, I use PHP to get response from the server. This works in local. But online, this last part where I try to settle a payment, I get an error 500. I think it could be because the server is not using HTTPS. I want to know if it's possible that the error 500 is coming from the fact we don't have HTTPS or if it's something else?
P.S: It's complicated to access to the server because of bureaucracy, I don't want to make all the process if it's sure it's not that!
Thank you!
P.S.: I also tried using curl instead, and the response was bool(false).
require_once("config.php");
use Paysafe\Environment;
use Paysafe\PaysafeApiClient;
use Paysafe\CardPaymentService;
use Paysafe\CardPayments\Authorization;
$client = new PaysafeApiClient($paysafeApiKeyId, $paysafeApiKeySecret, Environment::TEST, $paysafeAccountNumber);
$info = new Authorization(array(
//PAYMENT ARRAY (Getting POST variable from previous page)
));
$response = $client->cardPaymentService()->authorize($info);
$statut = $response->status;
That Environment::TEST obviously does not match the production environment (or host-name).
Just enable PHP error reporting for your IP only, in order not to possibly leak any details.
Also check the console there (if any), if that host if even authorized to access the API.
I mean, HTTP500 is an error description just alike "it does not work".

Facebook PHP SDK get page posts data is empty

I'm having a strange problem with the Facebook PHP SDK,
$response = $this->getConnection()->get("me/posts");
$feedEdge = $response->getGraphEdge();
var_dump($response); exit;
the getConnection() call provides me with a \Facebook\Facebook object with the default_access_token set but this works as I get a response from Facebook
But the body from the above var_dump gives:
["decodedBody":protected]=>
array(1) {
["data"]=>
array(0) {
}
}
What is really strange is when I use the Graph Explorer (as my Application and using the same Page) I see all the posts. So I thought maybe it was the access token was not working correctly so I copied and pasted the access token shown in from the graph explorer into my get() call to override the default like below unfortunately this did not work and I got exactly the same output.
$response = $this->getConnection()->get("me/posts", "EAA....");
$feedEdge = $response->getGraphEdge();
var_dump($response); exit;
So I'm unsure why the same token in one place would get the information when in another it would get an empty set the worst part is it's not like the Request is failing as that throws an exception it is like Facebook Graph API is reacting differently for my application when using the PHP-SDK verses using the Explorer.
So after hours of messing around testing stuff in the Graph API Explorer and the API I worked out, there is a problem with /me/posts endpoint when using the PHP SDK even if your using a manage_pages access_token it seems to lock on to the user that instead of the page unlike the graph explorer that the /me/posts, posts to the entity of the access_token.
So make sure to request /{page_id|user_id}/posts and it works correctly.

How to read facebook FriendsList using PHP?

I created Facebook app from developers.facebook.com. I have App ID and App Secret.
I given following permissions:
read_stream, manage_friendlists, user_friends
I used following code to read my facebook id and name:
$userdetails = file_get_contents(
"https://graph.facebook.com/v2.0/me?fields=id,name&access_token=".$access_token);
I got id and name in JSON format.{"id":"my_id","name":"Sathish Kumar"}
I used following code for get friendslist
$userdetails = file_get_contents("https://graph.facebook.com/v2.0/me?fields=id,name,friends&access_token=".$access_token);
But, I got Warning message
Warning: file_get_contents(https://graph.facebook.com/v2.0/me?fields=id,name,friends&access_token=my_access_token)
[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
How to get friends list using facebook api?
This code work fine for me. Check your access token is valid
$userdetails = file_get_contents("https://graph.facebook.com/v2.0/me?fields=id,name&access_token=".$access_token);
The correct endpoint would be /me/friends, you need to make another API call for that. I´d highly recommend using CURL for that instead of file_get_contents.
Keep in mind that you will only get friends already using the App, see changelog: https://developers.facebook.com/docs/apps/changelog

Facebook Credits - Getting error code 13 when trying to get credit balance

I'm trying to get a user's credit balance with this code:
$obj = json_decode(
file_get_contents('https://api.facebook.com/method/users.getStandardinfo'.
'?uids='.$facebook_uid.'&fields=credit_balance&access_token='.
''.$access_token.'&format=json'));
I get error code 13 with this message:
The underlying FQL query made by this API call has encountered the following error: credit_balance is not a member of the user table.","request_args".
I've approved for credits in my game a lot of places and 95% sure on, that I've been accepted.
What to do?
Simon, if you have re-authenticated the user and retried then it is likely an issue on our end. Can you post your application ID and I will take a look.
I had the same problem. There are couple of things you need to do to get facebook credit balances in your app.
Get your App whitelisted by Facebook so you can call this API.
Use the code below to get the credit balances.
Facebook's own documentation didn't work either. (No surprise there) Instad I used the code below.
$accessTokenURL = 'https://graph.facebook.com/oauth/access_token&client_id=' .$app_id .'&client_secret=' .$secret .'&grant_type=client_credentials';
$accessToken = file_get_contents($accessTokenURL);
$creditsInfoURL = 'https://api.facebook.com/method/users.getStandardinfo?fields=credit_balance&format=json&uids=' .$query_uid.'&' .$accessToken;
$creditsInfo = file_get_contents($creditsInfoURL);
list($creditsArray) = json_decode($creditsInfo, true);
$fbcredbalance = 0;
if(isset($creditsArray['credit_balance'])) {
$fbcredbalance = $creditsArray['credit_balance'];
}

Facebook Photo Upload into Album on Fan Page failed

I have written an Application what posts Photos on a FanPage into a Specific Album long time ago.
Now I didn't used for a half year. Now I had to set up the Application again and grant the extended Permissions to the Fan Page (streamm_publish) again.
But now I've a Problem, I'm using the old REST API what now gives me the error: An unknown error ocurred with the error code 1.
Then I tried to post throught the Facebook Graph api.
I tried to make a call to the Api /pageid/albumid/photos what's not working(Unknown path components)
I tried to makea a call to /albumid_from_my_page/photos then the Photos were posted to my profile
I tried to upload it to /pageid/photos what is the same as the one above
But the code fpr the REST Api worked well, what's the problem there, and why the new Graph Api isn't working how she should?(BUG?)
To post a photo to an album, this is the code:
$post_data = array(
"message" => "My photo caption",
"source" => '#' . realpath($file)
);
$album_id = "XXXXX";
$facebook->api("/$album_id/photos", 'post', $post_data);
Now I suppose to interact with the page albums you need a page access_token added to your $post_data array, for this check this answer.
You need take ACCESS_TOKEN page...
try:
http://graph.facebook.com/me/accounts?access_token= GET THIS TOKEN USING GRAPH API... OR MAKE USING the getAccessToken()...
and will see all pages and aplications that u have, find just application for this case and COPY TOKEN... and damn!!!
It's possible to see this token thought GRAPH API EXPLORER...
regards.

Categories