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, read the documentation:
        https://tech.yandex.com/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/v4/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 of sending a JSON request with OAuth authorization
# PHP 4 >= 4.3.0, PHP 5
 
 
# Yandex.Direct login 
$login = 'YOUR-LOGIN';


# API method 
$method = 'CreateOrUpdateCampaign';

 
# Input data 
$params = array(            
            'Login' => $login,
            'CampaignID' => 0, 
            'StatusContextStop' => 'No',
            'Name' => 'New campaign',
            'FIO' => 'Ivan Ivanovich Ivanov',        
            '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',
),

);
 
# ============================================================
 
# Encoding string data in 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',
);
 
 
# Converting to JSON
$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 receiving the server response
$result = file_get_contents('https://api.direct.yandex.ru/v4/json/', 0, $context);
 
 
# Outputting the result
print_r($result);
    
?>