T
T
Turbomanfirst2020-10-22 13:01:37
API
Turbomanfirst, 2020-10-22 13:01:37

Why doesn't the same code work when downloading offline conversions from the Yandex Metrics API?

I ran into an interesting situation when loading offline conversions via the API. The situation is such that the code works on the test and the conversions go to download, but the following arrives on the main domain:

{"errors":[{"error_type":"invalid_uploading","message":"Incorrect data format, error in line number 1"}],"code":400,"message":"Incorrect data format, error in line number 1"}


What could be the problem if the code is identical?

Here is the code itself, due to the specifics, the yclid parameter arrives from the get postback:

$yclid_id = filter_input(INPUT_GET, 'yclid'); 


if($yclid_id != "") {

  function getInfoYa($url,$token,$data,$boundary){
    
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');	

  	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host:api-metrika.yandex.net','Authorization: OAuth '.$token,"Content-Type: multipart/form-data; boundary=------------------------$boundary","Content-Length: " . strlen($data)));


    $response = array();
    $response['html']     = curl_exec($ch);
    $response['err']      = curl_errno($ch);
    $response['errmsg']   = curl_error($ch);
    $response['header']   = curl_getinfo($ch);
    


    curl_close($ch);		
    

    return $response;	


  }


    $token = "мойтокен";
    $orders = "Yclid,Target,DateTime".PHP_EOL;		

  global $yclid_id;

  $yaclid=$yclid_id;
    
    
    $orders .= "".$yaclid.",target,".time()."".PHP_EOL;


    $counterId = номер; //id счетчика
    $boundary = "7zDUQOAIAE9hEWoV";
    $filename = 'data.csv';

  $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 .= $orders . "\x0A\x0D\x0A";
  $data .= "--------------------------$boundary--";


  $url = "https://api-metrika.yandex.net/management/v1/counter/".$counterId."/offline_conversions/upload?client_id_type=YCLID&oauth_token=".$token;
    $yaInfo = getInfoYa($url,$token,$data,$boundary);

  $yaInfo = json_decode($yaInfo["response"]["html"],true);

  ?><pre><?print_r($yaInfo)?></pre><?
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bismoney, 2022-03-30
@bismoney

Also encountered on laravel.
Route was only get, you need either post or any.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question