创建Facebook Campaign并获取Campaign ID - Marketing API

时间:2017-11-02 06:53:13

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

这就是facebook告诉人们根据文档制作广告系列的方式: https://developers.facebook.com/docs/marketing-api/buying-api/

但是,新的Campaign构造函数中的null字段参数是什么?我放在那里的任何内容都会成为广告系列的ID,但却是无效的。 你如何获得广告系列的ID?

当我运行此代码时,我没有收到任何错误,但我的广告管理器上没有创建任何内容,当我回显$ campaign-> id时,它只是空白,因为id为null。但是,如果我print_r($ campaign)它返回一个包含许多字段但没有ID字段的大对象。

Facebook代码:

use FacebookAds\Object\Campaign;
use FacebookAds\Object\Fields\CampaignFields;
use FacebookAds\Object\Values\CampaignObjectiveValues;

$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,
));

这是facebook文档: https://developers.facebook.com/docs/marketing-api/using-the-api

然而,它并不是非常有用,因为它只是指向一个不提供相关信息的快速入门指南。

2 个答案:

答案 0 :(得分:2)

facebook的例子不完整。确保您启动Facebook广告API。

如果创建成功,Facebook库会将id分配给您调用create()的广告系列对象,并且可以按如下方式访问:

$campaign->{CampaignFields::ID};

以下是如何制作广告系列的完整示例:

   // 1. The facebook api has to be initiated before making request
    Api::init("App Id", "App Secret", "User Token");

    $campaign = new Campaign(null, 'act_<account id>');

    // 2. Populating the campaign object
    $campaign->setData([
        CampaignFields::NAME => 'TEST Campaign',
        CampaignFields::OBJECTIVE => CampaignObjectiveValues::LINK_CLICKS
    ]);

    try {

        // 3. If the operation is successful the id will be set
        $campaign->create();

        // Grabbing the id.
        $facebookCampaignId = $campaign->{CampaignFields::ID};

        // Printing the id
        var_dump($facebookCampaignId);
    } catch (RequestException $requestException) {

        // Can be null
        $facebookResponse = $requestException->getResponse();

        if (!empty($facebookResponse)) {

            // Its easier to debug using the response by Facebook.
            var_dump($facebookResponse->getBody());
        }

    } catch (Exception $exception) {
        var_dump($exception->getMessage());
    }

Full example of creating a Facebook Campaign with flight dates and a spent cap.

答案 1 :(得分:0)

在调用create方法后,您应该使用$ campaign-&gt; id获取id。检查您是否收到某些错误,也许您没有获得ID,因为未创建广告系列。

如果您正在寻找PHP SDK文档,请访问:docker-compose issue 4711

试试这个例子:

use FacebookAds\Object\Campaign;
use FacebookAds\Object\Fields\CampaignFields;

try{
    $campaign  = new Campaign(null, $account->id);
    $campaign->setData(array(
        CampaignFields::NAME => 'My First Campaign',
        CampaignFields::OBJECTIVE => 'LINK_CLICKS',
    ));
    $campaign->validate()->create(array(
        Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED,
    ));
    echo "Campaign ID:" . $campaign->id . "\n";
}
catch (Exception $e) {
    echo 'Error message: ' .$e->getMessage() ."\n" . "<br/>";
    echo 'Error Code: ' .$e->getCode() ."<br/>";
}