Can i run my facebook apps from local host - php

Problem: Can i run my facebook apps from local host.
Description: I want to get facebook wall posts on my website page. For testing, i created a facebook app and put site url as "http:localhost:8088/fb/" nd app domain as "localhost". Now i want to get user id so that i can access facebook wall posts.But all am getting is user id equals to zero.I have checked it so many times. but everything seems to be correct. So i want to know that can we use local host as site url?? Can i get user id by running my code from local host. Below is my code.
<?php
include_once "facebook.php";
$facebook = new Facebook(array(
'appId' => '<app id>',
'secret' => '<app secret>',
'cookie' => true,
));
$uid = $facebook->getUser();
echo $uid;
$me = null;
if ($uid) {
try {
$me = $facebook->api('/me');
$feed = $facebook->api('/me/feed/');
} catch (FacebookApiException $e) {
error_log($e);
}
}
$params = array(
'scope' => 'read_stream',
'redirect_uri' => 'http://localhost:8088/fb/feeds.php'
);
if ($uid) {
$logoutUrl = $facebook->getLogoutUrl();
}
else {
$loginUrl = $facebook->getLoginUrl($params);
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
</head>
<body>
<?php if($uid)
{
?>
Logout
<?php
}
else
{
?>
Login
<?php
}
?>
<?php if ($me): ?>
<?php
echo "<pre>";
print_r($feed);
echo "</pre>";
?>
<?php endif ?>
</body>
</html>
Please tell me what is the problem here. Is there any settings tht i need to perform. I will be very thankful.

Yes you can run your app from localhost environment. You have done everything correct except that you don't need to add localhost in the app domain and your site url should be
http:// localhost:8088 /fb/
instead of "http:localhost:8088/fb/"

A better way to test out the Facebook api on localhost is to first create a virtual host for your web application like yourapp.local and then set the URLs in your Facebook app information accordingly. You can create separate Facebook apps for local and remote sites.

You can for the most part.
Facebook apps which are just iframes don't need to connect to your host - only you/your browsers does.
Two things to watch out for:
Make sure you have SSL (https://) on your localhost. If not, disable SSL in your test Facebook account settings.
One situation where Facebook will need to access your host include sharing URLs or images that are hosted in your app. Use external placeholders when testing your local app.

I'm sure you can't do that if you have facebook aggregations ( Objects+actions) in your app. Because they need to be accessed directly by facebook

Related

Facebook getUser() not working

I am trying to connect to the php facebook sdk and pull user data.
<?php
require_once('facebook-php-sdk/src/facebook.php');
// Create our Application instance (replace this with your appId and secret).
$facebook= new Facebook(array(
'appId' => '2453463636',
'secret' => '365653656565656',
'allowSignedRequest' => false,
));
$user = $facebook->getUser();
if ($user) {
try {
// Get the user profile data you have permission to view
// $user_profile = $facebook->api('/me');
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
echo $user_profile['email'];
} catch (FacebookApiException $e) {
$user = null;
}
} else {
die('<script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}
?>
This works well on localhost while testing. I have a app created on facebook for this with a website platform added with the locahost as the site url in the dashboard.
To now test on the server I created another app on facebook with the domain and site url set as my staging site on my server.
I simply changed the appId and secret to that of the Facebook App created with the remote staging site url address, but this doesn't work.
Help Please! Is there something key that I missed in the code or dashboard that is not causing my page to get the user?
My page is a view in a codeigniter application, and require_once('facebook-php-sdk/src/facebook.php');, this directory resides in the root of my codeigniter application
EDIT:
I replace the die clause with die("failed");, and the text "failed" shows up when I try to load the page. Not sure what the problem is? This worked fine on localhost, why am I noy connecting now?
When I print_r($user); the page returns 0. Why am I not connecting.

Use Facebook Api Locally

I am attempting to use the facebook api with my php web application. I have downlaoded the api and in my app settings is added a website platform with the Site URL as my localhost address http://localhost:8080/app/ for testing.
However it does't add to the seetings with this, when I try to save this in the App Dashboard I get an error message, but when I set it as http://localhost/app/, it saves it.
I am doing this to sort out the error I get when I try to use the api.
When I go to http://localhost:8080/app/, the page redirects to:
https://www.facebook.com/dialog/oauth?client_id=648801771829346&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fkfc%2Ffacebook_connect&state=1e655991943d79a58aac8d22fbd2c61f&sdk=php-sdk-3.2.3
with the error:
Given URL is not permitted by the application configuration.: One or
more of the given URLs is not allowed by the App's settings. It must
match the Website URL or Canvas URL, or the domain must be a subdomain
of one of the App's domains.
My code:
<?php
require_once('facebook-php-sdk/src/facebook.php');
// Create our Application instance (replace this with your appId and secret).
$facebook= new Facebook(array(
'appId' => '648801771829346',
'secret' => 'd4a528ad0614314a9d3aaf59c978fbfa',
));
$user = $facebook->getUser();
if ($user) {
try {
// Get the user profile data you have permission to view
$user_profile = $facebook->api('/me');
echo "<pre>";
print_r($user_profile);
echo "</pre>";
} catch (FacebookApiException $e) {
$user = null;
}
} else {
die('<script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}
How do I fix this do that I can test locally on http://localhost:8080/app/, this is my xampp server.
DashBoard:
First, Add a platform -> Website
Settings -> Basics -> App on Facebook -> Canvas URL -> http://localhost/app?
Settings -> Basics -> Website -> Site URL as http://localhost/app/
There is a toggle button in the Settings->Advanced part of your application which says "Native or desktop app? Enable if your app is a native or desktop app". When you turn on that switch, I think you will be able to send back to localhost the information from Facebook.

APP authentication on facebook with PHP SDK

I have a problem with authenticating my Facebook APP with a Link generated through the Facebook PHP SKD:
when I click the link, an error appears on facebook (An error occurred. Please try again later.) and I won't get redirected to the "app login page".
https://www.facebook.com/dialog/oauth?redirect_uri=http%3A%2F%2Fxxx.test.net%2Fstart.php&state=8d0ea10xxxxxxx290be6c654f02xxxa&scope=user_likes
This is the code of the index.php:
<?php
require_once("./fbsdk/facebook.php");
//test.net
$config = array();
$config[‘appId’] = '37353621936xxxx';
$config[‘secret’] = 'fd39e0f0861f2bdae4fc79274749xxxx';
$config[‘fileUpload’] = false; // optional
$facebook = new Facebook($config);
?>
<!DOCTYPE html>
<html>
<head>
<title>
FB-DEV-TEST
</title>
</head>
<body>
<?php
$params = array(
'scope' => 'user_likes',
'redirect_uri' => 'http://xxx.test.net/start.php'
);
$loginUrl = $facebook->getLoginUrl($params);
echo "Login";
?>
</body>
</html>
I've already read this post, but I still can't figure out the problem... are there any changes I have to do at the Facebook APP center, before I can login? Thanx for your help & sry, for my bad english! :)
Check if your redirect_url domain matches the "Site URL:" in App settings on developers.facebook.com.
A few checklists that I can think of:
Make sure your Facebook app is not in sandbox mode (unless you are already signed in to your Facebook account before clicking on the login link)
At https://developers.facebook.com/apps/:
"Website with Facebook Login" must be checked, but for "Site URL", you can enter any landing URL.
Also at https://developers.facebook.com/apps/:
Make sure that your site domain is entered under App Domains.
Alternatively, you may want to check out Opauth, an authentication framework for PHP that handles these for you.

Facebook PHP SDK: how to display login dialog automatically?

My code cannot display the permission dialog when a user connect to my Facebook application. Instead of the login page, I got a 404 error.
My code:
require 'src/facebook.php';
require './config.php';
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true,
));
$userData = null;
$user = $facebook->getUser();
if ($user) {
try {
$userData = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream',
));
}
<div id="loginWrapper">
<?php if ($user): ?>
Logout
<?php else: ?>
Login with Facebook
<?php endif ?>
</div>
I have also tried to put directly an personalized URL (like bellow) and I got the same 404 error.
$loginUrl = "http://www.facebook.com/dialog/oauth?client_id=".$fb_app_id."&redirect_uri=".$fb_app_url."&scope=publish_stream";
UPDATE 1:
Otherwise when I try to Login directly through the server (from http://mydomain/application/index.php), I got a redirection to Facebook website to the URL below and got this error message:
https://www.facebook.com/dialog/oauth?client_id=xxxxxxxxxxxxxx&redirect_uri=http://apps.facebook.com/myapplication&state=f8cd945d8a9aefcd0b439fecd8515a68&canvas=1&fbconnect=0&scope=publish_stream
UPDATE 2:
Here my application configuration:
And the message I got when I try to access directly from the app link: apps.facebook.com/myapplication/ without be logged
I would appreciate any help.
The status_update permission you are using in the scope for the loginUrl is not a valid permission. Take a look at the docs.
That could be your problem.
Also remember, that if a user has already authorized your app, they will not see the permission dialog again on subsequent logins. They will only be taken through the facebook login procedure.
EDIT:
Take a look at the image I've attached, facebook will on redirect to the url you specify in the site url field in app settings:
Regarding the update of your post: Go to the Developer App and select the App you're getting this error for. Click edit settings and enter the domain your App is hosted on/you're redirecting to into the "App Domain" field. Now just save and it should work.
Edit (solution from comment above): If it just doesn't work when you're not logged in, may it be that your App runs in Sandbox Mode? You can check if it's enabled in the "Advanced Settings" of your App.

Facebook Application with PHP running losing session

Iam trying to build an Facebook Application based on PHP. The Application is running under php on my own Webhost inside an Canvas as iFrame.
I have included the newest Client Library for PHP from Facebook: facebook-php-sdk-94fcb13
To Authorize the user inside my application iam trying to use Facebook Connect, like the example shipped with the Client. Everything works fine the 1st Login, but when i hit the F5 Key to reload the page, the session is lost and i have to login again. When i call my application outside of the Facebook Canvas everything is fine.
Iam not sure, but i think my Browser (Chrome/FireFox - Ubuntu) is not allowing to store an cookie inside an iFrame.
Does someone knows an solution for this Problem? Here are some Parts of the Sourcecode:
$facebook = new Facebook(array(
'appId' => 'x',
'secret' => 'x',
'cookie' => 'true',
));
$session = $facebook->getSession();
$facebook->setSession($session);
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
Solved :-)
I have missed to setup the basedomain in configuration.
On the "Conntect" Tab you have to set the Basedomain to your host.
Example: Application is running under http://myapp.com/facebook/html than set the basedomain
to myapp.com
Now the cookie is stored correctly.
php passes the session id via a cookie value , if its not able to send it , sends via get parameter like *.php?phpsessid......
It will get lost if u use header() redirection , solution append .SID in url,
like
header("blah blah".SID);
Hope it helps you..

Categories