Facebook广告API错误

时间:2017-08-30 19:22:43

标签: php facebook facebook-ads-api

使用FB Ads PHP SDK创建Creative时出现错误

$ parent_id作为构造函数的参数已被弃用,请尽量不要在新代码中使用它。

代码在2.9和2.10更新之前正在运行。

我用来创建广告素材的代码是:

    $link_data = new AdCreativeLinkData();
$link_data->setData(array(
  AdCreativeLinkDataFields::MESSAGE => 'Product Description',
  AdCreativeLinkDataFields::LINK => $url_of_website,
  AdCreativeLinkDataFields::IMAGE_HASH => $image->hash,
  AdCreativeLinkDataFields::DESCRIPTION => 'Link Description',
  AdCreativeLinkDataFields::CALL_TO_ACTION => array(
    'type' => AdCreativeCallToActionTypeValues::LEARN_MORE,
    'value' => array(
      'link_title' => 'View Similar Products Now!',
      'lead_gen_form_id' =>  $form_id,
    ),
  ),
));

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

$creative = new AdCreative(null, $account_id);
$creative->setData(array(
  AdCreativeFields::NAME => $nm,
  AdCreativeFields::OBJECT_STORY_SPEC => $story,
  AdCreativeFields::URL_TAGS => 'product=' . $p_id,
));

$creative->create();

我在这个陈述中没有看到任何父ID。请帮忙

3 个答案:

答案 0 :(得分:2)

$parent_id is deprecated

The issue was reported on facebook github with issue# 314

Response from Facebook Developer

"We are depreciating creation with parent_id. We are seeing multiple endpoints that can create the same type of object. We do not have good ways to decide which one we should use if you are creating new object with parent_id. Moving forward, please instantiate the parent object with the parent_id and call create_XXX function to create the object you want."

Sample Code:

def get_unixtime(dt64):
    return dt64.astype('datetime64[s]').astype('int')
Hope this helps.

答案 1 :(得分:0)

使用setParentId($parrent_id)

示例代码:

$product_catalog = new ProductCatalog();
$product_catalog->setParentId($parrent_id);
$product_catalog->setData(
            [
                ProductCatalogFields::NAME => "Testjon Autojon Catalog",
                ProductCatalogFields::VERTICAL => "vehicles",
            ]
);
$product_catalog->create();

答案 2 :(得分:0)

我发现即使 here 中提到的已接受的答案,即 $parent_id 的使用已被弃用,但共享的示例代码仍然显示了旧的方法。

在该示例中,传递给 AdCreative() 的第二个参数仍然是 $parent_id

$creative = new AdCreative(null, 'act_<AD_ACCOUNT_ID>');

为了清楚起见,下面提到的是 \FacebookAds\Object\AbstractCrudObject 的构造函数的方法签名,它是 \FacebookAds\Object\AdCreative 的扩展源,您会在那里看到弃用通知。

  /**
   * @deprecated deprecate constructor with null and parent_id
   * @param string $id Optional (do not set for new objects)
   * @param string $parent_id Optional, needed for creating new objects.
   * @param Api $api The Api instance this object should use to make calls
   */
  public function __construct($id = null, $parent_id = null, Api $api = null) {
    parent::__construct();
    //...
  }

说至于新方法,这是现在应该做的方式:)

require __DIR__ . '/vendor/autoload.php';

use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\AdAccount;

$access_token = '<ACCESS_TOKEN>';
$app_secret = '<APP_SECRET>';
$app_id = '<APP_ID>';
$id = '<AD_ACCOUNT_ID>';

$api = Api::init($app_id, $app_secret, $access_token);
$api->setLogger(new CurlLogger());

$fields = array();
$params = array(
    'name' => 'Sample Creative',
    'object_story_spec' => ['page_id' => '<pageID>',
        'video_data' => ['IMAGE_URL' => '<imageURL>',
            'video_id' => '<videoID>',
            'call_to_action' => ['type' => 'LIKE_PAGE',
                'value' => ['page' => '<pageID>']
            ]
        ]
    ],
);
$adCreative = (new AdAccount($id))->createAdCreative($fields, $params);

echo json_encode($adCreative->exportAllData(), JSON_PRETTY_PRINT);

我找到了这个例子 here。请注意,尽管本文档的标题是“创建广告视频创意”,但它实际上展示了如何创建广告创意。 Facebook Marketing API 参考中有许多不一致之处,就是这种情况:)