B
B
BarneyGumble2021-06-14 01:39:01
PHP
BarneyGumble, 2021-06-14 01:39:01

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:
kHlTbQ1d.jpg?download=1&name=%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%2014-06-2021%2001:29 :53.jpg

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,


In the end my code looks like this:
<?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;


As a result, I run my php file in the browser and get:
{
  "errors" : [ {
    "error" : "Ошибка формирования печатной формы: отсутствует ссылка на шаблон для печати",
    "code" : 33003,
    "moreInfo" : "https://dev.moysklad.ru/doc/api/remap/1.2/#error_33003"
  } ]
}


At first I thought that the error was in the body of the request ... but where is the mistake if everything works in Postman, but not when copying the code for php. I started to turn off checkboxes in Headers in Postman, and, having turned off the "Content-Length" checkbox, I got the same error that I get in the browser:
ZYlAWkmF.jpg?download=1&name=%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%2014-06-2021%2001:31 :08.jpg

I was delighted, I think you just need to add CURLOPT_HTTPHEADER to make it like this:
CURLOPT_HTTPHEADER => array(
  'Authorization: Basic ==my-custom-token==',
  'Content-Type: application/json',
  'Content-Length: 278',
),


By the way, I got Content-Length by adding output to my code $info = curl_getinfo($curl)

. But it didn't help, the error is the same. Stuck, where to dig further?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timpo, 2021-06-14
@Timpo

You can dig with wireshark, compare the two packets that are sent and see the difference

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question