Get campaigns

PHP 5 using JSON with the cURL library

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.

//--- Input data ---------------------------------------------------------------------//
// Address of the Campaigns service for sending JSON requests (case-sensitive)
$url = '';
// OAuth token of the user to execute requests 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 -----------------------------------//
// Setting the request HTTP headers
$headers = array(
    "Authorization: Bearer $token",                   //OAuth token. The word Bearer must be used
   "Client-Login: $clientLogin",                      // Login of the advertising agency client
   "Accept-Language: en",                             // Language for response messages
   "Content-Type: application/json; charset=utf-8"    // Data type and request encoding

// Parameters for the request to the Yandex.Direct API server
$params = array(
   'method' => 'get',                                 // Method of the Campaigns service
   'params' => array(
      'SelectionCriteria' => (object) array(),        // Criteria for filtering campaigns. To get all campaigns, leave it empty
        'FieldNames' => array('Id', 'Name')           // Names of parameters to get
// Converting input parameters to JSON

// Initializing cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);

To fully conform to the HTTPS protocol, you can enable verification of the SSL certificate on the Yandex.Direct API server.
To enable verification, set the option CURLOPT_SSL_VERIFYPEER to true, comment out the line with the option CURLOPT_CAINFO, and set the path to the local copy of the root SSL certificate. 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($curl, CURLOPT_CAINFO, getcwd().'\CA.pem');

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

// Executing the request and getting the result
$result = curl_exec($curl);

//--- Processing the result  ---------------------------//
if(!$result) { echo ('cURL error: '.curl_errno($curl).' - '.curl_error($curl)); }
else {
   // Separating HTTP headers from the response message body
   $responseHeadersSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
   $responseHeaders = substr($result, 0, $responseHeadersSize);
   $responseBody = substr($result, $responseHeadersSize);
   if (curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) { echo "HTTP error: ".curl_getinfo($curl, CURLINFO_HTTP_CODE); }
   else {
      // Converting the response to JSON
      $responseBody = json_decode($responseBody);

      if (isset($responseBody->error)) {
         $apiErr = $responseBody->error;
         echo "API error {$apiErr->error_code}: {$apiErr->error_string} - {$apiErr->error_detail} (RequestId: {$apiErr->request_id})";
      else {
         // Extracting HTTP response headers: RequestId (ID of the request) and Units (information about points)
         $responseHeadersArr = explode("\r\n", $responseHeaders);
         foreach ($responseHeadersArr as $header) {
            if (preg_match('/(RequestId|Units):/', $header)) { echo "$header <br>"; }

         // Outputting the list of ad campaigns
         foreach ($responseBody->result->Campaigns as $campaign) {
            echo "Ad campaign: {$campaign->Name} ({$campaign->Id})<br>";

//--- Debugging information ---------------------------------------------//
//echo "<hr>Request headers: <pre>".curl_getinfo($curl, CURLINFO_HEADER_OUT)."</pre>";
//echo "Request: <pre>".json_encode($params, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."</pre>";
//echo "Response headers: <pre>".$responseHeaders."</pre>";
//echo "Response: <pre>".json_encode($responseBody, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)."</pre>";