问题描述:

I spent a lot of time trying to integrate the Facebook PHP SDK (v4) to my site on ZF1 without results, I tryed a lot of things I found on internet too, so this is my last resource (I dont like to bother anyone). In my poor understanding of ZF1 (I'm learning) I copied the 'src' folder from the Facebook PHP SDK (v4) into the library/zend/ folder, resulting on this http://imgur.com/ycWoleK, now in the FacebookSession.php file I changed this

class FacebookSession{

into this:

class Zend_Facebook_Facebook{

and in my controller/index I invoke like this

Zend_Facebook_Facebook::setDefaultApplication('YOUR_APP_ID', 'YOUR_APP_SECRET');

And this is what I get:

Warning: include_once(Zend/Facebook/Facebook.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/www/testfb/library/Zend/Loader.php on line 141

Warning: include_once(): Failed opening 'Zend/Facebook/Facebook.php'

for inclusion

(include_path='/Applications/MAMP/htdocs/www/testfb/application/../library:/Applications/MAMP/htdocs/www/testfb/library:.:/Applications/MAMP/bin/php/php5.5.10/lib/php')

in /Applications/MAMP/htdocs/www/testfb/library/Zend/Loader.php on

line 141

Fatal error: Class 'Zend_Facebook_Facebook' not found in

/Applications/MAMP/htdocs/www/testfb/application/controllers/IndexController.php

on line 15

I don't know whay I have to do! I tried a lot of things and nothing works! I looked for a lot of guides and only uses a previos version of the Facebook SDK (that one with only 3 files, facebook.php, base_facebook.php and a cert).

Some suggestion? Someone is running this sdk now? how?

Thanks a lot and sry for the post! (And sry about my english)

网友答案:

Open your class file Zend_Facebook_Facebook and comment out below line and then try:

//namespace Facebook;

Also remember if you have edited class name of FacebookSession to Zend_Facebook_Facebook then file name also should be Facebook.php instead of FacebookSession.php

网友答案:

So I ran in the same problem but for a different reason. I'll point it as last step.

  1. Add the FB SDK in you library folder the way it is in the zip you downloaded.

  2. The facebook SDK is provided with a file autoload.php which you can add on your bootstrap like so:

    protected function _initAutoload ()
    {
        $this->getApplication()
             ->getAutoloader()
             ->pushAutoloader(array('Doctrine', 'autoload'))
             ->pushAutoloader(array('Doctrine_Core', 'modelsAutoload'));
    
        require_once '/facebook-php-sdk-v4-4.0-dev/autoload.php';
    }
    
  3. Alternatively you can add it to the Zend autoloader, which can be weird since the Facebook one use lambda and makes the code a bit ugly. Also you need to remove the file_exist() all together if you have the FB SDK on a lib on your include_path. In my case I do, than it ended like so:

    protected function _initAutoload ()
    {
        define('FACEBOOK_SDK_V4_SRC_DIR', 'facebook-php-sdk-v4-4.0-dev/src/Facebook/');
        $this->getApplication()
            ->getAutoloader()
            ->pushAutoloader(function ($class)
            {
              // project-specific namespace prefix
              $prefix = 'Facebook\\';
    
              // base directory for the namespace prefix
              $base_dir = defined('FACEBOOK_SDK_V4_SRC_DIR') ? FACEBOOK_SDK_V4_SRC_DIR : __DIR__ . '/src/Facebook/';
    
              // does the class use the namespace prefix?
              $len = strlen($prefix);
              if (strncmp($prefix, $class, $len) !== 0) {
                // no, move to the next registered autoloader
                return false;
              }
    
              // get the relative class name
              $relative_class = substr($class, $len);
    
              // replace the namespace prefix with the base directory, replace namespace
              // separators with directory separators in the relative class name, append
              // with .php
              $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
    
                require $file;
                return true;
            })     
            ->pushAutoloader(array('Doctrine', 'autoload'))
            ->pushAutoloader(array('Doctrine_Core', 'modelsAutoload'));
    
    
    }
    

    As you have to come up with a custom autoloader for this method I think it is advisable to create a class MyNamespace_Facebook_Autoloader to host this ugliness in a cleaner fashion. Got to mess with it to make it work, make it right to the end, is my point of view. You can use Zend_Loader_Autoloader_Interface to implement it.

  4. Last and very important, when you calling a Facebook class never forget to add the whole name. Facebook\FacebookSession::setDefaultApplication() NOT FacebookSession::setDefaultApplication().

And there you have it, no need to edit any Facebook file and working. Hope it make you code better. Happy coding!

相关阅读:
Top