I have Page Id "252079588308065" in facebook. I just want to get the number of likes of this page using graph API or FQL.
Is it possible to get number of likes count of a particular page.
Please provide me a solution
Here is a way of getting the like count using Graph API.
<?php
//The following code returns the Number of likes for any facebook page.
//Page Id of TechRecite.com. Replace it with your page.
$page_id = "138564926335348";
$likes = 0; //Initialize the count
//Construct a Facebook URL
$json_url ='https://graph.facebook.com/'.$page_id.'';
$json = file_get_contents($json_url);
$json_output = json_decode($json);
//Extract the likes count from the JSON object
if($json_output->likes){
$likes = $json_output->likes;
}
//Printing the count
echo 'TechRecite.com has '.$likes.' Facebook Fans';
?>
Source: http://www.techrecite.com/get-facebook-likes-count-of-a-page-using-graph-api/
Related
I used following code to access facebook page posts.
$accessToken = 'EAAlGssgdgQIBAHuD9ZBZB6RWZClM3bmm8Vjv2nZBNmotTTnubgzdK4aiHbqJwhRlELjAurPEHKSqxJS7c0Pyd5ZBuqZAo2keabbkubx0AZCl3m6brDGlkXNgMq9dtNUZAx4P6QwdsXwNvJaEi2j3YDsHpZABiRxRK6qMAmZAyynLvJNCJ41ZBn9se28QUsDHG72mhZCzHFLpLQUxZCAZDZD';
$id = '109395947376896';
$url = "https://graph.facebook.com/$id/posts?access_token=$accessToken";
$result = file_get_contents($url);
$decoded = json_decode($result, true);
var_dump($decoded);
and get the following result. screenshot is attached
how do I get images of posts??
Without specifying the fields, you will only get default ones. This is how you can get fields (checkout the API docs for Posts about available ones):
$url = "https://graph.facebook.com/$id/posts?access_token=$accessToken&fields=full_picture,message,...";
Source: https://developers.facebook.com/docs/graph-api/reference/post/#fields
You can get them in field parameters
picture
full_picture
child_attachments
Also refer https://developers.facebook.com/docs/graph-api/reference/post/
I am using the Foursquare API to request information for my users.
It says to make a request to get user info like this,
https://api.foursquare.com/v2/users/self?oauth_token=TOKENHERE
So I am doing it like this,
$fsUser = file_get_contents("https://api.foursquare.com/v2/users/self?oauth_token=".$access_token);
When I var_dump I get null from the value I am saving it in, Here is what my full code looks like,
<?php
$client_id = "iwdhwouchweohcuwoehcowehcu";
$secret = "ojdwojwjwrhvo";
$redirect = "http://www.example.com";
if($_GET['code']){
//We need to hit up the authkey URL and get the key in JSON format
$authkey = file_get_contents("https://foursquare.com/oauth2/access_token?client_id=".$client_id."&client_secret=".$secret."&grant_type=authorization_code&redirect_uri=".$redirect."&code=".$_GET['code']);
//We then need to decode it and store that key in a variable
$auth = json_decode($authkey,true);
$access_token = $auth['access_token'];
//we then look up whatever endpoint of the api we want
$fsUser = file_get_contents("https://api.foursquare.com/v2/users/self?oauth_token=".$access_token);
$user = json_decode($fsUser, true);
// $name = $decoded_userinfo['response']['user']['firstName'];
}
?>
When I var_dump $fsUser I get bool(false)
I have var_dump every variable with no issues till I get to $fsUser I can not get past this part...
You also need to use the version number (v) with your request.
Like this
https://api.foursquare.com/v2/venues/search?client_id=CLIENT_ID&v=20140806&m=foursquare
See documentation here.
https://developer.foursquare.com/overview/versioning
Suddenly, the FQL Query does not work, I have this code for so many months and kept it running, now it doesn't work, even my past projects that has this code malfunctioned.
For example,
I have a fan page name called Sample Page One and it has the id of 540109632726307
So, I want to do is, retrieve the name of the fan page using the fan page id.
Query
https://graph.facebook.com/fql?q=SELECT+name+FROM+page+WHERE+page_id=540109632726307
Code
$fbp_id = 540109632726307; //Sample FB ID
$fql_query_url = 'https://graph.facebook.com/'.'fql?q=SELECT+name+FROM+page+WHERE+page_id= '.$fbp_id.'';
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
var_dump($json_output);
When I run the code, it results to NULL, it wasn't like that before. Did facebook updates the fql query thing? I can't also seem to find any news about it.
But when I run this link to the browser,
https://graph.facebook.com/fql?q=SELECT+name+FROM+page+WHERE+page_id=540109632726307
It displays the result of the fql query that I made
{
"data": [
{
"name": "Sample Page One"
}
]
}
It's a valid url. Is there something wrong with my code?
Three things-
Define fb_id as:
$fbp_id = "540109632726307"
(since its a big integer)
Leave no space after = in :
page_id= '.$fbp_id
There's no variable $json_output, use $fql_query_obj in var_dump instead.
So your code will look like:
$fbp_id = "540109632726307"; //Sample FB ID
$fql_query_url = 'https://graph.facebook.com/'.'fql?q=SELECT+name+FROM+page+WHERE+page_id='.$fbp_id.'';
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
var_dump($fql_query_obj);
Try this out.
Why the concatenation of two strings in $fql_query_url? It could be just one string.
You also do not need to concatenate the single quotes at the end.
Did you try to get the file via the api.facebook.com url?
e.g.
$fbp_id = "540109632726307"; //Sample FB ID
$fql_query_url = 'https://api.facebook.com/method/fql.query?query=SELECT+name+FROM+page+WHERE+page_id='.$fbp_id;
$fql_query_result = file_get_contents($fql_query_url);
$fql_query_obj = json_decode($fql_query_result, true);
var_dump($fql_query_obj);
Facebook is very inconsequent. Sometimes things work with graph.facebook.com and sometimes you need to use api.facebook.com.
I have a group on Facebook where the users post funny pictures. I am developing a website that will present the posted pictures in a more organised and interesting way.
My approach is to use Facebook OpenGraph API.
I would like to know how I can obtain the first posts. Eg: the first 10 posts.
By default the graph API (https://graph.facebook.com/{group_id}/feed/) returns the posts sorted from LAST TO FIRST.
I have read the page about Pagination (http://developers.facebook.com/docs/reference/api/pagination/). So, I know about offset and limit parameters.
There appears to be no method of sorting the feed in any other order.
The only approach I can see it to download the whole feed and take what you need...
// Set your access token here...
$accessToken = 'XXX';
// Set your GroupID here...
$groupId = '123';
// Set the number of feed items required here...
$qtyRequired = 10;
$url = 'https://graph.facebook.com/v2.2/' . $groupId . '/feed/?limit=100&access_token=' . $accessToken;
$feed = array();
while ($url) {
// Get the data from Facebook.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url
));
$data = json_decode(curl_exec($curl));
// Store the feed to $feed.
if (is_array($data->data)) $feed = array_merge($feed, $data->data);
// Load the next page or quit.
if (isset($data->paging->next)) $url = $data->paging->next;
else $url = false;
}
// Feed will contain the oldest feed items.
$feed = array_slice($feed, -$qtyRequired);
You might be able to speed it up by specifying the exact fields you need.
This script isn't working so good.
<?php
//include twitter class
require('classes/twitter.php');
$twitter = new Twitter('custToken', 'custSecret');
// set tokens
$twitter->setOAuthToken('blah blah');
$twitter->setOAuthTokenSecret('blah blah');
//$tweet = "This tweet was posted from a custom php script.";
//$twitter->statusesUpdate($tweet); // <-- this works!
$friends = $twitter->friendsList('tynamite');
foreach ($friends as $friend){
print_r($friend);
echo "<hr>";
}
?>
I want to display all my Twitter friends (everyone I follow) in a list.
The result I am getting is this.
Could anyone please help on this?
You should pass a cursor. You could use something like the code below (untested and just pseudo-code):
while($count == 20) {
$friends = $twitter->friendsList('tynamite', $cursor);
$count = count($friends);
$cursor++;
.. your code ..
}
twitter uses json to return its results. you need to parse the results returned and pull out the information you need. check this blog out for an example of how to use php to parse json