V
V
Vadim Timoshenko2018-09-10 15:53:48
PHP
Vadim Timoshenko, 2018-09-10 15:53:48

How to send call data to the Yandex.Metrica API?

Based on documentation https://tech.yandex.ru/metrika/doc/api2/management... I'm trying to load data from the example into Yandex metric:
UserId,DateTime,Price,Currency,PhoneNumber,TalkDuration,HoldDuration,CallMissed,Tag, FirstTimeCaller,URL , CallTrackerURL
133591247640966458.1481714026.678.90 +70987654321,17,23,0,,2, https://test.com/,https://test.com/ I do this with cURL:

$token  = "AAAAAAAAA";
    $counter_id = "XXXXXXXXXXXX";

    $url = 'https://api-metrika.yandex.ru/management/v1/counter/' . $counter_id . '/offline_conversions/upload_calls?client_id_type=USER_ID&oauth_token=' . $token;

    $file = "csv.csv";

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: text/csv']);

    $cfile = new CurlFile($file,  'text/csv');
    $data = array('data-binary' => $cfile);

    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $curl_response = curl_exec($curl);

    curl_close($curl);
    
    return $curl_response;

The text of the documentation says: Data is transferred as multipart/form-data, in CSV format, in the first line it is necessary to transfer the names of the columns. "But how to transfer the first line with the names of the columns first? Where I think is wrong?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xoo, 2018-09-10
@PbI6A_KuT

Oh, you moved it into a separate question)
In general, StaticCall,UserId - mandatory columns
Headers

Content-Type: multipart/form-data; boundary=-----------------------boundary
Content-Length: 12345

Body
--------------------------boundary\r\n
Content-Disposition: form-data; name="file"; filename="file.csv"\r\n
Content-Type: text/csv\r\n\r\n
DATA\r\n\r
----------------- ---------boundary--

Example (for curl everything is the same)
function request($url, $data, $headers){
  $opt = array(
    'http' => array(
      'method' => 'POST',
      'content' => $data,
      'header' => $headers
    )
  );

  $context = stream_context_create($opt);

  $response = @file_get_contents($url, FALSE, $context);
  
  return $response;
  
}

$oauth_token='xxx';
$boundary = "7zDUQOAIAE9hEWoV";
$filename = 'data.csv';

$calls = "StaticCall,UserId,DateTime,Price,Currency,PhoneNumber,TalkDuration,HoldDuration,CallMissed,Tag,FirstTimeCaller,URL,CallTrackerURL".PHP_EOL;
$calls .= "1,133591247640966458,1481714026,678.90,RUB,+71234567890,136,17,0,,1,https://test.com/,https://test.com/".PHP_EOL;
$calls .= "1,579124169844706072,1481718066,123.45,RUB,+70987654321,17,23,0,,2,https://test.com/,https://test.com/".PHP_EOL;
$calls .= "1,148059425477661429,1481718126,678.90,RUB,+71234509876,72,11,0,,0,https://test.com/,https://test.com/";

$data = "--------------------------$boundary\x0D\x0A";
$data .= "Content-Disposition: form-data; name=\"file\"; filename=\"$filename\"\x0D\x0A";
$data .= "Content-Type: text/csv\x0D\x0A\x0D\x0A";
$data .= $calls . "\x0A\x0D\x0A";
$data .= "--------------------------$boundary--";


$headers = array();
$headers[] = "Content-Type: multipart/form-data; boundary=------------------------$boundary";
$headers[] = 'Content-Length: '.strlen($data);
$headers = implode(PHP_EOL, $headers);

$url = "https://api-metrika.yandex.ru/management/v1/counter/39764535/offline_conversions/upload_calls?client_id_type=USER_ID&oauth_token=$oauth_token";

$result = request($url, $data, $headers);

var_dump($result);

For curl like that
CURLOPT_POSTFIELDS => $data
CURLOPT_HTTPHEADER => array(
    "Content-Type: multipart/form-data; boundary=------------------------$boundary",
    "Content-Length: " . strlen($data)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question