Answer the question
In order to leave comments, you need to log in
Error when saving a segment created from a file in Yandex Audience?
Hello, I ran into a problem when integrating with Yandex Audiences. When sending a post to save a segment created from a file, an error like "Could not read JSON, error in line 1, column 2" occurs.
Link to documentation https://yandex.ru/dev/audience/doc/segments/confir... Below is the source code:
//Source
$token = '';
$curl = curl_init(" https://api-audience.yandex.ru/v1/management/segme... ");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('file' => new CurlFile(realpath('test2.csv'))));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data", "Authorization: OAuth $token"));
$result = curl_exec($curl);
if(!$result) { echo ("Error: ".curl_error($curl).'('.curl_errno($curl).')'); }
else { echo $result; }
$segId = json_decode($result);
curl_close($curl);
$data_string = array("segment" => array('id' => $segId->segment->id, 'name' => 'test', 'hashed' => 0, 'content_type' => 'crm' ));
$curl2 = curl_init(" https://api-audience.yandex.ru/v1/management/segment/ ".$segId->segment->id."/confirm");
curl_setopt($curl2, CURLOPT_POST, true);
curl_setopt($curl2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl2, CURLOPT_HTTPHEADER, array("Authorization: OAuth $token"));
$result2 = curl_exec($curl2);
if(!$result2) { echo ("Error: ".curl_error($curl2).'('.curl_errno($curl2).')'); }
else { echo $result2; }
curl_close($curl2);
Query results:
{"segment":{"id":13222537,"type":"uploading","status":"uploaded","has_guests":false,"guest_quantity":0,"can_create_dependent":false," has_derivatives":false,"hashed":false,"item_quantity":1006,"guest":false}}{"errors":[{"
Please help me figure it out, I can't figure out what I'm doing wrong
Answer the question
In order to leave comments, you need to log in
Found the answer to my question) Inattention -_-
The answer is this: the second request did not send the "Content-Type" HTTP header, which can lead to the indicated situation.
That is, curl_setopt($curl2, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: OAuth $token"));
I close the question)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question