2016-09-12 10 views
1

PHP SDKで動作するMarketing APIのサンプルを取得しようとしています。私はキャンペーンを作成し、ターゲティングを設定し、画像をアップロードし、AdSetを作成しました。しかし、AdCreativeと広告自体に問題があります。AdCreativeのFacebook Marketing API(php SDK) '無効なパラメータ'

コードは「無効なパラメータ」で壊れていますが、無効なパラメータはどれかわかりません。私は3日間デバッグをしていましたが、私は立ち往生しています。ここで私が持っているコードです:

は、キャンペーンを作成します。

$campaign = new Campaign(null, 'act_xxxxxxx'); 
$campaign->setData(array(
    CampaignFields::NAME => '#My Campaign#', 
    CampaignFields::OBJECTIVE => CampaignObjectiveValues::LINK_CLICKS, 
)); 

$campaign->create(array(
    Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED, 
)); 

は、ターゲットとするユーザーを作成します。

$targeting = new Targeting(); 
$targeting->{TargetingFields::GEO_LOCATIONS} = 
    array(
    'location_types' => array('recent'), 
    'custom_locations' => array(
          array(
           'latitude'=> 'xx.xxxxxxx', 
           'longitude'=> 'xx.xxxxxxx', 
            'radius'=> 2, 
            'distance_unit'=> 'kilometer')), 
); 

はADSETを作成します。

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

$adset = new AdSet(null, 'act_xxxxxxxx'); 
$adset->setData(array(
    AdSetFields::NAME => '#My Adset#', 
    AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::LINK_CLICKS, 
    AdSetFields::BILLING_EVENT => AdSetBillingEventValues::LINK_CLICKS, 
    AdSetFields::BID_AMOUNT => 2, 
    AdSetFields::DAILY_BUDGET => 500, 
    AdSetFields::CAMPAIGN_ID => $campaign->id, 
    AdSetFields::TARGETING => $targeting, 
    AdSetFields::START_TIME => $start_time, 
    AdSetFields::END_TIME => $end_time, 
)); 
$adset->create(array(
    AdSet::STATUS_PARAM_NAME => AdSet::STATUS_PAUSED, 
)); 

は、画像をアップロードする:

$image = new AdImage(null, 'act_xxxxxxxx'); 
$image->{AdImageFields::FILENAME} = 'fb-ad-images/xxxxx.jpeg'; 

$image->create(); 
$img_hash=$image->{AdImageFields::HASH}.PHP_EOL; 

AdCreativeを作成します。

$link_data = new AdCreativeLinkData(); 
$link_data->setData(array(
    AdCreativeLinkDataFields::MESSAGE => 'try it out', 
    AdCreativeLinkDataFields::LINK => 'http://example.com', 
    AdCreativeLinkDataFields::CAPTION => 'My caption', 
    AdCreativeLinkDataFields::IMAGE_HASH => $img_hash, 
)); 

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

$creative = new AdCreative(null, 'act_xxxxxxx'); 

$creative->setData(array(
    AdCreativeFields::NAME => 'Sample Creative', 
    AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec, 
)); 

$creative->create(); 

は、広告を作成します。

$data = array(
    AdFields::NAME => 'My Ad', 
    AdFields::ADSET_ID => $adset->id, 
    AdFields::CREATIVE => array(
    'creative_id' => $creative->id, 
), 
); 

$ad = new Ad(null, 'act_xxxxxx'); 
$ad->setData($data); 
$ad->create(array(
    Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED, 
)); 

私はパラメータの検証との互換性をチェックしましたし、すべてがOKと思われます。エラーが

Fatal error: Uncaught exception 'FacebookAds\Http\Exception\AuthorizationException' with message 'Invalid parameter'

で誰でも助けてください、私は本当にこだわっています。ありがとう。

+0

まだ解決策はありますか? – jan

答えて

0

コードを見ると、ターゲティングでlongitudeとlatitudeを指定していないようです。 xx.xxxはテキストで置き換える必要があります。私が考え出したのは、コードのロギングを有効にし、エラーを示す実際のAPI呼び出しを取得することです。更新されたコードスニペットを見てください。

<?php 
$access_token = '<ACCESS_TOKEN>'; 
$app_id = <APP_ID>; 
$app_secret = '<APP_SECRET>'; 
// should begin with "act_" (eg: $account_id = 'act_1234567890';) 
$account_id = 'act_<Ad_ACCOUNT_ID>'; 
// Configurations - End 

if (is_null($access_token) || is_null($app_id) || is_null($app_secret)) { 
    throw new \Exception(
    'You must set your access token, app id and app secret before executing' 
); 
} 

if (is_null($account_id)) { 
    throw new \Exception(
    'You must set your account id before executing'); 
} 

define('SDK_DIR', __DIR__ . '/..'); // Path to the SDK directory 
$loader = include SDK_DIR.'/vendor/autoload.php'; 

use FacebookAds\Api; 
use FacebookAds\Logger\CurlLogger; 
Api::init($app_id, $app_secret, $access_token); 

// Create the CurlLogger 
$logger = new CurlLogger(); 
// To write to a file pass in a file handler 
// $logger = new CurlLogger(fopen('test','w')); 
// Attach the logger to the Api instance 
Api::instance()->setLogger($logger); 

use FacebookAds\Object\Campaign; 
use FacebookAds\Object\Fields\CampaignFields; 
use FacebookAds\Object\Values\CampaignObjectiveValues; 
use FacebookAds\Object\Targeting; 
use FacebookAds\Object\Fields\TargetingFields; 
use FacebookAds\Object\Fields\AdSetFields; 
use FacebookAds\Object\AdSet; 
use FacebookAds\Object\Values\AdSetOptimizationGoalValues; 
use FacebookAds\Object\Values\AdSetBillingEventValues; 

$campaign = new Campaign(null, $account_id); 
$campaign->setData(array(
    CampaignFields::NAME => '#My Campaign#', 
    CampaignFields::OBJECTIVE => CampaignObjectiveValues::LINK_CLICKS, 
)); 

$campaign->create(array(
    Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED, 
)); 

$targeting = new Targeting(); 
$targeting->{TargetingFields::GEO_LOCATIONS} = 
    array(
    'location_types' => array('recent'), 
    'custom_locations' => array(
          array(
           'latitude'=> '47.6062', 
           'longitude'=> '122.3321', 
            'radius'=> 2, 
            'distance_unit'=> 'kilometer')), 
); 

$start_time = (new \DateTime("+1 day"))->format(DateTime::ISO8601); 
$end_time = (new \DateTime("+2 day"))->format(DateTime::ISO8601); 
echo $campaign->id."\n"; 
$adset = new AdSet(null, $account_id); 
$adset->setData(array(
    AdSetFields::NAME => '#My Adset#', 
    AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::IMPRESSIONS, 
    AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS, 
    AdSetFields::BID_AMOUNT => 2, 
    AdSetFields::DAILY_BUDGET => 500, 
    AdSetFields::CAMPAIGN_ID => $campaign->id, 
    AdSetFields::TARGETING => $targeting, 
    AdSetFields::START_TIME => $start_time, 
    AdSetFields::END_TIME => $end_time, 
)); 
$adset->create(array(
    AdSet::STATUS_PARAM_NAME => AdSet::STATUS_PAUSED, 
)); 
関連する問題