Facebook SDK v4 for PHP Minimal Example

前端 未结 8 868
夕颜
夕颜 2020-12-02 14:44

I\'m trying to get the minimal example

using Facebook\\FacebookSession;

FacebookSession::setDefaultApplication(\'YOUR_APP_ID\',\'YOUR_APP_SECRET\');

// Use         


        
相关标签:
8条回答
  • 2020-12-02 15:20

    have recently solved this. as there is autoload.php file available with sdk you dont need to use require etc etc. just include that autoload.php on the start

     <?php
    session_start();
    // added in v4.0.0
    require_once 'autoload.php';
    
    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\Entities\AccessToken;
    use Facebook\HttpClients\FacebookCurlHttpClient;
    use Facebook\HttpClients\FacebookHttpable;
    
    // start session
    
    // init app with app id and secret
    FacebookSession::setDefaultApplication( 'app-id','app-secret' );
    
    // login helper with redirect_uri
    
        $helper = new FacebookRedirectLoginHelper('http://yourhost/facebook/' );
    
    try {
      $session = $helper->getSessionFromRedirect();
    } catch( FacebookRequestException $ex ) {
      // When Facebook returns an error
    } catch( Exception $ex ) {
      // When validation fails or other local issues
    }
    
    // see if we have a session
    if ( isset( $session ) ) {
      // graph api request for user data
      $request = new FacebookRequest( $session, 'GET', '/me' );
      $response = $request->execute();
      // get response
      $graphObject = $response->getGraphObject();
    
      // print data
      echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
    } else {
      // show login url
      echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
    }
    
    ?>
    

    after this you must have to check the path in autoload.php file

    $base_dir = defined('FACEBOOK_SDK_V4_SRC_DIR') ? FACEBOOK_SDK_V4_SRC_DIR : __DIR__ . '/src/Facebook/';
    

    this line is default code if u have changed the name of directories like placed all the files from /src/Facebook/ to /sdk/ then just replace the name always check the included path by using die(__DIR__ . '/src/Facebook/'); to make sure if it is correct.

    0 讨论(0)
  • 2020-12-02 15:25
    1. PHP Version 5.4.0 or higher is required.
    2. Facebook uses Implementations of PSR-4. Hence You dont have to use require or require_once or include or include_once.
    3. In PSR-4, you just need packagename(namespace) i.e. directory name and class file name only.It will register classes dynamically from given package name.Ex.:- use packaname\classname.
    4. You will find the file autoload.php in Facebook SDK Autoload root directory.
    5. use is used to load dynamic classes using spl_autoload_register
    6. Facebook register all library using autoload.php or autoload_fb.php
    7. You have to find autoload.php in your downloaded library like facebook-php-sdk-v4-4.0-dev/.
    8. If you just wants to use Facebook library from download source.Then you have to copy autoload.php in your root directory or in Facebook directory.
    9. defined constant for FACEBOOK_SDK_V4_SRC_DIR i.e. path of the facebook library
    10. You need to do as below to use in php

    Note: I have copied /var/www/stack/24006673/facebook-php-sdk-v4-4.0-dev/src/Facebook directory and /var/www/stack/24006673/facebook-php-sdk-v4-4.0-dev/autoload.php file in root directory /var/www/stack/24006673/

    define('FACEBOOK_SDK_V4_SRC_DIR','/var/www/stack/24006673/Facebook/');
    require_once("autoload.php");
    use Facebook\FacebookSession;
    use Facebook\FacebookRequest;
    use Facebook\GraphUser;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookRedirectLoginHelper;
    FacebookSession::setDefaultApplication('YOUR_APP_ID','YOUR_APP_SECRET');
    
    0 讨论(0)
  • 2020-12-02 15:27

    To avoid using require or require_once and to have a better project structure you should follow the following steps:

    1. Install composer from https://getcomposer.org/
    2. Create composer.json file in your web application root with the following contents: { "require" : { "facebook/php-sdk-v4" : "4.0.*" } }

    3. Call php composer.phar install

    4. Create a folder src/Acme/App
    5. Create a file src/Acme/App/Run.php with the following contents:

    <?php

    namespace Acme\App;
    
    use Facebook\FacebookSession;
    use Facebook\FacebookRequest;
    use Facebook\GraphUser;
    use Facebook\FacebookRequestException;
    
    class Run {
        const APP_ID = 'app_id';
        const APP_SECRET = 'app_secret';
    
        public function __construct()
        {
            FacebookSession::setDefaultApplication(self::APP_ID, self::APP_SECRET);
           // ...your code goes here...
        }
    
    }
    
    1. Create an index.php file with the following contents:

    <?php

    $loader = require_once 'vendor/autoload.php';
    $loader->add('Acme\App', __DIR__.'/src');
    $app = new Run();
    

    This will be a skeleton of your Facebook application (all your further classes should follow PSR-4 standard). Using composer's autoloader you will not need to use require_once for classes in any registered namespace.

    P.S. You may want to remove all your libraries from the web root. It will help to prevent direct access from web to them. Then you can just adjust the pathnames and keep only index.php in your web root. I hope, it's clear.

    0 讨论(0)
  • 2020-12-02 15:35

    There is a complete (working) example here: http://metah.ch/blog/2014/05/facebook-sdk-4-0-0-for-php-a-working-sample-to-get-started/

    Code:

    session_start();
    require_once( 'Facebook/FacebookSession.php' );
    require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
    require_once( 'Facebook/FacebookRequest.php' );
    require_once( 'Facebook/FacebookResponse.php' );
    require_once( 'Facebook/FacebookSDKException.php' );
    require_once( 'Facebook/FacebookRequestException.php' );
    require_once( 'Facebook/FacebookAuthorizationException.php' );
    require_once( 'Facebook/GraphObject.php' );
    
    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    
    // init app with app id (APPID) and secret (SECRET)
    FacebookSession::setDefaultApplication('APPID','SECRET');
    
    // login helper with redirect_uri
    $helper = new FacebookRedirectLoginHelper( 'http://www.metah.ch/' );
    
    try {
      $session = $helper->getSessionFromRedirect();
    } catch( FacebookRequestException $ex ) {
      // When Facebook returns an error
    } catch( Exception $ex ) {
      // When validation fails or other local issues
    }
    
    // see if we have a session
    if ( isset( $session ) ) {
      // graph api request for user data
      $request = new FacebookRequest( $session, 'GET', '/me' );
      $response = $request->execute();
      // get response
      $graphObject = $response->getGraphObject();
    
      // print data
      echo  print_r( $graphObject, 1 );
    } else {
      // show login url
      echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
    }
    
    0 讨论(0)
  • 2020-12-02 15:35

    just put this on include page:

    require DIR . '/path/to/facebook-php-sdk-v4/autoload.php';

    Also call the class before use, each time you need:

    use Facebook\FacebookSession;

    0 讨论(0)
  • 2020-12-02 15:36

    Yeah, the new tutorials aren't very helpful.

    I'm running through them, myself, and just to get the examples to work, I did:

    function facebookLoader($class) {
        require "/path/to/facebook-php-sdk-v4-master/src/" . str_replace("\\", "/", $class) . ".php";
    }
    
    spl_autoload_register("facebookLoader");
    
    Facebook\FacebookSession::setDefaultApplication([...]
    

    and prepended Facebook\ anywhere where was a class name being called.

    Also, there's a part in the tutorial where it says to use: FacebookRedirectLoginHelper();

    When you, in fact, still have to give the thing input:

    FacebookRedirectLoginHelper("http://yourRedirectUri.com/");

    Got me through the tutorial - ! Now on to figuring out how to get the user's email/complete profile information.

    0 讨论(0)
提交回复
热议问题