开发者

How to share an instance across several php files a-la-Wordpress?

Greetings Everyone,

I'm currently using Wordpress as my CMS and leveraging Facebook's Graph API for the user account/registration "stuff".

My question is how do I use Facebook's PHP SDK (found here: http://github.com/facebook/php-sdk/), when Wordpress compiles several php files to output a single webpage? In other words, Wordpress' templates header.php + index.php + footer.php = what you see on www.example.com

If I create my instance of the Facebook Application in HEADER.PHP, how will I make references to them in INDEX.PHP and FOOTER.PHP?

开发者_开发百科Thanks in advance!

----- SAMPLE CODE ---

in header.php

<?php
require '/facebook.php';

// Create our Application instance.
$facebook = new Facebook(array(
  'appId' => 'xxx',
  'secret' => 'xxx',
  'cookie' => true,
));
?>

in index.php

<?php
$session = $facebook->getSession();
?>


If you set $facebook in header.php, you won't be able to access it in index.php unless you globalise it.

This is because header.php is loaded in via the function get_header(), which narrows the scope of all non-global variables.

So you could either globalise $facebook OR, since you want a single instance of Facebook, I'd strongly recommend using a singleton helper function;

function get_facebook_instance()
{
    static $f;
    if (!isset($f)) {
        if (!class_exists('Facebook'))
            require('/facebook.php');
        $f = new Facebook(array(
            'appId' => 'xxx',
            'secret' => 'xxx',
            'cookie' => true,
        ));
    }
    return $f;
}

Note: If you're using PHP5, you don't have to worry about returning by reference.


Once you include the facebook client in your header.php file and create an instance of that:

$facebook = new Facebook('API', 'SECRET');

You can then access the facebook client instance $facebook from your index.php file or even footer.php file or anyother file on which header.php is included.


PHP is processed in order, that is you need to make sure that header.php is included before the call happens in index.php. It's possible that overall you have something like this:

In index.php

<?php
// blah blah other code
$session = $facebook->getSession();
// blah blah more code
include('header.php');

Which of course would be read as:

<?php
// blah blah other code
$session = $facebook->getSession();
// blah blah more code
require '/facebook.php';
// Create our Application instance.
$facebook = new Facebook(array(
  'appId' => 'xxx',
  'secret' => 'xxx',
  'cookie' => true,
));

You can see when you look at the overall code that $facebook wouldn't exist yet.

Add a var_dump($facebook); to the end of header.php (after you have set $facebook and also one to the start of index.php (right before you try to use $facebook).

The other thing that it might be is that you call to the Facebook Graph API isn't working correctly and is returning false or something. Your var_dump output will be the same if $facebook exists and is hasn't connected properly (or something like that), but will be different if you try to access $facebook before you have set it (one will be NULL and will generate an E_NOTICE, the other will be a Facebook object - or whatever is returned from the Facebook Graph API call).


Consider using the Wordpress Facebook Connect Plugin, which already implements this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜