2016-09-09 25 views
0

無効なパラメータのエラーが表示されますが、無効なパラメータはどれかわかりません。エラーは次のとおりです。Facebook marketing ap:cant create広告

Fatal error: Uncaught exception 'FacebookAds\Http\Exception\AuthorizationException' with message 'Invalid parameter' in C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Http\Exception\RequestException.php:140 Stack trace: #0 C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Http\Client.php(215): FacebookAds\Http\Exception\RequestException::create(Object(FacebookAds\Http\Response))

1 C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Http\Request.php(282):

FacebookAds\Http\Client->sendRequest(Object(FacebookAds\Http\Request))

2 C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Api.php(151):

FacebookAds\Http\Request->execute() #3 C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Api.php(193): FacebookAds\Api->executeRequest(Object(FacebookAds\Http\Request)) #4 C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Object\AbstractCrudObject.php(208): FacebookAds\Api->call('/act_XXXXXXXX...', 'POST', Array) #5 C:\A in C:\AppServ\www\marketing\vendor\facebook\php-ads-sdk\src\FacebookAds\Http\Exception\RequestException.php on line 140

これは私のコードであり、エラーが広告まで、それは私に、このエラーを示し、すべてが細かい作成された最後のオブジェクト$広告=新しい広告にあるようです。

<?php 
//date_default_timezone_set('America/Lima'); 
//require_once('vendor/autoload.php'); 


//$campaign_id = '6053849657204'; 

// Configurations 
$access_token = 'MYTOKEN'; 
$app_id = 'MYAPPID'; 
$app_secret = 'MYAPPSECRET'; 

$account_id = 'act_MYACCOUNT'; 
define('SDK_DIR', __DIR__ . ''); // Path to the SDK directory 
$loader = include SDK_DIR.'/vendor/autoload.php'; 
date_default_timezone_set('America/Los_Angeles'); 
// 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'); 
} 
use FacebookAds\Api; 
Api::init($app_id, $app_secret, $access_token); 
/** 
* Step 1 Read the AdAccount (optional) 
*/ 
use FacebookAds\Object\AdAccount; 
use FacebookAds\Object\Fields\AdAccountFields; 
$account = (new AdAccount($account_id))->read(array(
    AdAccountFields::ID, 
    AdAccountFields::NAME, 
    AdAccountFields::ACCOUNT_STATUS, 
)); 
echo "\nUsing this account: "; 
echo $account->id."\n"; 
// Check the account is active 
if($account->{AdAccountFields::ACCOUNT_STATUS} !== 1) { 
    throw new \Exception(
    'This account is not active'); 
} 
/** 
* Step 2 Create the Campaign 
*/ 
use FacebookAds\Object\Campaign; 
use FacebookAds\Object\Fields\CampaignFields; 
use FacebookAds\Object\Values\AdObjectives; 
$campaign = new Campaign(null, $account->id); 
$campaign->setData(array(
    CampaignFields::NAME => 'Noticia 1', 
    CampaignFields::OBJECTIVE => AdObjectives::LINK_CLICKS, 
)); 
$campaign->validate()->create(array(
    Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED, 
)); 
echo "Campaign ID:" . $campaign->id . "\n"; 
/** 
* Step 3 Search Targeting 
*/ 
use FacebookAds\Object\TargetingSearch; 
use FacebookAds\Object\Search\TargetingSearchTypes; 
use FacebookAds\Object\TargetingSpecs; 
use FacebookAds\Object\Fields\TargetingSpecsFields; 
$results = TargetingSearch::search(
    $type = TargetingSearchTypes::INTEREST, 
    $class = null, 
    $query = 'facebook'); 
// we'll take the top result for now 
$target = (count($results)) ? $results->current() : null; 
echo "Using target: ".$target->name."\n"; 
$targeting = new TargetingSpecs(); 
$targeting->{TargetingSpecsFields::GEO_LOCATIONS} 
    = array('countries' => array('PE')); 
/*$targeting->{TargetingSpecsFields::INTERESTS} = array(
    array(
     'id' => $target->id, 
     'name' => $target->name, 
    ), 
);*/ 
/** 
* Step 4 Create the AdSet 
*/ 
use FacebookAds\Object\AdSet; 
use FacebookAds\Object\Fields\AdSetFields; 
use FacebookAds\Object\Values\OptimizationGoals; 
use FacebookAds\Object\Values\BillingEvents; 
$adset = new AdSet(null, $account->id); 
$adset->setData(array(
    AdSetFields::NAME => 'Latam 1', 
    AdSetFields::CAMPAIGN_ID => $campaign->id, 
    AdSetFields::DAILY_BUDGET => '150', 
    AdSetFields::TARGETING => $targeting, 
    AdSetFields::OPTIMIZATION_GOAL => OptimizationGoals::REACH, 
    AdSetFields::BILLING_EVENT => BillingEvents::IMPRESSIONS, 
    AdSetFields::BID_AMOUNT => '1', 
    AdSetFields::START_TIME => 
    (new \DateTime("+1 week"))->format(\DateTime::ISO8601), 
    AdSetFields::END_TIME => 
    (new \DateTime("+2 week"))->format(\DateTime::ISO8601), 
)); 
$adset->validate()->create(array(
    AdSet::STATUS_PARAM_NAME => AdSet::STATUS_ACTIVE, 
)); 
echo 'AdSet ID: '. $adset->id . "\n"; 
/** 
* Step 5 Create an AdImage 
*/ 
use FacebookAds\Object\AdImage; 
use FacebookAds\Object\Fields\AdImageFields; 
$image = new AdImage(null, $account->id); 
$image->{AdImageFields::FILENAME} 
    = dirname(__FILE__).'/image.jpg'; 
$image->create(); 
echo 'Image Hash: '.$image->hash . "\n"; 
/** 
* Step 6 Create an AdCreative 
*/ 


    use FacebookAds\Object\AdCreative; 
use FacebookAds\Object\AdCreativeLinkData; 
use FacebookAds\Object\Fields\AdCreativeLinkDataFields; 
use FacebookAds\Object\AdCreativeObjectStorySpec; 
use FacebookAds\Object\Fields\AdCreativeObjectStorySpecFields; 
use FacebookAds\Object\Fields\AdCreativeFields; 

$link_data = new AdCreativeLinkData(); 
$link_data->setData(array(
    AdCreativeLinkDataFields::MESSAGE => 'MY DESC', 
    AdCreativeLinkDataFields::LINK => 'MY WEB', 
    AdCreativeLinkDataFields::CAPTION => 'My caption', 
    AdCreativeLinkDataFields::IMAGE_HASH => $image->hash, 
)); 

$object_story_spec = new AdCreativeObjectStorySpec(); 
$object_story_spec->setData(array(
    AdCreativeObjectStorySpecFields::PAGE_ID => 'MY PAGE ID', 
    AdCreativeObjectStorySpecFields::LINK_DATA => $link_data, 
)); 

$creative = new AdCreative(null, $account->id); 

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

$creative->create(); 


echo 'Creative ID: '.$creative->id . "\n"; 
/** 
* Step 7 Create an Ad 
*/ 
use FacebookAds\Object\Ad; 
use FacebookAds\Object\Fields\AdFields; 
$datax = array(
    AdFields::NAME => 'My Ad', 
    AdFields::ADSET_ID => $adset->id, 
    AdFields::CREATIVE => array(
    'creative_id' => $creative->id, 
), 
); 

$ad = new Ad(null, $account->id); 
$ad->setData($datax); 
$ad->create(array(
    Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED, 
)); 
echo 'Ad ID:' . $ad->id . "\n"; 

答えて

0

問題は、有効なリンクとページIDを指定する必要があることでした。私はあなたのPHPプロジェクトに上記のコードを追加したら

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

、それは出力にAPI呼び出しのカールバージョンを慰めるだろう、以下のコードのこの部分を追加することによって、コード内のより多くのログを追加することによって、それを考え出しましたSDKによって実行されます。 curlを使用して端末で失敗したAPI呼び出しを行い、デバッグのためにAPIがスローする特定のエラーを取得できます。 (現在、PHP SDKに正確なエラーは例外にまで伝播されません。)

はあなたがここに完全な作業コードスニペットをチェックアウトすることができ、

<?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_<ACCOUNT_ID>'; 
$page_id = 0; // REPLACE THIS WITH VALID PAGE 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); 

/** 
* Step 1 Read the AdAccount (optional) 
*/ 
use FacebookAds\Object\AdAccount; 
use FacebookAds\Object\Fields\AdAccountFields; 
$account = (new AdAccount($account_id))->read(array(
    AdAccountFields::ID, 
    AdAccountFields::NAME, 
    AdAccountFields::ACCOUNT_STATUS, 
)); 
echo "\nUsing this account: "; 
echo $account->id."\n"; 
// Check the account is active 
if($account->{AdAccountFields::ACCOUNT_STATUS} !== 1) { 
    throw new \Exception(
    'This account is not active'); 
} 
/** 
* Step 2 Create the Campaign 
*/ 
use FacebookAds\Object\Campaign; 
use FacebookAds\Object\Fields\CampaignFields; 
use FacebookAds\Object\Values\AdObjectives; 
$campaign = new Campaign(null, $account->id); 
$campaign->setData(array(
    CampaignFields::NAME => 'Noticia 1', 
    CampaignFields::OBJECTIVE => AdObjectives::LINK_CLICKS, 
)); 
$campaign->validate()->create(array(
    Campaign::STATUS_PARAM_NAME => Campaign::STATUS_PAUSED, 
)); 
echo "Campaign ID:" . $campaign->id . "\n"; 
/** 
* Step 3 Search Targeting 
*/ 
use FacebookAds\Object\TargetingSearch; 
use FacebookAds\Object\Search\TargetingSearchTypes; 
use FacebookAds\Object\TargetingSpecs; 
use FacebookAds\Object\Fields\TargetingSpecsFields; 
$results = TargetingSearch::search(
    $type = TargetingSearchTypes::INTEREST, 
    $class = null, 
    $query = 'facebook'); 
// we'll take the top result for now 
$target = (count($results)) ? $results->current() : null; 
echo "Using target: ".$target->name."\n"; 
$targeting = new TargetingSpecs(); 
$targeting->{TargetingSpecsFields::GEO_LOCATIONS} 
    = array('countries' => array('PE')); 
/*$targeting->{TargetingSpecsFields::INTERESTS} = array(
    array(
     'id' => $target->id, 
     'name' => $target->name, 
    ), 
);*/ 
/** 
* Step 4 Create the AdSet 
*/ 
use FacebookAds\Object\AdSet; 
use FacebookAds\Object\Fields\AdSetFields; 
use FacebookAds\Object\Values\OptimizationGoals; 
use FacebookAds\Object\Values\BillingEvents; 
$adset = new AdSet(null, $account->id); 
$adset->setData(array(
    AdSetFields::NAME => 'Latam 1', 
    AdSetFields::CAMPAIGN_ID => $campaign->id, 
    AdSetFields::DAILY_BUDGET => '150', 
    AdSetFields::TARGETING => $targeting, 
    AdSetFields::OPTIMIZATION_GOAL => OptimizationGoals::REACH, 
    AdSetFields::BILLING_EVENT => BillingEvents::IMPRESSIONS, 
    AdSetFields::BID_AMOUNT => '1', 
    AdSetFields::START_TIME => 
    (new \DateTime("+1 week"))->format(\DateTime::ISO8601), 
    AdSetFields::END_TIME => 
    (new \DateTime("+2 week"))->format(\DateTime::ISO8601), 
)); 
$adset->validate()->create(array(
    AdSet::STATUS_PARAM_NAME => AdSet::STATUS_ACTIVE, 
)); 
echo 'AdSet ID: '. $adset->id . "\n"; 
/** 
* Step 5 Create an AdImage 
*/ 
use FacebookAds\Object\AdImage; 
use FacebookAds\Object\Fields\AdImageFields; 
$image = new AdImage(null, $account->id); 
$image->{AdImageFields::FILENAME} 
    = dirname(__FILE__).'/image.jpg'; 
$image->create(); 
echo 'Image Hash: '.$image->hash . "\n"; 
/** 
* Step 6 Create an AdCreative 
*/ 


    use FacebookAds\Object\AdCreative; 
use FacebookAds\Object\AdCreativeLinkData; 
use FacebookAds\Object\Fields\AdCreativeLinkDataFields; 
use FacebookAds\Object\AdCreativeObjectStorySpec; 
use FacebookAds\Object\Fields\AdCreativeObjectStorySpecFields; 
use FacebookAds\Object\Fields\AdCreativeFields; 

$link_data = new AdCreativeLinkData(); 
$link_data->setData(array(
    AdCreativeLinkDataFields::MESSAGE => 'MY DESC', 
    AdCreativeLinkDataFields::LINK => 'www.google.com', 
    AdCreativeLinkDataFields::CAPTION => 'My caption', 
    AdCreativeLinkDataFields::IMAGE_HASH => $image->hash, 
)); 

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

$creative = new AdCreative(null, $account->id); 

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

$creative->create(); 


echo 'Creative ID: '.$creative->id . "\n"; 
/** 
* Step 7 Create an Ad 
*/ 
use FacebookAds\Object\Ad; 
use FacebookAds\Object\Fields\AdFields; 
$datax = array(
    AdFields::NAME => 'My Ad', 
    AdFields::ADSET_ID => $adset->id, 
    AdFields::CREATIVE => array(
    'creative_id' => $creative->id, 
), 
); 

$ad = new Ad(null, $account->id); 
$ad->setData($datax); 
$ad->create(array(
    Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED, 
)); 
echo 'Ad ID:' . $ad->id . "\n"; 
関連する問題