Reputation: 1570
I'm not a die-hard programmer and I don't do much of it anymore, but I figured I'd better update some of my code to actually work inside Facebook since the new version of the API is coming out and I'm still using 1.0 on all my apps.
Is there a way to use the new API and the new PHP SDK v4.0 without using a composer? I do so little with the API (besides log in really) that I don't need much. Facebook provides me a nice environment to create apps and then invite my friends to use them. They handle all the login and user identification so I don't have to.
I've tried a little bit to get into the new API and I'm noticing its little more complicated than before. Previously all I had to do was require the facebook.php file, add a couple little lines of code and presto! Now its not that way (or I'm just missing something really easy).
I'd like to avoid using a composer as that would require installing it on a server. I don't personally mind using something new (never used one before), but some of my stuff is for other people and asking them to do the same is not as easy.
If anyone could lend a hand, that'd be great. Thanks.
If it helps, this is my current (version 1.0) login code. I need to make it work with v2.0.
require_once '../fb-php-sdk/src/facebook.php';
$facebook_id=false;
$facebook=new Facebook(array('appId'=>$id,'secret'=>$secret));
$facebook_id=$facebook->getUser();
if($facebook_id)
{
try
{
$user_data['facebook']=$facebook->api('/me');
}
catch(FacebookApiException $e)
{
error_log($e);
$facebook_id=false;
}
}
if(!$facebook_id)
{ ?>
<script>
top.location.href="<?php echo $facebook->getLoginUrl(array('redirect_uri'=>$url)); ?>";
</script> <?php
exit();
}
Upvotes: 1
Views: 1397
Reputation: 1570
Turns out I had done something wrong with the Use statements. I'd never seen them before and didn't realize how vital they were to be required in the same file as the calls being made to the SDK. The following code works the way I need it to without Composer:
<?php
session_start();
require_once 'autoload.php';
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookStream;
use Facebook\HttpClients\FacebookStreamHttpClient;
use Facebook\FacebookAuthorizationException;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookClientException;
use Facebook\FacebookJavaScriptLoginHelper;
use Facebook\FacebookOtherException;
use Facebook\FacebookPageTabHelper;
use Facebook\FacebookPermissionException;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookServerException;
use Facebook\FacebookSession;
use Facebook\FacebookSignedRequestFromInputHelper;
use Facebook\FacebookThrottleException;
use Facebook\GraphAlbum;
use Facebook\GraphLocation;
use Facebook\GraphObject;
use Facebook\GraphPage;
use Facebook\GraphSessionInfo;
use Facebook\GraphUser;
use Facebook\GraphUserPage;
FacebookSession::setDefaultApplication($app_info['id'],$app_info['secret']);
$helper=new FacebookCanvasLoginHelper();
try
{
$session=$helper->getSession();
}
catch(FacebookRequestException $ex)
{
echo "Facebook Error: ".$ex->getMessage();
exit();
}
catch(\Exception $ex)
{
echo "Validation Failed: ".$ex->getMessage();
exit();
}
if(!isset($session))
{ ?>
<script>
top.location.href="https://www.facebook.com/dialog/oauth?client_id=<?php echo $app_info['id']; ?>&redirect_uri=<?php echo $app_info['canvas_url']; ?>";
</script> <?php
exit();
}
$facebook_data=(new FacebookRequest($session,'GET','/me'))->execute()->getGraphObject(GraphUser::className());
?>
Upvotes: 0
Reputation: 9885
I took a working login using Facebook API V4
and simplified it to help you get it up and running.
To make this simple, forget what you have been using and go to this link and download the file,
https://github.com/wuno/Facebook-Login-Example
This is a simple login with the Facebook V-4 APi included.
All you have to do is deploy the file to your server and you can get this login working in minutes.
Change three lines of code.
each line you need to change has this comment above it,
// EDIT THIS LINE ********************************************
Upload the whole folder to your server, do not change any file paths.
Create a new application at https://developers.facebook.com/ to acquire an App ID and App Secret.
Change 3 lines of code in the index.php file.
After setup to view the website go to http://www.YOUR/URL.com/Facebook-Login-Example
Upvotes: 3