0

I am trying to create a Facebook AD via their PHP SDK. The problem lies in the creation of the ad itself. If I comment out the last piece, its creates the adset. When I run this code, I get the error "a Parent ID is required"

try {   

///////////////////////////// CREATE AD SET ////////////////////////////////////
$data = array(
AdSetFields::NAME => $adname,
AdSetFields::BID_TYPE => 'CPC',
AdSetFields::BID_INFO => array(
'CLICKS' => 6,
),
AdSetFields::CAMPAIGN_STATUS => AdSet::STATUS_PAUSED,
AdSetFields::DAILY_BUDGET => 500000,
AdSetFields::CAMPAIGN_GROUP_ID => $campaign_id,
AdSetFields::TARGETING => array(
 'age_min' => 30,
'age_max' => 45,
'page_types' => array(
    'desktopfeed',
  ),
'geo_locations' => array(
  'countries' => array(
    'US',
     ),      
    ),
  ),
);
     $adset = new AdSet(null, $account_id);
    $adset->create($data);

 $adset_id = $adset->id;
echo $adset_id.'-';


//////////////////////CREATE AD IMAGE ///////////////////////////////


$image = new AdImage(null, $account_id);
$image->{AdImageFields::FILENAME} = '../adimages/epsom.jpg';
$image->create();

$creative = new AdCreative(null, $account_id);
$creative->setData(array(
  AdCreativeFields::TITLE => $adtitle,
  AdCreativeFields::BODY => $addesc,
  AdCreativeFields::OBJECT_URL => $tracking_promoted_url,
  AdCreativeFields::IMAGE_HASH => $image->hash,
));
$creative->create();

echo $creative->id.'-';

/////////////////////////////// CREATE AD GROUP ////////////////////////////

$fields = array(array(
  AdGroupFields::CREATIVE => 
    array('creative_id' => $creative->id),
    AdGroupFields::ADGROUP_STATUS => AdGroup::STATUS_PAUSED,
  AdGroupFields::NAME => 'My First AdGroup',
  AdGroupFields::CAMPAIGN_ID => $adset_id,
));
$ad = new AdGroup();
$ad->create($fields);

echo 'AdGroup ID:' . $adgroup->id . "-";


} catch (RequestException $e) {
     echo 'Caught Exception: '.$e->getMessage().PHP_EOL
   .'Code: '.$e->getCode().PHP_EOL
.'HTTP status Code: '.$e->getHttpStatusCode().PHP_EOL;
}
tytyguy
  • 340
  • 1
  • 4
  • 15
  • Is that error from somewhere in that SDK or from the API itself? Are you sure you're setting the Ad Set ID correctly on the Ad before trying to create it? – Igy May 19 '15 at 21:04
  • that is what the api returns. i echo the adset id to make sure it was creating it, and it seemed to output it. below is the error Fatal error: Uncaught exception 'Exception' with message 'A parent ID is required.' in /.../src/FacebookAds/Object/AbstractCrudObject.php:125 Stack trace: – tytyguy May 20 '15 at 18:28
  • Can you share more of your code? I wonder if or how you're setting up the Ads SDK. For instance, are you calling Api::init($app_id, $app_secret, $access_token); and $api = Api::instance(); per the docs? https://github.com/facebook/facebook-php-ads-sdk – bjeavons May 21 '15 at 18:50
  • yes, i am intializing the api... like i said, when i remove the last piece of the code (the adgroup) then it creates the adset with no ad. – tytyguy May 22 '15 at 17:50
  • 1
    I don't see anywhere after you do `$ad = new AdGroup();` where you set the ad set ID- are you missing that? (`campaign_id` is a required field (and is the Ad Set ID, but named `campaign_id` for legacy reasons)): https://developers.facebook.com/docs/marketing-api/adgroup/v2.3#syncadgroupcreation – Igy May 22 '15 at 21:25

2 Answers2

0

You're missing the parent ID parameter when you create the adgroup. The 2nd parameter should be the account ID you want to create this adgroup under:

$adgroup = new Adgroup(null, $accountId);

If you copied this from a doc, let me know and we can update.

Paul Bain
  • 4,364
  • 1
  • 16
  • 30
0

You may reference to the latest doc: https://developers.facebook.com/docs/marketing-api/reference/ad-campaign

From there, you will need to specify the ad account id(AD_ACCOUNT_ID) and ad campaign id(CAMPAIGN_GROUP_ID)