NOTE: This post was written using Facebook’s PHP SDK version 2.1.2. Since this post was made, the PHP SDK has changed and some of the process that are explained below may have changed as well. At some point I’ll have to revisit this post and update it but at this time just keep in mind of the above.
As promised, here is a post (similar to my Twitter API post) on using the Facebook API. There are many reason why one would want to access the Facebook API – maybe to create a mobile app that lets you post photos to your Facebook albums, or maybe you just want to show your last few Facebook status updates on your blog; what ever the reason may be, Facebooks Graph API mixed in with their PHP SDK makes it really easy to accomplish this.
Objectives
- Setup our environment
- Register an app on Facebook
- Understand the authentication process and extended parameters
- Understand Graph API
- Retrieve our latest status updates
- Add a new status update
- Retrieve our photos from our albums
- Add a new photo
Setup our Environment
You have two options here, you can access my github and download this project with all required files and library or you can follow this tutorial step by step – it’s really up to you. If you want to download all the files ahead of time, you can get them at this github repo. If you are going step by step, first create a folder (I’m calling mine facebook) in your server where you want this site, next create a folder named library in the root of this site, and finally add the following two files facebook.php and fb_ca_chain_bundle.crt in the library folder that you can get from the Facebook PHP SDK repo at: https://github.com/facebook/php-sdk/tree/master/src/.
Register an App on Facebook
I’m making the assumption you already have a facebook account and if you don’t, first create one. To create an app on Facebook, you first need to confirm your account by giving Facebook a phone number where they can send you a code. This may be a new process since I don’t remember having to do this a few months ago. Go to http://developers.facebook.com/setup and if your account is not verified, you’ll be prompted to add the information mentioned above. After verifying your accout, you’ll be taken to the ‘Create an Application’ page.

This is what the page should look like
Simply give your site a name and type in your site’s url and click create app. In my case I’m calling my app ‘Blog Test’ and my site would be ‘http://www.joeyrivera.com/’. Next you will be prompted to type in a captcha and continue. Now you are done setting up your app, you see a screen like the following with your app id and app secret:

Here is your app info
Authenticating and Extended Parameters
Facebook uses OAuth for authenticating involving tokens being send back and forth but we don’t have to concern ourselves with the details – the Facebook PHP SDK does it all for us. It makes sure to send all the required information in the proper format so Facebook gets what it needs and sends us back what we need. You can read more about authenticating with Facebook at their site and you can also read my Twitter API post that discusses the OAuth in a little more detail if you are interested. The SDK also takes care of handling all API calls for us so we only have to give it the resource we are trying to access, let it know if we want to do a get or post, and finally give it all the parameters required for the call.
Extended Parameters is an interesting way of restricting access to an app (I’m using the term app to describe what we are building here) through the API. Unless a user allows access to certain parts of their account, an app will not be able to modify nor read parts of the account even if they are authenticated. Extended Parameters need to be explicitly assigned at the point of authenticating. When the user is taken to Facebook and asked to allow access to their account, the user is also presented with all the various parts of their account this app wants to interact with. Here is an example of what the user will see when trying to authenticate with our app:
The first one in the screen shot is the default requested permission to access all the users basic information. The other three we need to accomplish our goals set including: read and post status updates as well as view and post photos. Here you can see a complete list of all the available extended permissions. If you access that link, you’ll see there are a lot of permissions. I recommend taking some time reading through their descriptions to get a better understanding of the kinds of things you can do with the API. There have been times where I’ve tried to do something with an app and the Facebook Graph API without success only to find out I hadn’t requested a required permission for that to work.
Time for code, what we are about to do is to create an array with our app id and app secret to feed to the Facebook class when we instantiate it. Next, we are going to check to see if we are authenticated and if not, redirect us to the Facebook authenticate page passing it the extended parameters we want access to. Below is the code for the config.php file. Download form my repository or create the file below on the root of your site:
config.php
$app_id = "yourappid";
$app_secret = "yourappsecret";
$facebook = new Facebook(array(
‘appId’ => $app_id,
‘secret’ => $app_secret,
‘cookie’ => true
));
if(is_null($facebook->getUser()))
{
header("Location:{$facebook->getLoginUrl(array(‘req_perms’ => ‘user_status,publish_stream,user_photos’))}");
exit;
}
Make sure to update the app_id and app_secret variables with the information you received from Facebook after registering your app. Let me give you a quick explanation of the code. First we include the facebook.php file that we downloaded from the Facebook PHP SDK github repository. Then we instantiate a facebook instance passing it the app id, app secret, and tell it to use cookies to store the session/token information after authenticating. Next we call getUser() to see if we are already authenticated – null means we are not, else we receive the users id. If we are not authenticated, we redirect the user to Facebook to authenticate. The url is provided to us from the getLoginUrl method. We are sending that method an array with a key req_perms (required permissions) and the value is a comma delimited list of all the extended permissions we want access to. Again, a complete list of all permissions can be found at Facebooks docs.
Go ahead and try to access this page in your browser. You should be immediately redirected to Facebook. If you are not already logged in with your Facebook account, you’ll first be asked to log in with the user which you want your app to access. Next you’ll see the ‘Request for Permission’ page, click allow and you’ll be taken back to the config.php page on your server. This process takes you back to the page which initiated the authentication which was the config.php page. We have now successfully authenticated with Facebook.
In testing your app, you may decide you need more or less extended permissions. While you are authenticated with Facebook and have your token stored in a cookie, you won’t be able to re-authenticate using this code since you’ll be getting a User object back so you have a few options: You can close your browser, clear cookies, and authenticate again after modifying your code; You can force another echo or header redirect to the getLoginUrl() again with the new parameters; or you can log into your Facebook account (the user you authenticated), click on account on the top right, privacy settings, and in the bottom left you’ll see application and websites, click on edit your settings. Under applications you use, you should see your app listed, click on edit settings. Finally click on the ‘x’ delete button to delete your app from the list. This will now remove your app from this users app list and won’t allow the app to access this account until the user authenticates again.
Graph API
This was confusing to me at first, specially since I keep rushing my project instead of taking my time to carefully look and read over the documents to truly understand what’s going on. Here is the Facebook Graph API overview if you would like to read that first but I’ll try to summarize it here. Graph API allows you to call various resources all of which will be returned to you as JSON objects. You’ll have to spend some time at the Graph API References page to understand the different objects available, parameters they have, and what the required extended permissions are to access them. I’ll try to explain connections in a sec.
Lets talk about the User object first. If you look at the reference page for user, you’ll see that to call the user object (the user who is authenticated by the app) you would call the graph api url with a ‘/me’ at the end. If the call is made successfully, you’ll receive a JSON object with all the possible properties defined in the reference page (if they are set). Here is a quick example.
user.php
If you run this code you get something like this:

This is my user object and properties.
Once you have the object, you can access it’s properties or do what you need with it. Now I’ll try to explain connections. If you look at the user reference page again and scroll down, you’ll see a connections section. These are the objects you can get that belong to this user. To access those objects you would call the url /me/object you want. For example, if you want to get this users albums, you would call /me/albums and this would return an array of album objects but remember to meet the required permission. For your app to access /me/albums, you need the user_photos extended permission (which we set in our config.php file).
So a connection is just a bridge between one object an another. Another example would be lets say we do as we did above and get all the users albums. Now we want to see all the photos inside each album. If you look at the album object, you’ll notice that under connections is photos which returns an array of photo objects. That is what we would call to get all the photos in that album for each album and you’ll see the code to do just this later on.
Get Status Posts
Create the following page and I’ll explain the code below.
status.php
// show statuses
$statuses = $facebook->api(‘/me/statuses’);
foreach($statuses[‘data’] as $status)
{
echo $status["message"], "<br />";
}
First include the config.php file we created earlier (you’ll include this in all the other pages as well). The api method is what we’ll be using every time we need to call a resource and it can take in three parameters: the resource, get/post (defaults to get), and params (if any). In this case we want to call the statuses connection in the user object which returns and array of status messages. After receiving the messages, we loop through each and echo them out. If you want to see all the properties of each status, just do a var_dump on each status. That’s it, now the next example.
Add New Status
Here we are doing something a bit different, we are publishing to Facebook and for more info you can look at the Facebook Graph API Overview in the publishing section. We want to create a new post so lets look at the reference guide for information on post and its properties. Scroll down to the publishing section and notice we need the publish_stream extended property and you can see all the different properties that this object supports. I’m personally a bit confused on what is considered a required property and what it not, is seems nothing is required but for this example we want to submit a post with a message.
status_add.php
// add a status message
$status = $facebook->api(‘/me/feed’, ‘POST’, array(‘message’ => ‘This post came from my app.’));
var_dump($status);
This is all the code required to make a post! It’s that easy. We call the api method, tell it we want to call the /me/feed connection, set method to post, and finally pass an array with the properties we want to set – in this case just the message property. If you wanted to set other properties, you would have set them in that array such as ‘link’ => ‘http://link.to.something’, etc. If the call is successfully, you’ll receive an id back which is the id for the new post you just created. Run this code and if all goes well, you should be able to go check our the users Facebook wall and see this latest post. I don’t think you can post the same message twice (unless I’m confusing Facebook with Twitter) so if you try it twice and it doesn’t work, that might be why.
View Photos From Albums
In this example, we want to get all the different albums for our user. Then we need to loop through each album and make a second call that will return all the photos for each album.
photos.php
// get albums
$albums = $facebook->api(‘/me/albums’);
foreach($albums[‘data’] as $album)
{
// get all photos for album
$photos = $facebook->api("/{$album['id']}/photos");
foreach($photos[‘data’] as $photo)
{
echo "<img src=’{$photo['source']}’ />", "<br />";
}
}
Here we call the albums connection on user to get all the album objects. We loop through each and for each album get the album id to then call the photo connection on the album object. If this sounds confusing just let me know and I’ll try to explain this further. When I think of connections I think of joins in a db. It’s like saying I want to join the albums table for the user in the user table therefor retrieving all rows in ablum for a specific user.
Add Photo
In the interest of time and complexity, I’m just going to add a photo without specifying an album. By doing this, Facebook will automatically create a new album to add this photo to. Just know that you can specify an album to add photos directly too if needed.
photos_add.php
$img = realpath("C:\\path\\to\\file.jpg");
// allow uploads
$facebook->setFileUploadSupport("http://" . $_SERVER[‘SERVER_NAME’]);
// add a status message
$photo = $facebook->api(‘/me/photos’, ‘POST’,
array(
‘source’ => ‘@’ . $img,
‘message’ => ‘This photo came from my app.’
)
);
var_dump($photo);
I choose this example for people that want to understand how to post a file through a service like this. First lets start at the top. We need to specify the path to the image we want to upload so change that path to where you file resides. Usually this would be the tmp path for a photo just uploaded to your server via $_FILE. Next we need to tell facebook to allow uploading of a file. I didn’t investigate that method much other then the comments said to specify the server so I use SERVER_NAME to signify this server. Next we make the call. We are calling /me/photos since that’s what is specified in the photos reference page under publishing. We are passing two parameters, message is the text that will show up with the image and source is the image we want to upload. Notice there is an @ before the path to the image. This is because we want to post the contents of the file and not pass a string to where the file is. Run this code and if all worked well, you’ll see this image and an new album in your users Facebook page.
Conclusion
If all went well, your users Facebook wall should look something like this:

My test users Facebook wall
I hope that with this, you have a good understanding on how Facebook’s Graph API works. Start trying to call other objects and play with different extended permissions. The Facebook team did a great job in providing us with the PHP SDK to make this so extremely easy so thumbs up to them. If you have any question/comments, just post them below.








#1 by Khan on June 8, 2011 - 4:33 pm
Quote
I am new to facebook API. This blog is one of the best blogs exist on the web for facebook API.
I need to know the code to show pictures(Thumbnails) of my fan page. I need to display these pictures on my site. What would be the code to do that? Is there any $facebook->api(‘/me/fanpage’ exist? I have used this method
$facebook_album = file_get_contents(“http://www.facebook.com/pages/Imrans-Fan-Page/165946010093388″); to get the data but it asks me for login.
Thanks in advance.
#2 by Joey on June 10, 2011 - 9:01 am
Quote
Hey Khan,
Take a look at the page_fan documentation using FQL. It might have what you are looking for: http://developers.facebook.com/docs/reference/fql/page_fan/
#3 by Gilson on June 9, 2011 - 12:57 pm
Quote
Hi,thank you for the tutorial, it really helped me. But i what im trying to do, is set a infinite session for my server that will be posting regularly and when i try to call $facebook->getSession(); or $facebook->setSession(); it says: “Fatal error: Call to undefined method Facebook::getSession() in…” i think that method have been removed, am i right? How can i do the same thing with the latest version?
Thank you!
#4 by Joey on June 10, 2011 - 9:15 am
Quote
Interesting, you are correct and the PHP SDK has changed. They have now created an abstract base class which can be extended to define how you want to store your access token. I’m glad they did it this way since we usually ended up having to extend it anyways. Just from looking over the code, it seems you can open the facebook class and modify the get/setPersistentData methods to store and load that token in a location of your choice such as a database. Both methods are automatically called from the base class during __construct or when authenticating. Try it out and let us know how it goes.
#5 by Jer on June 11, 2011 - 3:38 am
Quote
Joey, first, this is an amazing tutorial, is extremely well written and has solved a problem I have been trying to get past for about a week. My hats off to you. If you have a paypal link, throw it in here…this is information worth paying for
As for my question: I have two facebook accounts. Is it possible to post to both walls at the same time? I already authorized both accounts by running config.php a second time (when I was logged into my second account), but trial and error has not enabled me to figure out posting to both walls simultaneously.
Any suggestions? I’d be extremely grateful (and yes, such an answer is 100% paypal worthy to me…!)
– Jer
#6 by Joey on June 13, 2011 - 8:16 am
Quote
Jer, I’m glad you found this to be helpful. I put this up to share with others but if you insist on a paypal link I’ll send you an email
About your questions, I can think of a one thing to try. First, request offline access permission with both accounts when authenticating and then store your access tokens in a db after authenticating. I know with the older PHP SDK you would call get/setSession but with the new SDK it’s a bit different. When you want to post to both walls, load up the first token, instantiate the facebook class with that token, post to the wall, unset that facebook instance, then load up the second token, and post to that wall. Let me know if that works.
#7 by Safwan on June 20, 2011 - 12:19 am
Quote
Very nice article. Helped me understand a lot.
Am trying to make a application that will post to the users wall one wallpaper every week. What is the method to post to all the users of application? This article did make it clear how to post to the current user. Thanks
#8 by Joey on June 20, 2011 - 12:28 pm
Quote
Safwan, I’m glad you found this article informative. As far as your question, check the comment above that I left for Jer. It’s one way to do what you want although there may be more efficient ways out there.
#9 by Prince Merluza on June 27, 2011 - 2:41 am
Quote
Hello. thanks for this article, really informative and got me starting on app building. I need some help on building mine though, I’m making an app and this app will save some information from the user to the app so that other users of the app wuld be able to see that info. It’s ok since it’s an app exclusively for a group.
What’s the way to do this, should I save it to a sql database for retrieval and how do i make it so that it will change everytime the person changes his/her info. Basically how will i be able to access the info of someone not currently using the app.
#10 by Joey on June 27, 2011 - 3:17 pm
Quote
Hey, I’m not sure I completely understand your question. You want to track users who access your app so in that case you would want to store some information in a db. You can track as much information as they allow depending on the permissions they agreed to when first authenticating to your app. Now, if you requested offline permission, I assume you could every once in a while, using their stored token, call their fb profile to see if any information has changed to update your db? Just a thought…
#11 by Prince Merluza on July 5, 2011 - 9:13 pm
Quote
I see so it’s offline permissions. Seems like i don’t know enough php to understand how it works with tokens and stuff.
#12 by Apoo on June 29, 2011 - 4:20 pm
Quote
Awesome article, Thx. I am having a problem with the new PHP SDK. As someone mentioned Facebook removed getSession(). I was wondering how can I check if the user is the session? (assume the user is connected to my website previously. He accepted my permissions and everything.) In the previous SDK(RESt) I could get if getSession(), do … else show the login dialog. how can I do this with the new graph API? I have subclassed the API and this is my method, but doest work:
public function getSession(){
$old_rest_call_param = array(
‘method’ => ‘auth.getsession’,
‘auth_token’=> $this->FB->getAccessToken()
);
return $this->FB->api($old_rest_call_param);
}
#13 by Joey on July 12, 2011 - 1:37 pm
Quote
Apoo,
Unfortunately I haven’t played with the latest version of the SDK so I can’t comment. Maybe someone else has had a chance and can comment.
Joey
#14 by Stan Razvan on July 4, 2011 - 2:31 am
Quote
Hi joey, i followed you’re tutorial but it seems that when i try to get the request permission for my app the loads blank. Any ideas? pls help
#15 by Joey on July 12, 2011 - 1:27 pm
Quote
It could be that an error it happening but you may have errors turned off on your server so you can’t see them. Can you make sure error you have errors turned on and showing everything (http://php.net/manual/en/errorfunc.configuration.php)? Or check your logs as well for information there.
#16 by mike on July 6, 2011 - 6:15 pm
Quote
FYI: “req_perms” is now “scope” for the permissions request…
Also “canvas” and “fbconnect” might be useless in getLoginUrl() according to someone on IRC.
#17 by Joey on July 12, 2011 - 1:24 pm
Quote
I’m going to have to update this at some point as it seems lots has changed with the PHP SDK and/or the Graph API.
#18 by gregc on August 3, 2011 - 9:21 pm
Quote
I also need this app to be able to post when I am not logged in AND would like to post an identical message (I’m online now) each time. I get a duplicate message error now – is there a workaround for that?
#19 by Joey on August 4, 2011 - 8:35 am
Quote
As far as I know, you can’t post the same identical message twice, you would have to change something in it like add a period. To post when you are not logged in, you need to first request offline permission when you authenticate. After you authenticate, you need to store that session token into a file or database and each time you make a request to the Api you’ll have to send that token with it. Hope this helps.
#20 by gregc on August 4, 2011 - 9:09 am
Quote
The duplicate message I get is this:
“(#100) The status you are trying to publish is a duplicate of, or too similar to, one that we recently posted to Twitter”
Is that from your code? I ask because it says “Twitter”?
Small changes in the text to post do not work, so I’ll have to rotate between different phrases….
I have already done the “manage_pages” permission by adding that to the config file and will add offline_access now.
#21 by Joey on August 5, 2011 - 9:28 am
Quote
You are correct, that was Twitter that enforces that restriction and not Facebook. I had that same problem when I was doing some tests and passing the same message but then I started adding some random bits to make it work until my tests were done.
#22 by joy on August 18, 2011 - 7:41 am
Quote
Hi There,
Can you please tell me why i am getting this error
Fatal error: Uncaught OAuthException: (#200) The user hasn’t authorized the application to perform this action thrown in /library/facebook/base_facebook.php on line 1033
: |
#23 by Joey on August 19, 2011 - 9:10 am
Quote
Unfortunately I’m not sure. At some point I was getting some authentication issues and clearing my cache and forcing a session reset in the facebook account by deleting the user logging into the app fixed my problem. Try that.
#24 by Johan on August 19, 2011 - 10:50 am
Quote
Hi Joey, awesome article !!
My goal is to create an event AND to invite friends… creating an event is not that difficult, but I don’t seem to find information on how one can invite friends ?
#25 by Joey on August 24, 2011 - 12:13 pm
Quote
hey Johan,
It seems the only way atm to do this is to use the old REST api. You can still call these older methods using the PHP Facebook SDK. Here is an example: http://stackoverflow.com/questions/3346634/how-to-invite-a-users-friends-to-an-event-facebook-graph-api
Hope this helps.
#26 by Shaz on August 21, 2011 - 12:32 am
Quote
Hi,
I am not able to post my message when i am not logged in. Everything is configured propert, i got offline_access also, but this codes does not work when i am not logged into facebook. It got the facebook login page when i execute this page. If i am logged in then it is working fine. Can you please help me to resolve this.
‘app id’,
‘secret’ => ‘secret key’
));
$session = $facebook->getSession();
if(!empty($session))
{
# Active session, let’s try getting the user id (getUser()) and user info (api->(‘/me’))
try
{
$uid = $facebook->getUser();
$user = $facebook->api(‘/me/accounts’);
}
catch (Exception $e)
{
echo “error “. $e;
}
if(!empty($user))
{
# User info ok? Let’s print it (Here we will be adding the login and registering routines)
print_r($user);
foreach($user['data'] as $account)
{
if($account['id'] == 160603497351263)
{
$access_token = $account['access_token'];
echo “Page Access Token: $access_token”;
}
}
$uid = $facebook->getUser();
# let’s check if the user has granted access to posting in the wall
$api_call = array
(
‘method’ => ‘users.hasAppPermission’,
‘uid’ => $uid,
‘ext_perm’ => ‘publish_stream’
);
$can_post = $facebook->api($api_call);
if($can_post)
{
$attachment = array(‘message’ => ”,
‘access_token’ => $access_token,
‘name’ => ‘test message.’,
‘link’ => ‘http://www.google.com',
‘picture’ => ”);
# post it!
$facebook->api(‘/160603412341234/feed’, ‘post’, $attachment);
echo ‘Posted!’;
}
else
{
die(‘Permissions required!’);
}
}
else
{
# For testing purposes, if there was an error, let’s kill the script
die(“There was an error.”);
}
}
else
{
# There’s no active session, let’s generate one
$login_url = $facebook->getLoginUrl(array(‘req_perms’ => ‘email,user_birthday,status_update,publish_stream,user_photos,user_videos,manage_pages,offline_access’));
header(“Location: “.$login_url);
}
?>
#27 by Joey on August 24, 2011 - 12:29 pm
Quote
Shaz,
If I remember correctly on my project, I wanted it to always be using the same account no matter who was trying to access it. For this to work I had to request offline_access like you mentioned in your post. Then, I had to modify the getSession and setSession methods in the facebook class to store that session in a db table and get the session from the table. After doing all this, I authenticated once with the correct account (the session was stored and then reused every time) and I was able to get it working without having to be logged in to Facebook through that browser.
#28 by Jubpas on August 27, 2011 - 9:13 am
Quote
help me.
how post content to note or fanpage Using facebook APi
thank you.
#29 by Joey on September 2, 2011 - 10:42 am
Quote
Jubpas, unfortunately I haven’t tried these. Maybe someone here has some insight.
#30 by deepesh on September 20, 2011 - 4:10 am
Quote
How to debug anything while developing facebook apps. MY screen is always empty white …i Cant see anything. But i can see echo “test” if i write on top of my file.
#31 by Joey on September 20, 2011 - 8:59 am
Quote
There are a couple things to try. You may have your error reporting turned off or errors not displaying. Here is more info on error_reporting and display_errors: http://docs.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting. The other thing you can do is check your php error log file since that should have any errors and warnings encountered. Do a phpinfo() to see where that log is stored and open it to look.
#32 by Muhammad Sameer on September 21, 2011 - 6:48 am
Quote
OMG… Really An Awesome Tutorial… I Tried Too Many Tutorials… But No One Was Perfect… But You Are Awesome
Thanks a Lot…
#33 by Joey on September 21, 2011 - 9:12 am
Quote
Glad you found it helpful.
#34 by Bobby on September 26, 2011 - 9:20 pm
Quote
Any chance you know how to upload a profile cover to the new timeline?
#35 by Joey on September 28, 2011 - 8:18 am
Quote
Hey Bobby, I do not but maybe someone here might.
#36 by Puja on September 28, 2011 - 2:05 am
Quote
I am developing one facebook application.I have album details .In which I got cover photo Id. I want to show the cover photo how can show the cover photo from this cover photo Id.
Also I have photo array from the particular photo album . I used source and picture to show this photos from album .But after selecting photos I saved photo Id and in next page I want again source of photo from this photo Id.
Can you please help me for this.
#37 by Joey on September 28, 2011 - 9:03 am
Quote
So if you have the link to show the album details, you can add picture at the end of the url to use the picture connection which:
If you have the cover photo id (picture id of the albums cover photo) only, you should be able to call the photo object using that id and then get the url in the picture property. Hope this helps.
#38 by Puja on September 30, 2011 - 12:50 am
Quote
Hello ,
I am using below code
require_once ‘config.php’;
// get albums
$albums = $facebook->api(‘/me/albums’);
foreach($albums[‘data’] as $album)
{
$intAlbumId = $album['id'];
$coverphotoid = $album['cover_photo'];
$fql = “select src_big from photo where pid =’$coverphotoid’”;
$ret_obj = $facebook->api(array(
‘method’ => ‘fql.query’,
‘query’ => $fql,
));
print_r($ret_obj);
}
But print_r($ret_obj); is giving me blank array.
#39 by Joey on October 5, 2011 - 8:42 am
Quote
Puja, try going to the Graph API explorer tool to see if you get results. The link is: http://developers.facebook.com/tools/explorer/?method=GET&path=me%2Falbums
Click on “get access token” and select user_photos as that is the permission you need to view albums. Then click on “submit” and see if anything comes back. If you are getting data back from facebook, I would look at your php error logs to see if there are any errors when running your code.
#40 by Avantika on October 4, 2011 - 1:23 am
Quote
Hi,
i am having an application. i tried to access the albums in my account with the application but its returning an empty array.
i tried the url directly in the browser..
the url used is
https://graph.facebook.com/me/albums?method=get
&access_token=…
looking forward for reply
#41 by Joey on October 5, 2011 - 8:42 am
Quote
Avantika, try going to the Graph API explorer tool to see if you get results. The link is: http://developers.facebook.com/tools/explorer/?method=GET&path=me%2Falbums
Click on “get access token” and select user_photos as that is the permission you need to view albums. Then click on “submit” and see if anything comes back. If you are getting data back from facebook, I would look at your php error logs to see if there are any errors when running your code.
Pingback: Facebook Graph API App Easy w/ PHP SDK | prosoxi.com
#42 by mik on October 5, 2011 - 6:43 pm
Quote
Joey question Im trying to implement facebook in my site where users can grab photos from there albums and upload them to my site. Im having a hard time doing all this. Any chance you can help me. Iv been reading the developers site on facebook but cant seem to understand the part about pulling the data. I can get them to allow pop up but after how do i pull the info. Im more of a php guy.
#43 by Joey on October 7, 2011 - 10:55 am
Quote
Hey mik, I’m not sure I understand exactly what you are asking. If you want users to browse their photos, select a photo, and upload it to your server then what you can do is pass the photo url to your server and have it do a file_get_contents to download the file. Check my response to Puja’s comment, sounds like the same thing you are trying to do.
#44 by Puja on October 7, 2011 - 1:13 am
Quote
Thanks …I got the cover photo Using FQL queries.
Now I want to copy facebook album photos to my local machine.I have source of the photos. Can I do this?
Thanks in advance.
#45 by Joey on October 7, 2011 - 10:36 am
Quote
If you have the url to the photo, you can do something like
$data = file_get_contents($photo_url); // get the data from the url
file_put_contents(‘image.jpg’, $data); // store the data as an image file on your system
#46 by Puja on October 11, 2011 - 4:07 am
Quote
Thanks Joey .I will try this.
#47 by Sebastian on October 19, 2011 - 10:21 pm
Quote
Hola, tengo un inconveniente con tu ejemplo ojala me puedas ayudar:
El código para publicar en el muro es el siguiente:
function publicarMuro(){
// add a status message
//alert(msg);
api(‘/me/photos’, ‘POST’,
array(
‘source’ => $img,
‘message’ => ‘Esta foto viene desde mi app.’
)
);
var_dump($photo);
?>
Pero no funciona, lo raro es que el postear solo texto si funciona, he intentado poniendole:
// allow uploads
//$facebook->setFileUploadSupport(“http://” . $_SERVER['SERVER_NAME']);
pero da igual.
Gracias por tu ayuda
#48 by Joey on October 26, 2011 - 5:54 pm
Quote
Sebastian,
Aqui tengo un ejemplo: https://github.com/joeyrivera/Facebook-Graph-API-Examples/blob/master/photos_add.php
Si tienes una nueva version del api pasa true para setFileUploadSupport. Tambien no te olvides del ‘@’ antes del $img variable.
#49 by Ben on October 20, 2011 - 12:07 pm
Quote
Alright I’ve looked all over for how to post an update as a page to the page. I’ve seen how you can update a App Profile, but not the page itself. How would I go about doing that?
Also, I keep getting this error when running this (using codeigniter, hence how I’m loading the library):
$config = array(‘appId’=>’NONEYA’,'secret’=>”NONEYA”,’cookie’=>true);
$this->load->library(‘facebook/facebook’,$config);
$me = $this->facebook->api(‘/me’);
print_r($me);
“Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.”
Yes, I’m logged in, I’m an admin of the app and page.
#50 by spreq on October 21, 2011 - 6:43 pm
Quote
best sample php-facebook so far
now i see real sample and want to do it
other site just show technical howto
keep it up
#51 by Daniel Siqueira on October 28, 2011 - 6:21 pm
Quote
Hey there Joey, I’m wondering if you’ve found any way to retrieve events posted by the application?
The events created by the app display as being created by user and app, but when I query creator/owner all I get is the userID. I tried with FB.api(‘/168592903230706/events’) but I get nothing. Am I looking in the wrong place?
Also tried with FQL, it works with userID but not with applicationID
‘SELECT eid, name, start_time, creator FROM event WHERE eid IN (SELECT eid FROM event_member WHERE uid = me()/*or APPID*/)’
From the graph explorer I can see albums, images, status from my App Profile Page, but no events even thou I’ve created 10+ (http://developers.facebook.com/tools/explorer/?method=GET&path=168592903230706)
(You probably can’t see my app because it’s in sandbox mode, but you can see if there are any event objcts in an app)
#52 by Emmanuel on February 22, 2012 - 11:16 am
Quote
…You just saved me from nights of trying to figure this out!! Awesome. This is extremely useful!!
#53 by Nilesh Khaire on February 29, 2012 - 12:27 am
Quote
Hey joeyrivera , thank you for post … its really working in my case… thank u .. very much ..
#54 by tas on February 29, 2012 - 8:04 am
Quote
hi.. i am searching for a sample code on curl php that can display pictures found in a public album of a friend.
can anyone help please?
thanks
#55 by Gabriel on March 6, 2012 - 9:34 am
Quote
Hey, I need to solve a problem to my client, that i need to do a crontab, he post an article and each 30 minutes have a file that verify if my database in notices table have a post with the column facebook = ‘S’, if have public this article on his facebook, but i need to find a way that the user dont need to log in everytime he post something, so i enable the offline_acess and try to get the inifite session key, but when i use the function “$facebook->api_client->auth_getSession($auth_token);” the PHP return a error = “Fatal error: Call to a member function auth_getSession()”, and i dont know why it appear, because i found a lot of tutorials asking to use this function and everytime return this error.
Could you save me about it?
I’m using the last version of SDK-PHP
#56 by Joey on March 13, 2012 - 8:49 am
Quote
The SDK has changed since I first wrote this post, you’ll have to see what the newer methods are to store and get the token in your SDK – it should still work in a similar fashion though. Authenticate, store the token, and reuse it every call.
#57 by Matt on March 18, 2012 - 9:21 pm
Quote
Hey Joey, I was wondering if there was any quick and easy way to grab the number of likes a fan page has and use them throughout the site using an echo function?
#58 by swati on April 3, 2012 - 7:48 am
Quote
I want to delete the photo from facebook using Graph API.But i am getting error as—Fatal error: Uncaught OAuthException: (#3) Application does not have the capability to make this API call. thrown in C:\wamp\www\FacebookApp\facebook-php-sdk-6c82b3f\ src\base_facebook.php on line 1106
This has been reported as a bug on https://developers.facebook.com/bugs/125524667559003?browse=search_4f14be86ba58f3666689319
Can I Delete a Album using Graph API in PHP?
#59 by fgh on April 12, 2012 - 7:12 am
Quote
Type your comment here
#60 by naveed khan on April 22, 2012 - 9:29 am
Quote
Thanks alot,
millions of thanks…………..