Facebook Sdk V4 for PHP Minimal Example

I need a minimal example specifically for the Facebook Graph API (using PHP)

I used this tutorial. It has everything in it including the access token thing.

http://www.benmarshall.me/facebook-sdk-php-v4/

I hope this will be useful for other users googleing this issue.

Facebook Graph API PHP SDK v4 - Post on Page

Update: June, 27 2014, The SDK now comes with a built-in autoloader for those who can't use composer.

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

If that doesn't automatically find the path for you, you can define it with FACEBOOK_SDK_V4_SRC_DIR.

define('FACEBOOK_SDK_V4_SRC_DIR', '/path/to/facebook-php-sdk-v4/src/Facebook/');
require __DIR__ . '/path/to/facebook-php-sdk-v4/autoload.php';

The internals of the SDK rely on several other classes that you're not including. That's why autoloading is really important here.

Autoloading With Composer

The best way to do this is to install composer. And add the SDK in a composer.json file to the root of your project.

{
"require" : {
"facebook/php-sdk-v4" : "4.0.*"
}
}

Then run composer install from the command line where the composer.json file is. Then include the autoloader at the top of your script.

require_once __DIR__ . '/vendor/autoload.php';

Manual Autoloading

An alternative way to autoload these files is to replace your require_once's at the top with this solution from rm-vanda:

function facebookLoader($class) {
require "/path/to/facebook-php-sdk-v4-master/src/" . str_replace("\\", "/", $class) . ".php";
}

spl_autoload_register("facebookLoader");

CodeIgniter Facebook SDK 4 with Composer

You've mixed the position of the USE statement.
What you might do is to declare the classes from the FB SDK outside and before class, and not inside. By using Use inside a class you are pointing to trait functionality, which should be included into the class.

<?php
class MyClass extends MyBaseClass {
// this is a namespaced trait inside the class
// = extend class with trait
use SomeWhere\Trait;
}
?>

--

<?php
// this is the declaration of a namespaced class outside of the class
use SomeWhere\Class;

class MyClass extends MyBaseClass
{
public function helloWorld()
{
$c = new Class;
// ...
}
}
?>

--

Your code becomes:

<?php
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;

class Welcome extends CI_Controller {

public function index()
{
FacebookSession::setDefaultApplication('app id removed', 'app secret removed');

}
}

Facebook php-sdk v4 PhalconPHP integration

I've worked this one out. The issue was that the browser was automatically making a request to /favicon.ico as well,as I didn't have a favicon.ico this then rendered the default indexAction again and as such this was causing the getLoginUrl() method to fire again generating a new state. The simple fix is to just create a favicon, or define the error handling route for files not there (I was just using the boilerplate from the phalcon dev tools initially)



Related Topics



Leave a reply



Submit