Answer the question
In order to leave comments, you need to log in
Why does an API request work in Postman, but not via PHP-cURL?
I work with the MySklad API, the task is to get an invoice from them
. I made a request in Postman, everything works fine:
But I need to get this invoice in my php file for further work. In the screenshot above, on the right, there is a block with php / curl code, which I copy and add to my php file. The only thing I add there are 2 more lines for http authorization:
CURLOPT_USERPWD => 'mylogin:mypassword',
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://online.moysklad.ru/api/remap/1.2/entity/demand/fdc8dbab-acd6-11eb-0a80-09cb004a2c1a/export/',
CURLOPT_USERPWD => 'mylogin:mypassword',
CURLOPT_HTTPAUTH => CURLAUTH_DIGEST,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"template": {
"meta": {
"href": "https://online.moysklad.ru/api/remap/1.2/entity/demand/metadata/embeddedtemplate/29a9b716-8664-4b13-baae-a78db1710bdb",
"type": "embeddedtemplate",
"mediaType": "application/json"
}
},
"extension": "xls"
}',
CURLOPT_HTTPHEADER => array(
'Authorization: Basic ==my-custom-token==',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
{
"errors" : [ {
"error" : "Ошибка формирования печатной формы: отсутствует ссылка на шаблон для печати",
"code" : 33003,
"moreInfo" : "https://dev.moysklad.ru/doc/api/remap/1.2/#error_33003"
} ]
}
CURLOPT_HTTPHEADER => array(
'Authorization: Basic ==my-custom-token==',
'Content-Type: application/json',
'Content-Length: 278',
),
$info = curl_getinfo($curl)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question