If exist a facebook fan page like this:
https://www.facebook.com/HuffingtonPost
I suppose to get likes count calling graph API:
https://graph.facebook.com/https://www.facebook.com/HuffingtonPost
Infact here I get:
{
"id": "https://www.facebook.com/HuffingtonPost",
"shares": 435839
}
On the other hand if I call
https://graph.facebook.com/HuffingtonPost
I get a more verbose output:
{
"id": "18468761129",
"name": "The Huffington Post",
"picture": "http://profile.ak.fbcdn.net/hprofile-ak-ash2/188072_18468761129_6398033_s.jpg",
"link": "http://www.facebook.com/HuffingtonPost",
"likes": 435832,
"category": "Website",
"website": "http://www.facebook.com/HuffingtonPost",
"username": "HuffingtonPost",
"company_overview": "The Internet Newspaper\nNews | Blogs | Video | Community",
"description": "The Huffington Post - The Internet Newspaper. - Company Overview: The Internet Newspaper News | Blogs | Video | Community | Facebook",
[... omissis ...]
}
Can anybody tell me what's difference between these two opengraph objects?
There is also a slight difference between number of shares and likes. Why?
Update:
During last days graph api returned also object type, so I realized that:
First API call returns an link_stat type object.
Second API call returns a page type object.
In first case shares count should represent sum of:
number of likes of this URL
number of shares of this URL (this includes copy/pasting a link back to Facebook)
number of likes and comments on stories on Facebook about this URL
number of inbox messages containing this URL as an attachment.
In second case like count represents only itself
May somebody confirm me shares count correctness?
For the breakdown between likes, shares and comments (which are added up and used as the "likes" number on the likes button, you're better off using FQL.
If you use OG, something like http://graph.facebook.com/http://example.com will show you:
{
"id": "http://example.com",
"shares": 3
}
... as you've noted above. If you use FQL, you can get the breakdown of each.
<?php
// require the php sdk
require_once 'facebook-php-sdk/src/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'cookie' => true,
));
$external_result = $facebook->api(array(
'method' => 'fql.query',
'query' => 'SELECT share_count, like_count, comment_count, total_count, click_count FROM link_stat WHERE url="http://example.com";'
));
echo '<li>'.number_format($external_result[0]['like_count']).' likes, '.number_format($external_result[0]['share_count']).' shares';
echo '<pre>';
print_r($external_result);
echo '</pre>';
?>
This will display something on-screen like:
* 1 likes, 2 shares
Array
(
[0] => Array
(
[share_count] => 2
[like_count] => 1
[comment_count] => 0
[total_count] => 3
[click_count] => 0
)
)
Also, SA now has a Facebook-specific site that may be helpful to you. :) facebook.stackoverflow.com
First one is something that tells you how many likes selected url have.
Using second one you will get information about Page Object through page identifier
Related
Now I posting a single photo to wall like this:
$response = $facebook->api("/$group_id/photos", "POST", array(
'access_token=' => $access_token,
'message' => 'This is a test message',
'url' => 'http://d24w6bsrhbeh9d.cloudfront.net/photo/agydwb6_460s.jpg',
)
);
It works fine, but can I somehow post a multiple photos, something like this:
You can now publish multiple images in a single post to your feed or page:
For each photo in the story, upload it unpublished using the {user-id}/photos endpoint with the argument published=false.
You'll get an ID for each photo you upload like this:
{
"id": "10153677042736789"
}
Publish a multi-photo story using the {user-id}/feed endpoint and using the ids returned by uploading a photo
$response = $facebook->api("/me/feed", 'POST',
array(
'access_token=' => $access_token,
'message' => 'Testing multi-photo post!',
'attached_media[0]' => '{"media_fbid":"1002088839996"}',
'attached_media[1]' => '{"media_fbid":"1002088840149"}'
)
);
Source: Publishing a multi-photo story
You can make batch requests as mentioned here: https://stackoverflow.com/a/11025457/1343690
But its simple to loop through your images and publish them directly.
foreach($photos as $photo)
{
//publish photo
}
Edit: (regarding grouping of photos on wall)
This grouping is done by facebook automatically if some photos are uploaded into the same album.
Currently you cannot create an album in a group via Graph API - it is not supported (as of now), see this bug.
But you can do this - create an album manually, then get the album_id by-
\GET /{group-id}/albums, then use the the code with album_id instead of group_id-
foreach($photos as $photo){
$facebook->api("/{album-id}/photos", "POST", array(
'access_token=' => $access_token,
'name' => 'This is a test message',
'url' => $photo
)
);
}
I've tested it, see the result-
Actually you can upload a multi story photo(I did it using Graph Api and PHP) but the problem comes if you need scheduled this post.Your post is schedule but also it shows on the page's feed.
P.S. I'm using Graph Api v2.9
PHP Code
$endpoint = "/".$page_id."/photos";
foreach ($multiple_photos as $file_url):
array_push($photos, $fb->request('POST',$endpoint,['url' =>$file_url,'published' => FALSE,]));
endforeach;
$uploaded_photos = $fb->sendBatchRequest($photos, $page_access_token);
foreach ($uploaded_photos as $photo):
array_push($data_post['attached_media'], '{"media_fbid":"'.$photo->getDecodedBody()['id'].'"}');
endforeach;
$data_post['message'] = $linkData['caption'];
$data_post['published'] = FALSE;
$data_post['scheduled_publish_time'] = $scheduled_publish_time;
$response = $fb->sendRequest('POST', "/".$page_id."/feed", $data_post, $page_access_token);
$post_id = $cresponse->getGraphNode()['id'];
You will need to upload each photo first with published state to false, and then use the ID's of the unpublished photos to the /me/feed endpoint to schedule the photo. The schedule needs to be within the 24 hours from the time the photos are uploaded as facebook deletes all unpublished photos in 24 hours.
Ref:
https://developers.facebook.com/docs/graph-api/photo-uploads/
There is no way to publish more than one photo in the same graph API call.
See documentation: https://developers.facebook.com/docs/graph-api/reference/user/photos
I need to list users who like specific url or at least facebook page
I tried using both PHP and https://api.facebook.com/method/fql.query
I can see the number of likers for the URL
$arr[1]['url'] = 'http://example.com/test/test3.php?ref=gametest&fb_ref=gametest&fb_source=gametest';
$arr[1]['object_id'] = '10150930574980642';
require_once("facebook.php");
$facebook = new Facebook(array(
'appId' => '325617367526193',
'secret' => '6c40e01dd717431c9b7ec6ce68bf0c94',
));
I used http://developers.facebook.com/docs/reference/fql/like/ example to form similar query
$query = <<<FQL
SELECT user_id FROM like WHERE object_id="10150930574980642"
FQL;
$fql_query_url = 'https://graph.facebook.com/fql?q=' . rawurlencode ( $query );
$fql_query_url .= '&access_token=' . rawurlencode ( $facebook->getAccessToken() );
but the resulting link gives no data at all
{
"data": [
]
}
The same result via link with or without access_token
api.facebook.com/method/fql.query?query=SELECT%20user_id%20FROM%20like%20WHERE%20object_id%3D%2210150930574980642%22
The "like" table will only return the IDs of users who like the following objects (video, note, link, photo, or album).
You cannot find out the user Ids of people who like a particular page, only objects within that page.
e.g. The likes for an album on the Facebook Page (SELECT user_id FROM like WHERE object_id="10150146071791729"&access_token=[access_token])
If you want to get the users that have liked a particular link, you will need to get the object_id of this link first:
https://api.facebook.com/method/fql.query?query=SELECT url,site,id FROM object_url WHERE url IN ('http://developers.facebook.com')
This will return the object_id of the link http://developers.facebook.com.
When I post a status through the graph api, I can then retrieve the status through "me/feed" or "me/home". But now the newly posted status can only be retrieved through "me/home".
By checking the status's origin data, I found the posted status has the following privacy:
"privacy": {
"description": "Friends",
"value": "ALL_FRIENDS"
},
When I post message through Facebook WEB, I can choose the privacy, which can be one of PUBLIC, FRIENDS, ONLY ME or CUSTOMER. Can I do the same thing with the Graph API?
Do you mean something like:
$wallpost = $facebook->api('me/feed','post',array(
'name' => 'Headline',
'message' => '',
'privacy' => array(
'value' => 'CUSTOM',
'friends' => 'EVERYONE'
)....
I try to read the list of people who likes a page, but it looks like it is not possible with the Graph API or FQL. I found lots of entries of people trying it, but nobody found a solution. Is it possible?
Here I get the profile info, I see the field "likes" with the number 200
https://graph.facebook.com/162253007120080/
But when I try to read it, I get an empty data array?
https://graph.facebook.com/162253007120080/likes
Anybody an idea how to handle this?
HI,
here you go, streight out my tool box:
<?php
require_once('../library/Facebook.php');
$facebook = new Facebook(array(
'appId' => '123456789000000',
'secret' => 'asdf',
'cookie' => true,
));
$result = $facebook->api(array(
'method' => 'fql.query',
'query' => 'select fan_count from page where page_id = 1234567890;'
));
echo $result[0]['fan_count'];
?>
Call the graph api link. Extract the contents using json_decode.
The resulting data array isn't empty when you go there, it is:
{
"error": {
"message": "An access token is required to request this resource.",
"type": "OAuthException",
"code": 104
}
}
Which means you need an access token in order to access that LIKE data..
I want to retrieve all of the posts from a given facebook page along with the associated comments.
I wrote this code (app details obfuscated, replace with your own in order to run it).
<?php
require_once('facebook.php');
$facebook = new Facebook(array(
'appId' => 'MY_APP_ID',
'secret' => 'MY_APP_SECRET',
'cookie' => true,
));
$pages = array(
"stackoverflow" => 11239244970
);
$result = $facebook->api(array(
'method' => 'fql.multiquery',
'queries' => '{
"posts": "select post_id, source_id, actor_id, target_id, likes, message from stream where source_id = '.$pages["stackoverflow"].'",
"comments": "select post_id, text, username, fromid from comment where post_id in (select post_id from #posts)"
}'
));
echo json_encode($result);
?>
posts returns the expected results, but comments returns just one comment.
This example queries the stackoverflow facebook page
The comment returned from the comments query is "Joined!" (from this post). I can't figure out what's special about this comment.
Any thoughs?
I tried to find a solution playing with Graph Api
https://graph.facebook.com/me/fql?q=select message, comments from stream where source_id = 11239244970
is working but when returning comments it returns only the last 2. It's exaclty how Facebook itself shows the stream and comments with "View all XX comments" link.
To query the posts and comments together can be used:
https://graph.facebook.com/fql?q={"posts":"select post_id,message, comments from stream where source_id = 11239244970","comments": "select post_id, text, username from comment where post_id in (select post_id from #posts)"}&access_token=...
According to Facebook:
Each query of the stream table is limited to the previous 30 days or
50 posts, whichever is greater, however you can use time-specific
fields such as created_time along with FQL operators (such as < or >)
to retrieve a much greater range of posts.