7

I've switched my files over from a local environment to my vps and now my facebook notification isn't working even thought I'm pretty sure I've updated all the paths correctly. I've tried writing the require path numerous ways. I'm doing a "$.post" from jquery to a php page where the facebook notification is sent and am getting this error:

<b>Fatal error</b>:  Class 'Facebook' not found in 

<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9</b><br />

//THIS IS MY PHP REQUIRE PATH. 

require_once('php-sdk/facebook.php') ;

//IN MY LOCAL ENVIRONMENT I WAS USING THIS PATH BECAUSE IT WAS THE ONLY ONE THAT WORKED. THIS DOESN'T WORK ON MY VPS THOUGH. 

require_once(dirname(__FILE__).'/php-sdk/facebook.php') ;
Spilot
  • 1,495
  • 8
  • 29
  • 55
  • 1
    you need to check where is `facebook.php` located relatively to `accepted.php` file or use absolute paths like you used to do in dev. – Mehdi Karamosly Nov 06 '13 at 21:37
  • 1
    They are both in my public_html directory, except facebook php is in the php-sdk folder which holds the other necessary facebool files for the sdk to work – Spilot Nov 07 '13 at 23:30
  • could it be a right problem ? make sure the files have r-w-execute rights `chmod -R 775` should do the trick. – Mehdi Karamosly Nov 08 '13 at 00:45
  • I'm somewhat new to all of this and I don't know what that means. Which files need to have these rights? – Spilot Nov 08 '13 at 01:00
  • are you connected to a linux machine ? – Mehdi Karamosly Nov 08 '13 at 01:20
  • ok run `ls -l` to see file permissions and make sure they have the correct permissions, or else run `locate facebook.php` it will give you the absolute path, then use that hardcoded path to include your file i hope this helps. – Mehdi Karamosly Nov 08 '13 at 06:59
  • http://stackoverflow.com/questions/14184223/using-require-once-for-up-directory-not-working – User Nov 16 '13 at 07:07
  • what do you get when you `var_dump(explode(PATH_SEPARATOR, get_include_path()));`? – Elias Van Ootegem Nov 21 '13 at 12:12
  • You accepted JakeGould answer, does it mean that the issue is solved and that it was a path issue? – Pierre Nov 21 '13 at 14:04
  • @MehdiKaramosly: That could be excessively permissive. PHP doesn't need an execute bit to run in a webserver (which, admittedly, was not explicitly stated in the question), and if it's an SDK (dir name), it shouldn't need write access if not changing the SDK code itself. – GeminiDomino Nov 21 '13 at 15:06
  • May be problem in your `php-sdk/` directory – Chinmay235 Nov 22 '13 at 13:15

11 Answers11

10

You use require_once and have error "Class 'Facebook' not found". If you tried require_once on a file that does not exist, it would cause other error: "Fatal error: require(): Failed opening required 'php-sdk/facebook.php'". So the path is probably OK. Check if you uploaded php-sdk properly. The facebook.php might be empty.

Veelkoov
  • 1,366
  • 14
  • 26
8

Your error is :

Fatal error</b>:  Class 'Facebook' not found in 
<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9

The valuable information here is :

  • the error occurred because the Facebook class is unknown which means that require_once did not pop this error
  • the error occurred on line 9 of accepted.php

Now you have to look why the class Facebook is unknown on line 9.

  • if you have included the facebook.php before line 9 it is probably not containing the right class. (class names are case sensitive!)
  • if you include facebook.php after line 9 you just have to call it earlier.

PS: posting the first 10-15 lines of accepted.php might give us enough information to pinpoint the exact problem here.

furas
  • 134,197
  • 12
  • 106
  • 148
Daniel P
  • 471
  • 3
  • 9
6

I have faced issues like this before, and the best way to handle this is to set your true filepath as a variable & prepend that to your includes/requires. Becuase the whole dirname(__FILE__) setup can act oddly in different environments especially those that use symbolic links. Explicitly stating where files are to be set is the best solution.

So let’s assume this is your codebase path; as per your example:

/home/zjkkvcxc/public_html/

Set that as a variable that all of your pages load in some way like this:

$BASE_PATH = '/home/zjkkvcxc/public_html/';

And now when you make calls to the file system for your app, do this:

require_once($BASE_PATH . 'php-sdk/facebook.php');

What is nice about a setup like this is that you can make your app portable between environments by just changing $BASE_PATH to match your local environment. Like this might be a path for a MAMP (Mac OS X LAMP) setup:

$BASE_PATH = '/Application/MAMP/htdocs/';

Regarding how odd __FILE__ can act in symlinked environments, read up here:

Since PHP 4.0.2, _ FILE _ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • It can't be a path problem, see Avaer's answer for explanation. – Pierre Nov 20 '13 at 17:14
  • The original poster clearly stated that they had to change paths when migrating from one place to another. While superficially it could simply be an issue of the `class` and not the file being found, without seeing the larger infrastructure in place, it makes most sense that the `path` is the issue. As indicated by the original poster accepting this answer as the solution to their issue. – Giacomo1968 Nov 21 '13 at 00:29
  • 1
    @Pierre Why? I guess it is because my answer was the accepted answer because it solved the user’s problem. Ask them. As for your comment on the -1/+1, that’s basically unwarranted extortion (aka: bullying) so your comment has been flagged as such. Cheers! – Giacomo1968 Nov 21 '13 at 02:20
  • 1
    I am glad that your answer solved this problem, but it's weird. Using `require` or `require_once` should pop a fatal error if anything goes wrong, so the OP should report this to PHP developers, must be a bug. – Daniel P Nov 21 '13 at 07:23
  • @Jake Your explanation isn't an explanation ("because my answer was the accepted answer because it solved the user’s problem"), Daniel well explained what is the problem. For the "bullying", you should have misunderstood my intentions, I said that I want to remove my -1 (because your answer was accepted), but I can't because when I try, I have the following message: "You last voted on this answer 20 hours ago Your vote is now locked in unless this answer is edited". That's why I asked you to edit your answer (just add a space). For the +1, it's a kind of reward for an explanation. – Pierre Nov 21 '13 at 14:00
  • I removed my -1, do you have a real explanation? Is it a bug? I didn't found anything about that, is it something you've ever seen? – Pierre Nov 21 '13 at 16:12
  • @Pierre The only person who can answer the question is the original poster. My guess is that the application in not something that operates as simply as described. And while Avear states that the class could not be found, so it cannot be a file include issue, it could be the case that one file is including another file, which includes yet another file. And if something in that chain breaks, that function does not get loaded. – Giacomo1968 Nov 21 '13 at 16:39
  • 2
    @Pierre : this clearly shows that sometimes the correct answer is not the logical one. It's not Jake's fault that either PHP or the OP gave a incorrect error report. The correct solution doesn't always make sense unless you know all the details (that includes the existence of a bug). Anyway Jake's advice on defining a base path for your includes is certainly good enough to receive credit from the OP. (btw, that's how I handle includes myself and I always use the absolute path). – Daniel P Nov 21 '13 at 19:30
  • That's how I handled my includes too, I use now autoload. I've been fair, I don't understand how is it possible that this answer can be the correct one, but it is, that's why I did undownvote. – Pierre Nov 21 '13 at 23:02
2

Is it possible that:

  • your short_open_tag is enabled on your local environment and
  • it's disabled on your vps and
  • you are using <?instead of <?php in your facebook.php file
r3wt
  • 4,642
  • 2
  • 33
  • 55
Pierre
  • 429
  • 3
  • 15
2

Class not found error not refers to problem loading file. You are using a require , if file not exists a require error will be raised and this is not the case.

Probably the class inside facebook.php is not called Facebook, probably is with another name or with other case like "facebook"

Fatal error</b>:  Class 'Facebook' not found in 
<b>/home/zjkkvcxc/public_html/accepted.php</b> on line <b>9
Dubas
  • 2,855
  • 1
  • 25
  • 37
1

For some odd reason, i encounter circumstances where php doesn't find the file i need through conventional means. i found some code used for searching a directory and retrieving a list of files, which was advantageous in a url generation script i was writing. overtime, i have used this simple script for many tasks, such as finding files when normal require/include fails. in a pinch, this hack works for me.

Just fill in the file name $somefile and directory name $log_directory if your directory is in the base path just type it with no slashes. if its below the base path type it like this /path/to/folder

hope this helps.

$somefile = '<!--Filename You need to find-->';
$log_directory = '<!--Directory you want to search-->';
$results_array = array();
if (is_dir($log_directory))
{
        if ($handle = opendir($log_directory))
        {
            while(($file = readdir($handle)) !== FALSE)
                {

                        $results_array[] = $file;

                }
            closedir($handle);
        }
}

foreach($results_array as $value) 
{
    if ($value == $somefile) {

        $url_include = $log_directory."/".$value;

        }else{

        die("error file not found.");

        }
}

require_once("$url_include");
r3wt
  • 4,642
  • 2
  • 33
  • 55
1
require_once JPATH_SITE.'/php-sdk/facebook.php';
Tony Stark
  • 8,064
  • 8
  • 44
  • 63
saloni
  • 121
  • 4
0

The require path you said is wrong:

require_once('php-sdk/facebook.php') ;

This need there has a file got full pathname: /dir/to/your/actual/include/path/php-sdk/facebook.php

And later you require file in include_path wrong, dirname(__FILE__) OR __DIR__ is directory of the file where require_once() is called, so you are looking for facebook.php in php-sdk sub-directory under you current php script.

To fix your problem, you have 2 plan

  1. Better one, find your actual include_path and put php-sdk files in, include_path can find from php.ini, or run php -m from shell, or phpinfo(), in this way, your first require_once() will work.

  2. Make sure php-sdk directory includes facebook.php is in same parent directory with your calling php script, this is not common usage, but will make your second require_once() work.

I suggest you to study more on php include_path configure and __FILE__ magic constant.

Fwolf
  • 671
  • 4
  • 8
0

Try $_SERVER['DOCUMENT_ROOT'].'/path to /php-sdk/facebook.php'

$_SERVER['DOCUMENT_ROOT'] will give absolute path for www or htdocs folder then you can provide path to facebook.php

r3wt
  • 4,642
  • 2
  • 33
  • 55
Raviraja
  • 406
  • 2
  • 5
  • 17
0
define('DOC_ROOT', realpath(dirname(__FILE__) . '/'));
require_once(DOC_ROOT . '/php-sdk/facebook.php');

Assuming the folder php-sdk is at the root of you website directory.

superphonic
  • 7,954
  • 6
  • 30
  • 63
0
require_once('php-sdk/facebook.php') ;

This assumes that the facebook.php file is in a folder that resides in the current folder of the PHP file beind called.

Is this the case? I suspect not since you have an error!

Are you in a folder up from the root?

---ROOT
---ROOT > FOO --your script here?
---ROOT > php-sdk --facebook in here

If so...

require_once('../php-sdk/facebook.php'); 

would work.