U
U
UbivaIIIka2018-01-04 02:29:06
PHP
UbivaIIIka, 2018-01-04 02:29:06

Loading offline conversions using PHP API?

Please help me implement the Yandex method in PHP.
https://tech.yandex.ru/metrika/doc/api2/management...
I tried something with a curl, but it didn’t work out very well for me (Debazhil, Yandex does not give any errors, but the data file is not loaded on the server.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg, 2018-01-04
@politon

so it shouldn't load. You receive data and parse on the fly. You can also save as a file. But the data is received as a string

D
dmitred, 2020-09-08
@dmitred

<?


  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 = "XXXXXXXXXXXXXXXXX";
    $orders = "UserId,Target,DateTime,Price,Currency".PHP_EOL;		
    
    
    
    $orders .= "719992702,PAY,".time().",50,RUB".PHP_EOL;
    $orders .= "599163530,PAY,".time().",150,RUB".PHP_EOL;
    $orders .= "317910723,PAY,".time().",175,RUB".PHP_EOL;		
    
    $counterId = XXXXXXXX; //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=CLIENT_ID&oauth_token=".$token;

    $yaInfo = getInfoYa($url,$token,$data,$boundary);

    $yaInfo = json_decode($yaInfo["response"]["html"],true);
    
    ?><pre><?print_r($yaInfo)?></pre><?
  
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question