skip to Main Content

I’m having some trouble with the Facebook Marketing API. I need to read all the active campaings the current user has, in all of his ad accounts. So, the first step is to read all the ad accounts.

First, I tried what the documentation says:

// Add to header of your file
use FacebookAdsObjectAdUser;

// Add after Api::init()
$me = new AdUser('me');
$my_adaccount = $me->getAdAccounts()->current();

But the AdUser.php file doesn’t exists in the SDK.

So, I tried doing a request to the endpoint /me/adaccounts

$fb = new Facebook([
    'app_id'     => AppId,
    'app_secret' => AppSecret,
]);

$response = $fb->get('/me/adaccounts', Token);

This throws an error saying I’m using a deprecated version of the Marketing API (but I’m using the 3.2!!). I also tried

$response = $fb->get('me?fields=adaccounts', Token);

And got the same error. Can anyone help me find another way??

PS: English is not my first language, sorry about that…

3

Answers


  1. You can check campaigns using insights api. Type following line in explorer and see the magic

    me/campaigns?fields=id,name,status,effective_status

    This will give you list of 25 campaigns and you can read their status & effective status.
    You can use php sdk syntax call above api url and read response.

    Login or Signup to reply.
  2. I had the same problem. Check this solution!

        // Add after Api::init()
    
        $user = new FacebookAdsObjectUser();
        $user->setId($userID);
        $user->setApi($api);
    
        $accounts = $user->getAdAccounts(array(FacebookAdsObjectFieldsAdFields::NAME));
        foreach ($accounts as $acc) {
          echo $acc->name;
        }
    
    Login or Signup to reply.
  3. Using php SDK

    try {
      // Returns a `FacebookFacebookResponse` object
      $response = $fb->get(
        '/<USER_ID>/adaccounts',
        '{access-token}'
      );
    } catch(FacebookExceptionsFacebookResponseException $e) {
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
    } catch(FacebookExceptionsFacebookSDKException $e) {
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search