skip to Main Content

I am getting an error when trying to create an ad on Facebook through the PHP API.

Exception in AbstractCrudObject.php line 113: A parent ID is required.

The thing is, I copied the code from https://developers.facebook.com/docs/marketing-api/guides/lead-ads/create/v2.8 and just modified it to match what I need. I am confused as to what is missing, since I can’t find something on this thus far through the documentation. Anyone else have the same issue or find out how to fix it?

Here is my code:

$link_data = new AdCreativeLinkData();
$link_data->setData([
    AdCreativeLinkDataFields::LINK => $creativeInfo->link,
    AdCreativeLinkDataFields::MESSAGE => $creativeInfo->message,
    AdCreativeLinkDataFields::IMAGE_HASH => $image->{AdImageFields::HASH}.PHP_EOL,
    AdCreativeLinkDataFields::CAPTION => $creativeInfo->caption,
    AdCreativeLinkDataFields::DESCRIPTION => $creativeInfo->description,
    AdCreativeLinkDataFields::CALL_TO_ACTION => [
        'type' => $creativeInfo->cta,
        'value' => [
            'lead_gen_form_id' => $creativeInfo->leadform,
        ],
    ],
]);

$story = new AdCreativeObjectStorySpec();
$story->setData([
    AdCreativeObjectStorySpecFields::PAGE_ID => $creativeInfo->page_id,
    AdCreativeObjectStorySpecFields::LINK_DATA => $link_data,
]);

$creative = new AdCreative();
$creative->setData([
    AdCreativeFields::OBJECT_STORY_SPEC => $story,
]);
$creative->create();

2

Answers


  1. When you instantiate an AdCreative object you have to provide the ad account ID the Creative is on, like this:

    $creative = new AdCreative(null, 'act_<AD_ACCOUNT_ID>'); //i.e. act_12424141412
    
    Login or Signup to reply.
  2. $adCreative = (new AdCreative(null))
      ->setParentId("act_{$this->pageId}");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search