eBay API Create a Campaign



<?php
  ini_set('max_execution_time',0);
  //sales_price_percentage may be any value between 1.0 to 20.0 and it must have only one decimal point
  //campaignName must have only AlphaNumeric characters
  //The seller must accept the Promoted Listing terms and conditions from his/her account otherwise error will be displaying while calling the API
  function convertUTC($date)//yyyy-mm-dd hh:mm:ss
  {
      $dt = new DateTime($date);
      $dt->setTimezone(new DateTimeZone('UTC'));
      return $dt->format('Y-m-d H:i:s');
  }
  function createCampaign($credentialArr)
  {
      if($_REQUEST['start_date'] && $_REQUEST['end_date'] && $_REQUEST['max_price'] && $_REQUEST['min_price'] && $_REQUEST['sales_price_percentage'])
      {
          $JSON_data = '{
      "campaignCriterion" :
      {
        "autoSelectFutureInventory" : "true",
        "criterionType" : "INVENTORY_PARTITION",
        "selectionRules" : [
        {
          "categoryIds" :
          '.json_encode($credentialArr['category_array']).',
          "categoryScope" : "MARKETPLACE",
          "maxPrice" :
          {
            "currency" : "EUR",
            "value" : "'.$_REQUEST['max_price'].'"
          },
            "minPrice" :
          {
            "currency" : "EUR",
            "value" : "'.$_REQUEST['min_price'].'"
          }
        }
        ]
      },
        "campaignName" : "'.$_REQUEST['campaign_name'].'",
        "endDate" : "'.explode(' ',convertUTC($_REQUEST['end_date']))[0].'T'.explode(' ',convertUTC($_REQUEST['end_date']))[1].'Z",
        "fundingStrategy" :
        {
        "bidPercentage" : "'.$_REQUEST['sales_price_percentage'].'",
        "fundingModel" : "COST_PER_SALE"
        },
        "marketplaceId" : "EBAY_'.$_REQUEST['site_name'].'",
        "startDate" : "'.explode(' ',convertUTC($_REQUEST['start_date']))[0].'T'.explode(' ',convertUTC($_REQUEST['start_date']))[1].'Z"
      }';
      $ch = curl_init('https://api.ebay.com/sell/marketing/v1/ad_campaign');
      curl_setopt_array($ch, array(
          CURLOPT_HTTPHEADER => array(
           'Authorization: Bearer '.$credentialArr['authToken'],
           'Content-Type: application/json',
           'Accept: application/json',
           'X-EBAY-API-APP-NAME'=>'YOUR_APP_ID'//for best practice,use production environment
       ),
       CURLOPT_RETURNTRANSFER=>TRUE,
       CURLOPT_SSL_VERIFYHOST=>0,
       CURLOPT_SSL_VERIFYPEER=>0,
       CURLOPT_POSTFIELDS => $JSON_data
      ));
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
      $response = curl_exec($ch);
      if($response === FALSE){
       die(curl_error($ch));
      }
      echo $response;
    }
  }
  //Category IDs which support promoted listings for UK
  $maincategoryIds = array('169484','20081','550','14024','2984','169485','267','12576','625','180372','180373','82099','36085','15032','421','177074','11450','11116','1','169487','58058','293','14339','237','11232','180124','159912','175607','3270','26395','3252','11700','72236','281','25622','21670','10063','96382','35578','169486','11233','619','180363','180431','52638','179487','36627','1281','870','36628','173668','84149','888','64482','180433','220','122304','179680','177599','1249','180365','72232');

  createCampaign(
    array(

      'authToken'=>'OAuth_TOKEN',//which expires after certain time so need to refresh this token for your API

    'category_array'=>$maincategoryIds));
?>

Comments