使用Facebook Marketing API Adset具有空ID

时间:2017-04-28 12:49:03

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

使用PHP SDK试图制作广告,当我去创建广告时,它说adset_id字段丢失了。当我dd()$ adset变量时,id为null。这是代码和数据转储:

public function createCampaign(Request $request){
 $campaign = new Campaign(null, env('FACEBOOK_ACCOUNT_ID'));
 $campaign->setData([
 CampaignFields::NAME => "Treat Me At Home Campaign For {$request-
>user()->name}",
CampaignFields::OBJECTIVE => CampaignObjectiveValues::LINK_CLICKS,
]);

$campaign->create([
 Campaign::STATUS_PARAM_NAME => Campaign::STATUS_ACTIVE,
]);
$request->user()->campaign_id = $campaign->id;
$request->user()->save();
$targeting = new Targeting();

$targeting->{TargetingFields::GEO_LOCATIONS} =
 array(
  'zips' => array("{'key':'US:{$request->zip}'}")
  );
 $start_time = Carbon::now()->toIso8601String();
 $end_time = Carbon::now()->addMonth()->toIso8601String();

 $adset = new AdSet(null, env('FACEBOOK_ACCOUNT_ID'));
 $adset->setData(array(
  AdSetFields::NAME => "{$request->user()->name} Ad Set",
  AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS,
  AdSetFields::OPTIMIZATION_GOAL => 
  AdSetOptimizationGoalValues::REACH,
  'effective_status' => AdSet::STATUS_PAUSED,
  AdSetFields::BID_AMOUNT => 2,
  AdSetFields::DAILY_BUDGET => round(($request->monthly_amount * 
  .9)/30),
  AdSetFields::CAMPAIGN_ID => $request->user()->campaign_id,
  AdSetFields::TARGETING => $targeting,
  AdSetFields::START_TIME => $start_time,
  AdSetFields::END_TIME => $end_time,
   ));

 $image = new AdImage(null, env('FACEBOOK_ACCOUNT_ID'));
 $image->{AdImageFields::FILENAME} = public_path("/users/{$request-
>user()->id}/profile_pic.jpg");

$image->create();

$link_data = new AdCreativeLinkData();
$link_data->setData(array(
  AdCreativeLinkDataFields::MESSAGE => $request->message,
  AdCreativeLinkDataFields::LINK => 
"https://treatmeathome.online/providers/{$request->user()->username}",
  AdCreativeLinkDataFields::CAPTION => 
 "https://treatmeathome.online/providers/{$request->user()-
>username}",
  AdCreativeLinkDataFields::IMAGE_HASH => $image->
{AdImageFields::HASH},
));

$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
  AdCreativeObjectStorySpecFields::PAGE_ID => '449735951890448',
  AdCreativeObjectStorySpecFields::LINK_DATA => $link_data,
));
$creative = new AdCreative(null, env('FACEBOOK_ACCOUNT_ID'));

$creative->setData(array(
  AdCreativeFields::NAME => "Creative for {$request->user()->name}",
  AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));

$creative->create();
$data = array(
  AdFields::NAME => "Ad for {$request->user()->name}",
  AdFields::ADSET_ID => $adset->id,
  AdFields::CREATIVE => array(
    'creative_id' => $creative->id,
  ),
  );
  dd($data);

$ad = new Ad(null, env('FACEBOOK_ACCOUNT_ID'));
$ad->setData($data);
$ad->create(array(
  Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED,
));
dd($ad);
return redirect('home');
}





array:3 [▼
"name" => "Ad for Jyrone Parker"
"adset_id" => null
"creative" => array:1 [▼
"creative_id" => "6076404535842"
]
]

那为什么要这样做呢?非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

我的首要嫌疑人是env('FACEBOOK_ACCOUNT_ID')。请确保它采用'act_12345678'格式。