• Загрузка офлайн-конверсий с помощью API на PHP?

    @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><?
    	
    ?>
    Ответ написан
    Комментировать