JSON

Accessing the API using JSON and token-based authorization.

Getting information about the client

This example demonstrates calling the GetClientInfo method.

<?php

/*
    Sample program code for interacting with the API of the Yandex Direct service.
 
     The sample uses recommended syntax for the Yandex Direct API 
         in PHP with the JSON protocol and token-based authorization.
 
     Note that all text data must be UTF8-encoded.
 
     For more information about getting a token, see the documentation:
https://tech.yandex.ru/oauth/doc/dg/
*/

require_once "HTTP/Request.php";

// Important: data is sent using the POST method
$req =& new HTTP_Request("https://api.direct.yandex.ru/v/json/");
$req->setMethod(HTTP_REQUEST_METHOD_POST);

// Initializing authorization parameters
$data = array(
    token => "YOUR-YANDEX-OAUTH-TOKEN"
);

// Parameters for requesting the GetClientInfo method
$data['method'] = "GetClientInfo";
$data['param'] = array(YOUR-YANDEX-LOGIN);

/*
 If your version of PHP doesn't support the built-in json_encode/json_decode function, you can  use a library: http://pear.php.net/package/Services_JSON
*/ 

require_once 'Classes/json.php';
$json = new Services_JSON;
$json_data = $json->encode($data);

/*
 If the built-in json_encode/json_decode function is supported, the program may look like this:
        $json_data = json_encode($data);
        $decoded_result = json_decode($result);

*/

$req->addRawPostData($json_data);

$response = $req->sendRequest();
$errmsg = PEAR::isError($response);

if (! $errmsg) {
     $result = $req->getResponseBody();
     $decoded_result = $json->decode($result);

     if (isset($decoded_result->data)) {

 // Processing the method response
         print_r($decoded_result);

     } else if ($decoded_result->error_code) {
         // If the API server returned an error
         echo "Error: code = ".$decoded_result->error_code
                     .", str = ".$decoded_result->error_str
                     .", detail = ".$decoded_result->error_detail;
     } else {
         echo "Unknown error";
     }

} else {
 // If an error occurred when attempting the request
    echo "Request error: ".$errmsg;
}

?>

Creating a campaign

This example demonstrates calling the CreateOrUpdateCampaign method.

<?php
    
# Method for sending a JSON request with OAuth authorization
# PHP 4 >= 4.3.0, PHP 5
 
 
# Yandex Direct login name
$login = 'YOUR-LOGIN';


# API method 
$method = 'CreateOrUpdateCampaign';

 
# input data 
$params = array(            
            'Login' => $login,
            'CampaignID' => 0, 
            'StatusContextStop' => 'No',
            'Name' => 'New campaign',
            'FIO' => 'Ivanov Ivan',        
            'EmailNotification' => array(
                                    'Email' => $login . '@yandex.ru',
                                    'SendWarn' => 'Yes',
                                    'MoneyWarningValue' => 1,
                                    'WarnPlaceInterval' => 15,        
                                    'SendAccNews' => 'No'        
                                    ),    
            'MinusKeywords' => array('download', 'mp3', 'song'),
            'AddRelevantPhrases' => 'Yes',
            'RelevantPhrasesBudgetLimit' => 50,
            'Strategy' => array(
                           'StrategyName' => 'HighestPosition',
                           ),

);

# ============================================================

# converting string data to UTF-8
function utf8($struct) {
    foreach ($struct as $key => $value) {
        if (is_array($value)) {
            $struct[$key] = utf8($value);
        }
        elseif (is_string($value)) {
            $struct[$key] = utf8_encode($value);
        }
    }
    return $struct;
}
  
# forming the request    
$request = array(
    'token'=> 'YOUR-YANDEX-OAUTH-TOKEN', 
    'method'=> $method,
    'param'=> utf8($params),
    'locale'=> 'ru',
);


# transforming to JSON format
$request = json_encode($request);

 
# request parameters
$opts = array(
    'http'=>array(
        'method'=>"POST",
        'content'=>$request,
    )
); 

 
# creating the stream context
$context = stream_context_create($opts); 
 
 
# sending the request and getting a response from the server
$result = file_get_contents('https://api.direct.yandex.ru/v/json/', 0, $context);
 
 
# outputting the result
print_r($result);
    
?>