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
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:
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:
Here is a full example of how to create a campaign: