PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 26, 2022

[FIXED] Fatal Error "Class not found" Facebook Ads SDK

 February 26, 2022     facebook-graph-api, facebook-php-sdk, php     No comments   

Issue

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.


Solution

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.

  • https://github.com/facebook/facebook-php-ads-sdk
  • http://getcomposer.org - You can read more about composer here.


Answered By - Evan Chen
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing