Get campaigns

PHP 5 using SOAP with the SoapClient class

This example shows a request using the Campaigns.get method, along with the result processing and output. To use the example, specify the OAuth access token in the input data. For a request on behalf of an agency, also specify the client login.

<?php
ini_set("soap.wsdl_cache_enabled", "0");

//--- Input data -------------------------------------------------------------------------//
// Address of the WSDL description for the Campaigns service (case-sensitive)
$wsdlUrl = 'https://api.direct.yandex.com/v5/campaigns?wsdl';
// OAuth token of the user that requests will be made on behalf of
$token = 'TOKEN';
// Login of the advertising agency client
//Required parameter if requests are made on behalf of an advertising agency
$clientLogin = 'CLIENT_LOGIN';

// --- Preparing and executing the request -----------------------------------//
// Creating the stream context: setting the request HTTP headers
$streamOptions = stream_context_create(array(
   'http' => array(
      'header' => "Authorization: Bearer $token"."\r\n".> >    // OAuth token. The word Bearer must be used
                  "Client-Login: $clientLogin"."\r\n".      // Login of the advertising agency client
                  "Accept-Language: ru"                     // Response language
   ),
   /*
   // To fully conform to the HTTPS protocol, you can enable verification of the SSL certificate on the Yandex Direct API server
   'ssl' => array(
      'verify_peer' => true,
      'cafile' => getcwd().DIRECTORY_SEPARATOR.'CA.pem' // Path to the local copy of the root SSL certificate
   )
   */
));

// Initializing the SOAP client
$client = new SoapClient($wsdlUrl,
   array(
      'trace'             => true,
      'exceptions'        => false,
      'features'          => SOAP_SINGLE_ELEMENT_ARRAYS,
      'cache_wsdl'        => WSDL_CACHE_NONE,
      'encoding'          => 'UTF-8',
      'stream_context'    => $streamOptions
   )
);

// Parameters of the request to the Yandex Direct API server
$params = array(
   "SelectionCriteria" => (object) array(),     // Selection criteria for filtering campaigns. To get all campaigns, leave it empty
   "FieldNames" = > array ('Id', 'Name') // Names of parameters to get
);

// Executing a request with the get method for the Campaigns service and getting the result
$result = $client->get ($params);

// --- Processing the result---------------------------//
if (is_soap_fault($result)) {
   $error = "SOAP Fault: faultcode: {$result->faultcode}, faultstring: {$result->faultstring}";
   if (isset($result->detail->FaultResponse)) {
      $apiErr = $result->detail->FaultResponse;
      $error .= " detail: {$apiErr->errorCode} - {$apiErr->errorDetail} (RequestId: {$apiErr->requestId})";
   }
   echo $error;
}
else {
 // Extracting HTTP response headers: RequestId (ID of the request) and Units (information about points)
 $headers = explode("\r\n", $client->__getLastResponseHeaders());
   foreach ($headers as $header) {
      if (preg_match('/(RequestId|Units):/', $header)) { echo "$header<br>"; }
   }
   
   // Outputting the list of campaigns
   foreach ($result->Campaigns as $row) { echo "Ad campaign: {$row->Name} ({$row->Id})<br>"; }
}

//--- Debugging information ---------------------------------------------//
//echo "<hr>Request headers:<pre>".$client->__getLastRequestHeaders()."</pre>";
// echo "Request:<pre>".htmlspecialchars($client->__getLastRequest())."</pre>";
// echo "Response headers:<pre>".$client->__getLastResponseHeaders()."</pre>";
// echo "Response:<pre>".htmlspecialchars($client->__getLastResponse())."</pre>";
?>