2

I had a working site using facebook api version 2.0 but was having some problems. I realized some of my calls were depreciated and so I set up composer and included the facebook api.

I think my problem is how I am including the api in my site.

I was using require 'facebookAPI/src/facebook.php'; Now that I am using composer this does not work.

file path is, vendor/facebook/php-sdk-v4/src/Facebook/ There is no facebook.php file in here.

How do I add the api? If facebook.php does not exist anymore? Do I use different files to achieve different goals? And last.. Can I use the autoloader by

require 'vendor/autoload.php';

If is there anything else i'm supposed to do from there?

Here is my full current code that just shows a blank page when the page loads.

<?php       
require 'vendor/autoload.php';

$facebook = new Facebook(array(
  'appId'  => 'foo foo foo',
  'secret' => 'foo foo foo',
));

$user = $facebook->getUser(); 

if ($user) {
  try {
    $user_info          = $facebook->api('/' . $user);
    $user_tags          = $facebook->api('/' . $user . '/tagged_places');
    $friends            = $facebook->api('/' . $user . '/friends');
    $user_feed          = $facebook->api('/' . $user . '/feed/?with=location');
    $friends_locations  = $facebook->api('/' . $user . '/friends/?fields=location');
    $user_checkins      = $facebook->api('/' . $user . '/checkins');
    $friends_checkins   = $facebook->api('/' . $user . '/friends/checkins');

  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

if ($user) {
$params = array( 'next' => 'http://www.wuno.com/sandbox/actions/fbLogout.php' );
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
  $loginUrl = $facebook->getLoginUrl(array(
    'scope' => 'user_tagged_places, email, publish_actions, publish_stream, user_birthday, user_work_history, user_hometown, user_photos, user_about_me, user_checkins, friends_checkins, user_location, friends_location, read_stream, user_status, friends_status')
  );
}
?>
wuno
  • 9,547
  • 19
  • 96
  • 180
  • If you had Facebook API v2.0 working why are you changing it? – WizKid May 31 '14 at 23:03
  • because the friend_ permissions are depreciated and I am trying to use the user_tagged_places permission with tagged_places call and I was told in order to get that to work I needed to use the new api – wuno May 31 '14 at 23:05
  • In fact you told me friends_ permissions are depreciated – wuno May 31 '14 at 23:06
  • Facebook PHP SDK v4 supports both Facebook API v1.0 and v2.0. But if you created your app after 4/30/2014 or if you app was not active before than you will have to use v2.0 – WizKid May 31 '14 at 23:06
  • active meaning approved or you mean created on my developer page – wuno May 31 '14 at 23:07
  • No it doesn't have to be approved to be active. But you can't have created the app before 4/30/2014 and then done nothing with it – WizKid May 31 '14 at 23:08
  • ok well using the set up with composer would you mind enlightening me on what I need to include in place of facebook.php to get this going. If I need to start over im fine with that. I just want to start from the top of the page and work my way down so I can understand this better – wuno May 31 '14 at 23:10
  • I never used composer but there is information at https://developers.facebook.com/docs/php/gettingstarted/4.0.0 on how to use the SDK with composer – WizKid May 31 '14 at 23:11
  • ya well can you tell me what files you would include from the api in src to make this work without composer? – wuno May 31 '14 at 23:13
  • Simply don't go without Composer. You didn't even report you ran into problems, you seem to just try to figure out where your files went. Don't worry about file location with Composer. Include the generated autoloader and instantiate classes after that right away. – Sven Jun 01 '14 at 21:36
  • yes as of now I got the files initiated but I just keep seeing the same error no matter what Fatal error: Class 'facebook\src\Facebook\FacebookSession' from here FacebookSession::setDefaultApplication( 'foo','foo' ); – wuno Jun 01 '14 at 21:40
  • You seem to be missing two points: 1, sdk v4 works quite differently than the 3.x branch; and 2, v4 uses namespaces so you need to use the FQCN or add a `use` statement. – Maerlyn Jun 11 '14 at 09:41
  • @NichoDiaz: If you have an answer to your own question then please post a description of your chosen solution as an answer and accept it yourself. Stack Overflow is much more about building a useful reference library of solutions than than it is about getting your own problem fixed – Borodin Jun 12 '14 at 01:24
  • Agreed. And I will! :) – wuno Jun 12 '14 at 04:44

1 Answers1

2

Facebook api v-4 uses namespaces

namespace
A namespace is a way of grouping code that exists across multiple files without having a naming collision. That is, you can have the same named class in two different places if they are encapsulated within namespaces.

so in the code in the question above the FB API is loaded with vendor and called like this,

require 'vendor/autoload.php';

Now that the FB API is added you just need to use the namespace of the classes you require for whatever you're trying to accomplish.

In my situation I ditched vendor and added these classes like so,

  // include required classes from Facebook SDK
require_once( 'facebook/src/Facebook/FacebookHttpable.php' );
require_once( 'facebook/src/Facebook/FacebookCurl.php' );
require_once( 'facebook/src/Facebook/FacebookCurlHttpClient.php' );
require_once( 'facebook/src/Facebook/FacebookSession.php' );
require_once( 'facebook/src/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'facebook/src/Facebook/FacebookRequest.php' );
require_once( 'facebook/src/Facebook/FacebookResponse.php' );
require_once( 'facebook/src/Facebook/FacebookSDKException.php' );
require_once( 'facebook/src/Facebook/FacebookRequestException.php' );
require_once( 'facebook/src/Facebook/FacebookOtherException.php' );
require_once( 'facebook/src/Facebook/FacebookAuthorizationException.php' );
require_once( 'facebook/src/Facebook/GraphObject.php' );
require_once( 'facebook/src/Facebook/GraphSessionInfo.php' );

// Called class with namespace
use Facebook\FacebookHttpable;
use Facebook\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;

Please take note this is done with \ and not /

Each task you want to achieve with the FB API may require different classes then the ones included in my answer.

wuno
  • 9,547
  • 19
  • 96
  • 180
  • 2
    The SDK now includes an autoloader: http://stackoverflow.com/questions/23532858/facebook-graph-api-php-sdk-v4-post-on-page/23942425#23942425 – SammyK Jun 27 '14 at 12:11
  • So if I read this right, did you get the API to work WITHOUT the composer? I'm busy trying to do this myself. – Phillip Jul 25 '14 at 01:36
  • Tell me what your having a problem with and Ill try to help you – wuno Jul 25 '14 at 01:40
  • Would you mind posting the full extent of your code (like the first post) with everything working? I'm essentially just trying to figure out how to initialize the API without using Composer. – Phillip Jul 25 '14 at 01:40
  • Ok make a question on SO post a description of your exact problem and what ever your code is. Ill answer your question and ill post something on github to try to help you. – wuno Jul 25 '14 at 01:44
  • Is this really an answer as your own question title is "Using PHP Composer For Facebook API"? I know you have a workaround but it doesnt really answer the question posed. – Craicerjack Nov 19 '14 at 16:30