how to get post_id of a Facebook post (programmatically) - facebook

How to get post_id of a Facebook post programmatically, for example, I see a post on my wall, what is the best way to get its post_id programmatically.
thanks Gurus.

This is obviously extremely difficult to due since the nature of facebook's feed and the framework it uses (reactjs). They do not want you to get post_ids and they do not want you to mimic their service. The most you'll get is id's like r.2.15, etc, which will mean nothing to you.

See, whenever you post a comment (using Graph API), you get the post_id in return.
But if you want the post id of a feed that was not published using Graph API, you can do-
get all the feeds with \GET /user-id/feed You'll get message, description, id and other details
check for for your post and get the id

You can get the post ids using graph api. If you have a valid access token you can try this below code to get the post ids. Here we are using page /feed to get the posts. you can use page /posts to get only page posts.
$fb_page = ''; //your page name
$access_token = ''; //your access token
$url = "https://graph.facebook.com/v2.2/".$fb_page.'/feed?access_token='.$access_token;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
$details = json_decode($result,true);
$posts = $details['data'];
foreach ($posts as $post)
{
echo "<p>".$post['id']."</p>";
}

Related

How to get URL of the newly posted Facebook post via Graph API from returned response?

This:
$graphNode = $response->getGraphNode();
echo $graphNode['id'];
will output an id (a long number) of the newly created post.
How to get the URL where the new post is located form $graphNode?
Not sure what you mean with URL... If you want to query the Graph API for object details, you should be able to use
$request = $fb->request('GET', $graphNode['id']);

How can you use data from facebook's graph API without having to login?

I am creating a personal website about myself and i would like to have it pull images and wall posts from facebook and display them on the page, at the moment this all works fine.....for one hour which is how long an access token lasts for.
I dont want to use access_tokens because the posts and profile picture are public and the point of this website is that it updates itself as i update my facebook profile
Can the facebook API do what i ask of it (which is to allow the photos to be pulled all the time without fear of expiration or even having to log in), or does the facebook API not allow that and is only really used for people wanting to use thair facebook account on your website to comment or like?
I hope i was clear enough...
to summerize, i would like to my website to pull pictures of my profile when people visit the website and not have to deal with access_tokens
If you configure your "Facebook app" as a "web application" you will get an AppID and an AppSecret. Using these values you can dynamically request a new token every time your page loads and use that token for all of your requests. I believe facebook will return the same token if you continue to request a new token before the old one has expired.
Here is a PHP function to achieve this:
public static function GetNewAccessToken(){
try{
$url = "https://graph.facebook.com/oauth/access_token?client_id=" . \Core\Config::FB_APPID . "&client_secret=" . \Core\Config::FB_APPSECRET . "&grant_type=client_credentials";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$response = curl_exec($ch);
curl_close($ch);
//response will be access_code=<CODE> so we need to strip off the access_code part at the front/
$token = "";
if (isset($response) && $response !== FALSE && substr($response, 0, 13) === "access_token="){
$token = substr($response, 13);
}
return $token;
}
catch(\Exception $exc){
return "";
}
}

Created album as a page but can't upload Photos using Graph API

I have searched intensively but I only can find similar topics but no one leads me to a working solution.
As my topic says i created an album as a page using following code.
$page_token = $facebook->api('/'.$page['id'],'GET',array('fields'=> 'access_token'));
$facebook->setAccessToken($page_token['access_token']);
$facebook->setFileUploadSupport(true);
$args = array(
'message'=> 'test album',
'name'=> 'Test Album'
);
$ch = curl_init();
$url = 'https://graph.facebook.com/'.$page['id'].'/albums?access_token='.$page_token['access_token'];//APP_ID.'|'.APP_SECRET;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
$newAlbum =json_decode($data,true);
$newAlbumID = $newAlbum['id'];
Ok that works so far, I am receiving an album ID. but when I look into the JSON object or look up graph.facebook.com/ALBUM_ID i receive nothing even on the page there is no album.
I have manage_pages read_stream photo_upload publish_stream create_event rsvp_event permissions. Creating an event on the page works pretty fine. What I am doing wrong?
When I try to upload some pictures and create albums it only works for my own profile but I would need it for my page. I don't understand why I am receiving an ID which isn't working when I look it up.
I hope you can help me guys.
Hello everybody I think I have solved my problem. At first it's important to know I have tested nad tried some weeks to finally get the solution.
As I said I was able to create Events and Questionings on a page. Today I compared creating an album on my profile with creating one on a page and I got to know that this also works fine for both.
But when I tried to upload a photo into the album or on the page wall it failed with this lovely exception: "An unexpected error has occurred. Please retry your request later"
Today I tried to remove some permissions. as you can see below
OLD --> define('PERMISSIONS', 'offline_access read_stream photo_upload user_photos friends_photos user_photo_video_tags publish_stream read_friendlists ads_management create_event user_online_presence friends_online_presence rsvp_event manage_pages');
NEW --> define('PERMISSIONS', 'photo_upload user_photos friends_photos publish_stream create_event manage_pages');
and now it works!

Can i get all my posts(profile feed) using Facebook Graph API explorer?

Can i get ALL my posts(my Profile feed) using Graph API explorer?
I tried using https://graph.facebook.com//posts?limit=500
but it doesn't seem to give me everything(even increasing the limit doesn't work) which
means it given only a predefined number of posts.
Is there a way to get all posts by me from the time i joined Facebook(because i can get them by scrolling down on my Wall which means facebook has stored them).
Thanks in advance
Get Access Token
http://developers.facebook.com/tools/explorer
Then
https://graph.facebook.com/YOUR_ID/feed?access_token=FACEBOOK_ACCESS_TOKEN
This post might also be helpful:
How to show facebook feed messages from my site without access_token?
You do not need an access token!
$url = 'url_pulled_from_database';
$url = str_replace('http://www.', 'http://graph.', $url);
$get_id = file_get_contents($url);
$get_id = json_decode($get_id, true);
$fbID = $get_id['id'];
//THEN CALL THE FUNCTION
fb_parse_feed($fbID, $maxnumber);
I cant take credit for the fb_parse_feed function, but you can find it here
https://gist.github.com/banago/3864515

Get Facebook user's profile picture prior to authenticating app

Summary:
I am working on a website that connects to Facebook through the Graph API. I was asked whether I can get a user's profile picture if they are already logged into Facebook, but prior to the user authenticating the app. Is this possible?
EDIT:
I know you can get people's profile picture by going to http://graph.facebook.com/USERNAME_OR_USERID/picture?type=large, but would I be able to get a user's username or ID if they are logged into Facebook, but have not authorized my app?
Details:
According to the Facebook API User documentation, you can get their Facebook ID without an access_token, and I know if you go directly to a user's /me URL you can see their basic information. With the Facebook ID, I can grab their profile pic.
However, when I try to call it from the website using the following code, I get an OAuthException error.
FB.api('/me', function(response) {
console.log(response);
});
I'm guessing that they have to authenticate the app, but I'm looking to see if I may have missed a way of getting their profile pic.
Thanks for your help!
You can get everyone's Facebook profile picture without authentication.
You just call http://graph.facebook.com/USERID/picture?type=large.
This URL redirects you to image URL.
Edit: Querying by username is not allowed anymore, so I updated USERNAME_OR_USERID as USERID.
yes you can get..
http://graph.facebook.com/naseer.panhwer/picture?type=normal
http://graph.facebook.com/naseer.panhwer/picture?type=small
http://graph.facebook.com/naseer.panhwer/picture?type=large
You can generate an access token using an application id and secret as follows:
https://graph.facebook.com/oauth/access_token?client_id={appID}&client_secret={appSecret}&grant_type=client_credentials
NOTE: Don't do this on the client side as you risk having your application getting hijacked.
The resulting token can be user to query users with the graph API without a login prompt.
How to get FB images w/o oauth or access tokens:
I was trying to get only images of few members but I was stuck then wrote this snippet and worked for me,
The HTML tag
<img src="getFbImage.php?id=<fbid_of_the_person>&type=[small|large]" />
The PHP code
<?php
$remoteImage = !isset($_GET['id']) ? "../images/default.png" : getImg($_GET['id']);
$imginfo = getimagesize($remoteImage);
header("Content-type: ".$imginfo['mime']);
readfile($remoteImage);
function getImg($id){
$dom = new DOMDocument;
$dom->loadHTML(getData($id));
$tags = $dom->getElementsByTagName('img');
$img = '';
foreach ($tags as $tag) {
$img =$tag->getAttribute('src');
if($img=='')
continue;
else if(isset($_GET['type']) && $_GET['type']=='large')
break;
}
//This iteration has a lot of images try your luck.
return $img;
}
function getData($id){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://www.facebook.com/photo.php?fbid='.$id);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 Firefox/39.0');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
I only had to use a 30x30 image and I got what I wanted. Check out it also provides you a large image.
Cheers!
Joy