I am trying to get the Facebook Ads SDK for PHP installed and working on my server, but I am experiencing some issues that I can't figure out.
This is the way I have the SDK installed on my server:
/var/www/vhosts/mydomain.com/httpdocs/ads-sdk/ -> (listing sub directories)
/examples/
/src/
/test/
autoload.php
index.php
I have an index.php file sitting in the "ads-sdk" directory.
I am just attempting to include some files from the SDK into my index.php file as follows:
require (__DIR__ . '/autoload.php');
require_once(__DIR__ .'/src/FacebookAds/Api.php');
require_once(__DIR__ .'/src/FacebookAds/Object/AdUser.php');
require_once(__DIR__ .'/src/FacebookAds/Object/Fields/AdAccountFields.php');
require_once(__DIR__ .'/src/FacebookAds/Object/Fields/ConnectionObjectFields.php');
require_once(__DIR__ .'/src/FacebookAds/Object/Fields/ConnectionObjectTypes.php');
use FacebookAds\Api;
use FacebookAds\Object\AdUser;
use FacebookAds\Object\Fields\AdAccountFields;
use FacebookAds\Object\Fields\ConnectionObjectFields;
use FacebookAds\Object\Values\ConnectionObjectTypes;
Api::init($app_id, $app_secret, $access_token);
I am using an autoloader in the index.php file, and here is the code for that:
spl_autoload_register(function ($class)
{
// project-specific namespace prefix
$prefix = 'FacebookAds\\';
// base directory for the namespace prefix
$base_dir = defined('FACEBOOK_SDK_V4_SRC_DIR') ? FACEBOOK_SDK_V4_SRC_DIR : __DIR__ . 'src/FacebookAds/';
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// 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';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
index.php throws a Fatal Error:
Fatal error: Class 'FacebookAds\Object\AbstractCrudObject' not found in /var/www/vhosts/mydomain.com/httpdocs/ads-sdk/src/FacebookAds/Object/AdUser.php on line 34
This is line 34 of AdUser.php:
namespace FacebookAds\Object;
use FacebookAds\Object\Fields\AdUserFields;
use FacebookAds\Object\Traits\CannotCreate;
use FacebookAds\Object\Traits\CannotDelete;
use FacebookAds\Object\Traits\CannotUpdate;
use FacebookAds\Object\Traits\FieldValidation;
use FacebookAds\Cursor;
class AdUser extends AbstractCrudObject { <-- line 34
I am new to namespaces in PHP, and cannot figure out what could be going wrong, and why the AbstractCrudObject class cannot be found.
You should be using composer to include the SDK into your code (it makes this all really simple). There is a walkthrough of how you should set up composer, where to get it, and how to use it on the Github README.