PHP facebook application - php

I'm starting out to learn how to build a Facebook application, but I've run in to some problems. I can't seem to find a simple tutorial on how to authenticate and ask for permissions from the user. The app itself is in php and consists of a simple quiz. I'm using the php sdk 3.0, but in the example file you have to login before you are asked to allow permissions. I just want the user to be redirected to the permissions prompt when the app is loaded.
So, to be more specific, I need som sort of basic script that authenticates and ask the user for permissions. I'm using the php sdk, but if there is a better way please feel free to suggest this.

Here is a list of all permissions

try getting this url on some link or redirect the user to it:
https://www.facebook.com/dialog/oauth/?scope=email,user_about_me,publish_stream&client_id=xxxxxxxxxxx&redirect_uri=REDIRECT_URL&response_type=token
where the scope are the permission you are asking for, the clien_id sould be your app id (not the secret) and the redirect_uri (its URI not URL) is the site that the user will see after installing (I recomend to get here te full facebook app URL, i.e https://facebook.com/fanpage/?sk=app_xxxxxxxxxxxxxxxx)
and thats the simpliest way, if you feel capable of more stuff you can use this PHP code:
$fbconfig['appid'] = "xxxyyy";
$fbconfig['secret'] = "xxxxxxxxxxxxxxx";
$fbconfig['baseurl'] = "https://www.facebook.com/pages/FANPAGE/xxxxxxxxxxxxxxxx?sk=xxxyyy";
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,user_about_me,publish_stream',
'redirect_uri' => $fbconfig['baseurl'],
'display' => 'popup'
)
);
and redirect user to the $loginUrl.
hope it helps, cheers.

Related

Get user access token with permission via php with FB sdk 3.2

I'm admin of a page and I have my app with ID and Secret.
When I go to Graph api Explorer I can easily take my User Access Token with permission "manage-pages".
I need to take the same via php, using the FB sdk 3.2.
I managed to run only this:
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'My app ID',
'secret' => 'My app secret',
));
//This token is: "My app ID|My app secret"
print_r($facebook->getAccessToken());
How Can I get the user access token with permission via php with fb sdk 3.2?
Can somebody help me? Thank you!
You need to generate a Login URL and redirect Users to that one, it´s explained in the docs for 3.x.x: https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/
With manage_pages, it would be like this:
$params = array(
'scope' => 'manage_pages',
'redirect_uri' => 'https://www.myapp.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
I highly suggest upgrading to a server with PHP 5.4+ though.
You shouldn't waste your time with using an outdated version of the PHP SDK, or is there a reason why you chose the old version?
See
https://developers.facebook.com/docs/php/gettingstarted/4.0.0
on how to implement Facebook Login.

when attempting to connect to facebook with PHP script endless redirects

first facebook asks me to click okay to authorize the app then chrome gives me this error. and the I have this endless redirect problem. I have read many cases about this but none clear and simple enough to get me over this obstacle.
The webpage at __________&redirect_uri=http%3A%2F%2Fmizu.net46.net%2Ffacebookpost6.php&state=_________________&sdk=php-sdk-3.2.3&req_perms=user_status%2Cpublish_stream%2Cuser_photos%2Coffline_access%2Cmanage_pages#=">https://www.facebook.com/dialog/oauth?client_id=__________&redirect_uri=http%3A%2F%2Fmizu.net46.net%2Ffacebookpost6.php&state=_________________&sdk=php-sdk-3.2.3&req_perms=user_status%2Cpublish_stream%2Cuser_photos%2Coffline_access%2Cmanage_pages#= has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
what I know.
facebook.php is working
the secret and appid are being passed correctly by the script.
I must have configured my app settings on the facebook development site almost correctly
how do I know this?
because when I change those variables in my script or adjust the settings
on facebook development server, I dont even get this far, I dont get asked to click ok or see my facebook profile in chrome.
what I dont know.
How to fix this redirect problem. or complete the authentication process. echo $user always returns 0.
my code looks like this:
require 'src/facebook.php';
$app_id = "xxxxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' =>
'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
thanks for any help
changing the setting to no when asked" is this a desktop app? " in the advanced section of app configuration on developers.facebook.com seem to resolve this issue.

How to redirect to a page after loggin in with facebook?

I am using Facebook php-sdk for allowing the users to login to my website through facebook.
The login function works properly.But After I login I want the user to get redirected to my website.Does anyone knows where to change the redirect url??I would had liked to post the code of for it here but its not just one file plus the code is way way to long..I have downloaded the example form web and used.If i have to use "header()" for relocating I don't know where to write..I want to know where can i write the code for redirecting
I am using this example
https://github.com/facebook/facebook-php-sdk
In the above link navigate to "test/test.php" there too many functions here which are being called and all of them has REDIRECT_URI option..I am confused to where do i need to change the the REDIRECT_URI..And is tests.php is the place to change the URL??If Not then where i have to change??
I will take this tutorial as an example since there is no code in your question.
http://cacovsky.wordpress.com/2011/05/17/login-with-facebook-php-sdk-a-tutorial/
If you visit this page or another tutorial, which I assume you have done before, you will be able to search the site for the term "header".
header('Location: http://localhost/fb/index.php');
This line provides the information to which site in your application the Facebook Login will redirect to.
Dunno how often this was asked now ...
header("Location: index.php");
This is how you can use.
If you use FB login button use below code
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => true
));
<fb:login-button scope="email,friends_likes" onlogin='window.location="{here your full URL after login}";'>Connect with Facebook</fb:login-button>
If you use signin URL using the FB API, you can use as
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => true
));
$params = array(
'scope' => 'email, friends_likes',
'redirect_uri' => '{here your full URL after login }'
);
$loginUrl = $facebook->getLoginUrl($params);

Facebook login without a form

How can I log into Facebook without a login form?
I've created an application which posts wall posts to one of my pages automatically (with a cron job).
But it needs login. I've done everything with the PHP SDK 3.1 and it works if I am logged into Facebook by my browser. But it does not work on my hosting and this is important for me because it is "scheduled auto poster".
Is it possible with the PHP SDK or JavaScript SDK or anything?
PS: Logging in with cURL does not work (code: http://www.daniweb.com/web-development/php/code/290893).
I use PHP SDK 3.1.
Here is my code:
require 'sdk/facebook.php';
$facebook = new Facebook(array(
'appId' => $bilgi_appID,
'secret' => $bilgi_appSecret,
'fileUpload' => true,
'cookie' => true // enable optional cookie support
));
$facebook->setFileUploadSupport(true);
$args = array(
'message' => $bilgi_fotoMesaji,
"access_token" => $access_token,
"image" => $file
);
$data = $facebook->api('/'.$bilgi_sayfaninAlbumIDsi.'/photos', 'post', $args);
if ($data)
print_r("basariyla yuklendi...");
This works when I am logged in Facebook.
But how can I post a photo or post while I am logged out? It redirects login page.
Scheduled auto-posting is available when requesting the publish_stream permission, even when the user is actively using your application. From Permissions Reference:
publish_stream
Enables your app to post content, comments, and likes to a user's stream and to the streams of the user's friends.
With this permission, you can publish content to a user's feed at any
time. However, please note that Facebook recommends a user-initiated
sharing model. Please read the Platform
Policies to ensure you
understand how to properly use this permission.

Why can I not establish access to my Facebook app?

I am following the instructions on this page to create a PHP script which will (hopefully) allow me to post to Facebook from my web site.
All I really want to establish is the ability to enter text and a link into a form on my web site and have it be posted to a Facebook wall.
My understanding is that I need to validate a Facebook app in order to do this, which is what brought me to the above web page.
I have followed their instructions dutifully, and by now I have been through it many times, checking to make sure I have followed their steps accurately.
However, when it gets to the part where I run fb_access.php from my web site, I get absolutely no response from Facebook.
I notice that the screen shot examples on the page I am using are not what I am seeing in the Facebook interface. The tutorial is from December 2010, which I would hope is recent enough, but perhaps things have changed...?
How do I get Facebook to respond to my PHP script?
<?
require_once 'facebook.php';
$app_id = "<my App ID";
$app_secret = "<my App Secret>";
$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;
}
?>
Try using the Facebook PHP SDK demo script here:
https://github.com/facebook/php-sdk/blob/master/examples/example.php
Once you have this script running - you can then begin to request calls, such as post to wall. If the call is successful, FB will prompt you with a Request for Permission.

Categories