I am building a Facebook app and need to get the userid directly from Facebook. I intend to use Facebook PHP SDK to get the userid but it always crash my server.
Here's my code:
<?php
require 'facebook-php-sdk-master/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
// Get User ID
$user = $facebook->getUser();
echo $user;
?>
I always get the server error message:
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
I'm using the Facebook PHP SDK from https://github.com/facebook/facebook-php-sdk. Can someone help me with regards to this problem?
I found out the error. It turns out that there's an error in the base_facebook.php file. You need to modify the getCurrentUrl() function to redirect to the app URL that you used in the App Settings. Hope that save someone from debugging for hours!
Related
I know this has been ask3d before but I have tried several of the posts but still cannot get it to work. I am being forced to use strict mode for url redirects and no matter what I put for the domain nothing works.
<?php
if(!session_id()){
session_start();
}
// Include the autoloader provided in the SDK
require_once __DIR__ . '/src/Facebook/autoload.php';
// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
/*
* Configuration and setup Facebook SDK
*/
$appId = '********'; //Facebook App ID
$appSecret = '***************'; //Facebook App Secret
$redirectURL = 'https://www.themathouse.com/'; //Callback URL
$fbPermissions = array('email'); //Optional permissions
$fb = new Facebook(array(
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.2',
));
// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();
// Try to get access token
try {
if(isset($_SESSION['facebook_access_token'])){
$accessToken = $_SESSION['facebook_access_token'];
}else{
$accessToken = $helper->getAccessToken();
}
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
?>
On the facebook app I have themathouse.com as the app domain and https://www.themathouse.com as the Valid OAuth redirect URIs.
When I try logging in with facebook I get the following error:
Graph returned an error: Can't Load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings.
Any help would be greatly appreciated.
Make sure that your Redirect Url matches what is set in your App Settings, under Facebook Login -> Settings -> Valid OAuth redirect URIs
In this case, it seems to be https://www.themathouse.com/
EDIT: Also, as it seems that you are using the PHP SDK, make sure that you are using the currently latest version, 5.6.2, as this one fixed an issue present on 5.6.1 and older that may affect you.
THIS worked for me!! (after fiddling with a bunch of stuff suggested on various forums, to no avail)
I updated the 'FacebookRedirectLoginHelper.php' file:
https://github.com/facebook/php-graph-sdk/blob/5.x/src/Facebook/Helpers/FacebookRedirectLoginHelper.php and voila! Pesky login error fixed :)
(Oh, I also updated code for other recently changed files [within last few months or so] in the Facebook PHP/SDK, so you should also do this as well: https://github.com/facebook/php-graph-sdk). Good luck!
Update with the latest SDK, it will solve your problem.
I have just started with the Facebook application development. I downloaded the PHP SDK from Facebook couple of weeks ago.
Following is the first piece of code which i wrote.
<?php
require 'src/facebook.php';
$app_id = 'My APP ID';
$application_secret = 'My APP Secret';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $application_secret,
'cookie' => true, // enable optional cookie support
));
$uid = $facebook->getUser();
echo $uid;
?>
But it is always giving me 0 as a result even when i am already login into my facebook account in other tab.
My Canvas URL is pointing to my localhost as i have hosted this app on my local machine only.
I have read almost every post related to this issue but i was not able to solve this issue somehow.. It would be really helpful if anybody could help me in resolving this issue.
Cheers.
Ajay
The code will always return 0 unless you have authorised the app - even if you are logged into facebook elsewhere. Login to your app by going to the URL outputted by $facebook->getLoginUrl();
Then, you will find that $facebook->getUser() will return the correct User ID.
I'm trying to send a message from an app to a user when a specific event happens. Right now I have this code
$param = array(
'message' => 'XYZ shared a file with you',
'data' => 'additiona_string_data',
'access_token' => $facebook->getAccessToken(),
);
$tmp = $facebook->api("/$uid/apprequests", "POST", $param);
but I always get Uncaught OAuthException: (#2) Failed to create any app request thrown
I don't know where is the problem.
You should read the requests documentation.
In there is an explination about two different types of requests.
user initiated (with the request dialog )
app generated (with the Graph API)
What you need is the app generated requests, that means you'll need the apps access token and not the users.
I assume that you are using the users access token because you did not include the initiation of the facebook object in your code sample and have probably verified the user already hence the getAccessToken() call will return the users access token and not the applications access token.
I'm a little confused as to "I'm trying to send a message from an app to a user when a specific event happens. Right now I have this code" means.
Sending an email to a user when someone posts on their wall
Sending an event invite to a user
Sending an app invite to a user
Writing on a users wall when something happens like 'XYZ shared a file with you'.
To answer
You need email and read_stream permissions of the user. Monitor his wall using the RealTime Updates and then email him using your server SMTP.
See http://developers.facebook.com/docs/reference/api/event/#invited on how to create an event invite
As #Lix pointed out, see https://developers.facebook.com/docs/channels/#requests
You should accomplish this using the new Open Graph object/actions. See this example: https://developers.facebook.com/docs/beta/opengraph/tutorial/
You can receive Facebook app access token via:
https://graph.facebook.com/oauth/access_token?client_id=FB_APP_ID&client_secret=FB_APP_SECRET&grant_type=client_credentials
Working code sample to post app-to-user request using Facebook PHP SDK (add error handling where required):
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_APP_SECRET,
));
$token_url = "https://graph.facebook.com/oauth/access_token?" ."client_id=" .
FB_APP_ID ."&client_secret=" . FB_APP_SECRET ."&grant_type=client_credentials";
$result = file_get_contents($token_url);
$splt = explode('=', $result);
$app_access_token =$splt[1];
$facebook->setAccessToken($app_access_token);
$args = array(
'message' => 'MESSAGE_TEXT',
);
$result = $facebook->api('/USER_ID/apprequests','POST', $args);
I'm just starting a new app with the php-sdk. I've done an app a few years ago, but this is the first time with the newer setup dialogs.
The canvas url is pointing to my web server and the app's subdirectory.
Right now this is the only code in my app...just the "hello world" from the php-sdk sample.
<?php
include 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'myappid',
'secret' => 'mysecret',
));
// Get User ID
$user = $facebook->getUser();
?>
Yes, the appid and secret are the actual numbers.
I've waited several minutes to propagate but when going to https://apps.facebook.com/myappsname it just tosses me a 404 error. Is there another URL I should go to when it's in sandbox?
To get https://apps.facebook.com/[APP_NAMESPACE] working, you must specify [APP_NAMESPACE] the App Namespace in the Dev App (https://developers.facebook.com/apps) under Basic settings.
I was developing facebook canvas application, and I found this simple code fails. I don't know what went wrong, because I took it straight from tutorial:
<?php
require 'facebook.php';
/*
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
*/
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true,
));
//Request params
if(!($facebook->getSession()))
{
header("Location:" . $facebook->getLoginUrl(array('req_perms' => 'publish_stream')));
exit;
}
?>
the problem is at header("Location:" . $facebook->getLoginUrl(array('req_perms' => 'publish_stream')));. When I remove it (including removing the exit), the application can run well. However, when I have it, the application doesn;t show anything. Just blank page. And there is "load resource error from channel.facebook.com" on chrome's developer tools.
Can anybody help me spot what when wrong? I don't understand what went wrong In this code. I have made sure that appId and secret are correct.
This might help you:
load resource error from channel.facebook.com is irrelevant to your problem, I've seen that error plenty of times on the facebook page, it is their problem.
Most likely you are getting a blank screen because there is a PHP error, and you have error_reporting set to 0. Try error_reporting(E_ALL) to see if this is the problem.
Where is this code located, is the header you are sending ahead of other text output? Try placing the code on top of the page.
These are just some basic stuff you can check, hope it helps. Good luck.
I had the same problem (blank page) when trying to redirect (in PHP from the server side) to the login URL generated with the function getLoginUrl of the Facebook PHP SDK.
The problem seems to be related to the fact that the redirection is in the iFrame.
The redirection needs to be for the parent frame and not the canvas frame.
In PHP there is no way to tell the browser to redirect the parent frame soo you have to do it in Javascript:
$loginUrl = $facebook->getLoginUrl(array('req_perms' => 'publish_stream'));
echo '<script type="text/javascript">top.window.location="'.$loginUrl.'";</script>';