skip to Main Content

This is how facebook tells people to create a campaign according to the documentation:
https://developers.facebook.com/docs/marketing-api/buying-api/

However, what is the null field parameter inside the new Campaign constructor? Anything I put in there becomes the Campaign’s ID, but an invalid one.
How do you get the campaign’s ID?

When I run this code, I get no errors, but nothing is created on my Ads Manager, and when i echo $campaign->id it is just blank, because the id is null. However, if i print_r($campaign) it returns a large object with many fields, but no ID field.

Facebook code:

use FacebookAdsObjectCampaign;
use FacebookAdsObjectFieldsCampaignFields;
use FacebookAdsObjectValuesCampaignObjectiveValues;

$campaign = new Campaign(null, 'act_<AD_ACCOUNT_ID>');
$campaign->setData(array(
CampaignFields::NAME => 'My campaign',
CampaignFields::OBJECTIVE => CampaignObjectiveValues::LINK_CLICKS,
));

$campaign->create(array(
 Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED,
));

Here is the facebook documentation:
https://developers.facebook.com/docs/marketing-api/using-the-api

However, it isn’t very useful because it just points to a quick start guide that gives no relevant information.

2

Answers


  1. You should get the id with $campaign->id, after you call the create method. Check if you get some error, maybe you don’t get id because campaign is not created.

    If you are looking for PHP SDK doc it is here: https://github.com/facebook/facebook-php-ads-sdk

    Try this example:

    use FacebookAdsObjectCampaign;
    use FacebookAdsObjectFieldsCampaignFields;
    
    try{
        $campaign  = new Campaign(null, $account->id);
        $campaign->setData(array(
            CampaignFields::NAME => 'My First Campaign',
            CampaignFields::OBJECTIVE => 'LINK_CLICKS',
        ));
        $campaign->validate()->create(array(
            Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED,
        ));
        echo "Campaign ID:" . $campaign->id . "n";
    }
    catch (Exception $e) {
        echo 'Error message: ' .$e->getMessage() ."n" . "<br/>";
        echo 'Error Code: ' .$e->getCode() ."<br/>";
    }
    
    Login or Signup to reply.
  2. The facebook examples are incomplete. Make sure your initiate the Facebook Ad API.

    If the creation was successful the Facebook library assigns the id to the campaign object you called create() on and it can be accessed as follows:

    $campaign->{CampaignFields::ID};
    

    Here is a full example of how to create a campaign:

       // 1. The facebook api has to be initiated before making request
        Api::init("App Id", "App Secret", "User Token");
    
        $campaign = new Campaign(null, 'act_<account id>');
    
        // 2. Populating the campaign object
        $campaign->setData([
            CampaignFields::NAME => 'TEST Campaign',
            CampaignFields::OBJECTIVE => CampaignObjectiveValues::LINK_CLICKS
        ]);
    
        try {
    
            // 3. If the operation is successful the id will be set
            $campaign->create();
    
            // Grabbing the id.
            $facebookCampaignId = $campaign->{CampaignFields::ID};
    
            // Printing the id
            var_dump($facebookCampaignId);
        } catch (RequestException $requestException) {
    
            // Can be null
            $facebookResponse = $requestException->getResponse();
    
            if (!empty($facebookResponse)) {
    
                // Its easier to debug using the response by Facebook.
                var_dump($facebookResponse->getBody());
            }
    
        } catch (Exception $exception) {
            var_dump($exception->getMessage());
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search