无法使用Facebook Business SDK创建广告集

时间:2019-03-30 18:39:36

标签: php laravel-5.7 facebook-marketing-api facebook-business-sdk

我们正在使用Laravel 5.7,PHP 7.1和XAMPP 3.2.2 我们希望使用PHP Business SDK和Marketing API创建Facebook Campaign。

通过以下源代码成功创建了广告系列

/* Start Campaign*/
$fields = array(
);
$params = array(
    'name' => $request->campaign_name,
    'objective' => 'LINK_CLICKS',
    'status' => 'PAUSED',
);
$campaignData = ((new AdAccount($account_id))->createCampaign(
    $fields,
    $params
)->exportAllData());
/* End Campaign*/

这,我尝试编写AdSet的源代码,但出现以下错误 “不建议将$ parent_id作为构造函数的参数使用,请尝试不要在新代码中使用它。” 在下一行

$adset = new AdSet(null, $account_id);

下面您将找到我创建AdSet的所有源代码

$start_time = (new DateTime("+1 week"))->format(DateTime::ISO8601);
$end_time = (new DateTime("+2 week"))->format(DateTime::ISO8601);

$adset = new AdSet(null,$account_id);
$adset->setData(array(
        AdSetFields::NAME => 'My Ad Set',
        AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::REACH,
        AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS,
        AdSetFields::BID_AMOUNT => 2,
        AdSetFields::DAILY_BUDGET => 1000,
        AdSetFields::CAMPAIGN_ID => $campaignData['id'],
  AdSetFields::TARGETING => $targeting,
  AdSetFields::START_TIME => $start_time,
  AdSetFields::END_TIME => $end_time,
));
$adset->create(array(
    AdSet::STATUS_PARAM_NAME => AdSet::STATUS_PAUSED,
));

为解决我遇到的问题,我修改了以下内容

$adset = new AdSet(null, $account_id);

$adset = new AdSet($account_id);

下面是我的新源代码。

$adset = new AdSet($account_id);
$adset->setData(array(
        AdSetFields::NAME => 'My Ad Set',
        AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::REACH,
        AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS,
        AdSetFields::BID_AMOUNT => 2,
        AdSetFields::DAILY_BUDGET => 1000,
        AdSetFields::CAMPAIGN_ID => $campaignData['id'],
  AdSetFields::TARGETING => $targeting,
  AdSetFields::START_TIME => $start_time,
  AdSetFields::END_TIME => $end_time,
));
$adset->create(array(
    AdSet::STATUS_PARAM_NAME => AdSet::STATUS_PAUSED,
));

现在我在下一行收到错误“对象已经有ID”

$adset->create(array(

您能帮我找出问题的根源以及在哪里出错吗?

1 个答案:

答案 0 :(得分:0)

您收到“对象已具有ID”错误,因为 构造函数是广告集ID,因此save方法立即失败。

“ $ parent_id作为构造函数的参数已被弃用”只是最新sdk中的警告,因此出于演示目的,您可以忽略它。

我建议尝试在没有任何目标的情况下创建广告集并致电validate。还可以捕获请求异常并转储正文以获取有关请求失败原因的详细说明。

use FacebookAds\Http\Exception\RequestException;

$adset = new AdSet(null, 'act_xxx');
$adset->setData([
        AdSetFields::NAME => 'Demo Ad Set',
        AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::REACH,
        AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS,
        AdSetFields::BID_AMOUNT => 2,
        AdSetFields::DAILY_BUDGET => 1000,
        AdSetFields::CAMPAIGN_ID => $facebookCampaignId
    ]);

    try {
        $adset->validate([AdSet::STATUS_PARAM_NAME => AdSet::STATUS_ACTIVE]);
    } catch (RequestException $e) {
        // Super helpful debugging information
        $response = json_decode($e->getResponse()->getBody(), true);
        var_dump($response);
    }

Here is a link to a working example.