Facebook营销API - 广告创意无法正常生成

时间:2017-12-08 19:38:14

标签: facebook facebook-php-sdk facebook-marketing-api

我正在制作一个脚本,通过API生成Facebook广告。由于某些原因,广告素材的变量未添加到广告中。主要问题是标题和正文没有被添加。以下是我生成广告素材的代码:

    try{
    $creative = new AdCreative(null, $accountId);
    $creative->setData(array(
        AdCreativeFields::NAME => $ad_info['Ad Name'],
        AdCreativeFields::TITLE => $ad_info['Title'],
        AdCreativeFields::BODY => $ad_info['Body'],
        AdCreativeFields::IMAGE_HASH => $image->hash,
        //AdCreativeFields::OBJECT_URL => $ad_info['Preview Link'],
        AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
        AdCreativeFields::URL_TAGS => $ad_info['URL Tags'],
    ));

    $creative->create();
    echo 'Creative ID: '.$creative->id . "\n";
}
catch (Exception $e) {
    echo '<pre>creative';print_r($e);exit;
    if($e->getMessage()){
        echo 'Error message: ' .$e->getMessage() ."\n" . "<br/>";
    }
    //echo 'Error Code: ' .$e->getCode() ."<br/>";
}

将广告素材添加到广告的代码:

        $ad = new Ad(null, $accountId);
    $ad->setData(array(
        AdFields::CREATIVE =>
            array('creative_id' => $creative->id),
        AdFields::NAME => $ad_info['Ad Name'],
        AdFields::ADSET_ID => $adsetId,
        AdFields::STATUS => 'ACTIVE',
        AdFields::TRACKING_SPECS => array('action.type' => $ad_info['action type'], 'fb_pixel' => $fb_pixel),
    ));

    $ad->create();

它不会丢失错误,其他变量(例如广告名称,状态等)似乎都会正确添加到广告中。据我所知,根据Facebook的API文档,上述所有内容都是正确的。我甚至尝试在正在生成的广告素材和将其添加到广告之间投放睡眠功能。问题是什么?

1 个答案:

答案 0 :(得分:0)

我弄清楚问题是什么。如果广告广告素材对象是链接广告,则该广告广告素材对象不会应用于此广告。为了显示标题和正文文本,您必须将它们添加到AdCreativeLinkData中,如下所示:

        $link_data->setData(array(
          AdCreativeLinkDataFields::DESCRIPTION => $ad_info['Link Description'],
          AdCreativeLinkDataFields::LINK => $ad_info['Link'],
          AdCreativeLinkDataFields::NAME => $ad_info['Title'],
          AdCreativeLinkDataFields::MESSAGE => $ad_info['Body'],
          AdCreativeLinkDataFields::IMAGE_HASH => $image->hash,
          AdCreativeLinkDataFields::CALL_TO_ACTION => array(
            'type' => AdCreativeCallToActionTypeValues::SIGN_UP,
            'value' => array(
              'link' => $ad_info['Link'],
              'link_caption' => $ad_info['Link'],
            ),
          ),
        ));

一旦我将NAME和MESSAGE变量添加到此对象,广告的标题和正文就会完美呈现。