Facebook广告创作中的问题

时间:2016-07-31 12:49:15

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

以下是我创建广告的代码,我在第7步遇到了致命错误:

public function add_ads($account_id,$campaign_id){
    /**
     * Step 3 Search Targeting
     */
    $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";
    print_r($target);

    $targeting = new TargetingSpecs();
    $targeting->{TargetingSpecsFields::GEO_LOCATIONS}
      = array('countries' => array('GB'));
    $targeting->{TargetingSpecsFields::INTERESTS} = array(
        array(
            'id' => $target->id,
            'name' => $target->name,
        ),
    );
    print_r($targeting->getData());
    /**
     * Step 4 Create the AdSet
     */
    $adset = new AdSet(null, "act_".$account_id);
    var_dump($account_id);
    //var_dump(expression)
    $adset->setData(array(
        AdSetFields::ACCOUNT_ID => "act_".$account_id,
        AdSetFields::NAME => 'My First AdSet',
        AdFields::BID_TYPE => 'CPC',
        AdSetFields::BID_INFO => array('CLICKS' => 500),
        AdSetFields::CAMPAIGN_ID => $campaign_id,
        AdSetFields::TARGETING => $targeting,
        AdSetFields::OPTIMIZATION_GOAL => AdSetOptimizationGoalValues::REACH,
        AdSetFields::BILLING_EVENT => AdSetBillingEventValues::IMPRESSIONS,
        AdSetFields::BID_AMOUNT => 2,
        AdSetFields::DAILY_BUDGET => 4000,
        AdSetFields::START_TIME =>(new \DateTime("+1 week"))->format(\DateTime::ISO8601),
        AdSetFields::END_TIME =>(new \DateTime("+2 week"))->format(\DateTime::ISO8601),
    ));
    print_r($adset->getData());
    $adset->validate()->create(array(
      AdSet::STATUS_PARAM_NAME => AdSet::STATUS_PAUSED,
    ));

    echo 'AdSet  ID: '. $adset->id . "\n";

    /**
     * Step 5 Create an AdImage
     */

    $image = new AdImage(null, "act_".$account_id);
    $image->{AdImageFields::FILENAME}
      = __DIR__.'/avatar3.jpg';
      print_r($image);
    $image->create();
    echo 'Image Hash: '.$image->hash . "\n";

    /**
     * Step 6 Create an AdCreative
     */


    $creative = new AdCreative(null, "act_".$account_id);
    $creative->setData(array(
      AdCreativeFields::NAME => 'Sample Creative',
      AdCreativeFields::TITLE => 'Welcome to the Jungle',
      AdCreativeFields::BODY => 'We\'ve got fun \'n\' games',
      AdCreativeFields::IMAGE_HASH => $image->hash,
      AdCreativeFields::OBJECT_URL => 'http://www.example.com/',
    ));

    $creative->create();
    echo 'Creative ID: '.$creative->id . "\n";

    /**
     * Step 7 Create an Ad
     */
    $ad = new Ad(null, "act_".$account_id);

   $ad->setData(array(
      AdFields::CREATIVE =>
        array('creative_id' => $creative->id),
      AdFields::NAME => 'My First Ad',
      AdFields::ADSET_ID => $adset->id,
    ));
    print_r($ad);
 $ad->create(array(Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED));
    echo 'Ad ID:' . $ad->id . "\n";
    return $ad;
}

这是我得到的错误:

  

致命错误:未捕获的FacebookAds \ Http \ Exception \ AuthorizationException:/ var / www / html / codeigniter_metronic-master / application / libraries / facebook / vendor / facebook / php-ads-sdk / src / FacebookAds / Http中的参数无效/Exception/RequestException.php:140

     

堆栈跟踪:

     

#0 /var/www/html/codeigniter_metronic-master/application/libraries/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Client.php(215 ):FacebookAds \ Http \ Exception \ RequestException :: create(Object(FacebookAds \ Http \ Response))

     

#1 /var/www/html/codeigniter_metronic-master/application/libraries/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Request.php(282 ):FacebookAds \ Http \ Client-> sendRequest(对象(FacebookAds \ Http \ Request))

     

#2 /var/www/html/codeigniter_metronic-master/application/libraries/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(151): FacebookAds \ HTTP \请求 - >执行()

     

#3 /var/www/html/codeigniter_metronic-master/application/libraries/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(193): Facebook广告\ Api-&gt; executeRe位于第140行的/var/www/html/codeigniter_metronic-master/application/libraries/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Exception/RequestException.php < / p>

1 个答案:

答案 0 :(得分:0)

尝试同步广告制作方法

use FacebookAds\Object\Ad;
use FacebookAds\Object\Fields\AdFields;

$fields = array(
  AdFields::NAME => 'My Ad',
  AdFields::ADSET_ID => $adset_id,
  AdFields::STATUS => Ad::STATUS_PAUSED,
  AdFields::CREATIVE => array(
    'creative_id' => $creative_id,
  ),
);

$ad = new Ad();
$ad->create($fields);

Ad creating