致命错误 "Class not found" Facebook 广告 SDK
Fatal Error "Class not found" Facebook Ads SDK
我正在尝试安装 PHP 的 Facebook 广告 SDK 并在我的服务器上运行,但我遇到了一些无法解决的问题。
这是我在服务器上安装 SDK 的方式:
/var/www/vhosts/mydomain.com/httpdocs/ads-sdk/ -> (listing sub directories)
/examples/
/src/
/test/
autoload.php
index.php
我在 "ads-sdk" 目录中有一个 index.php 文件。
我只是试图将 SDK 中的一些文件包含到我的 index.php 文件中,如下所示:
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);
我在 index.php 文件中使用了一个自动加载器,这里是它的代码:
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 引发致命错误:
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
这是 AdUser.php 的第 34 行:
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
我是 PHP 中命名空间的新手,无法弄清楚可能出了什么问题,以及为什么找不到 AbstractCrudObject class。
您应该使用 Composer 将 SDK 包含到您的代码中(这让这一切变得非常简单)。在 Github 自述文件中有关于如何设置 composer、从哪里获得它以及如何使用它的演练。
- https://github.com/facebook/facebook-php-ads-sdk
- http://getcomposer.org - 您可以在此处阅读有关作曲家的更多信息。
我想为 2019 年来到这里的任何人更新这个问题:
新 SDK 现在称为 Business SDK
Create composer.json with the following contents:
{
"name": "name/my_test_php_app",
"type": "project",
"require": {
"facebook/php-business-sdk": "^3.0.0"
},
"authors": [
{
"name": "Your Name",
"email": "some@email.com"
}
]
}
Install the SDK by running the following command in your terminal window:
composer install
我正在尝试安装 PHP 的 Facebook 广告 SDK 并在我的服务器上运行,但我遇到了一些无法解决的问题。
这是我在服务器上安装 SDK 的方式:
/var/www/vhosts/mydomain.com/httpdocs/ads-sdk/ -> (listing sub directories)
/examples/
/src/
/test/
autoload.php
index.php
我在 "ads-sdk" 目录中有一个 index.php 文件。
我只是试图将 SDK 中的一些文件包含到我的 index.php 文件中,如下所示:
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);
我在 index.php 文件中使用了一个自动加载器,这里是它的代码:
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 引发致命错误:
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
这是 AdUser.php 的第 34 行:
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
我是 PHP 中命名空间的新手,无法弄清楚可能出了什么问题,以及为什么找不到 AbstractCrudObject class。
您应该使用 Composer 将 SDK 包含到您的代码中(这让这一切变得非常简单)。在 Github 自述文件中有关于如何设置 composer、从哪里获得它以及如何使用它的演练。
- https://github.com/facebook/facebook-php-ads-sdk
- http://getcomposer.org - 您可以在此处阅读有关作曲家的更多信息。
我想为 2019 年来到这里的任何人更新这个问题:
新 SDK 现在称为 Business SDK
Create composer.json with the following contents:
{
"name": "name/my_test_php_app",
"type": "project",
"require": {
"facebook/php-business-sdk": "^3.0.0"
},
"authors": [
{
"name": "Your Name",
"email": "some@email.com"
}
]
}
Install the SDK by running the following command in your terminal window:
composer install