Ошибки при выполнении post-запроса, в чем причина?

Добрый день!

Есть форма:
<form method="post" action="post.php">
    <input type="text" name="query" id="documentNumber" oninput="identifyType(this.value)">
    <input type="submit" value="Check" id="check">
</form>

Пытаюсь выполнить post-запрос в post.php:
$data = http_build_query(
    array(
        'query' => $_POST['query']
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $data
    )
);

$context  = stream_context_create($opts);
$result = file_get_contents('https://rmsp.nalog.ru/search-proc.json', false, $context);
echo $result;


В результате появляется предупреждение:
Warning: file_get_contents(https://rmsp.nalog.ru/search-proc.json): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request


Пробовал использовать curl:
if (isset($_POST['query'])) {$query = $_POST['query'];}
$myCurl = curl_init();

curl_setopt_array($myCurl, [
    CURLOPT_URL => 'https://rmsp.nalog.ru/search-proc.json',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => 'query='.$query
]);

$response = curl_exec($myCurl);
curl_close($myCurl);

echo $response;


Получаю следующий ответ:
{"ERROR":"-","STATUS":500}


Пробовал в php.ini прописывать разное:
allow_url_fopen=On
extension=php_openssl.dll
allow_url_include = On


Если просто использовать форму без скрипта, то запрос выполняется:
<form method="post" action="https://rmsp.nalog.ru/search-proc.json">
    <input type="text" name="query" id="documentNumber" oninput="identifyType(this.value)">
    <input type="submit" value="Check" id="check">
</form>


И в строке браузера тоже работает:
https://rmsp.nalog.ru/search-proc.json?query=7705151026


В чем может быть проблема?
Мне необходимо в post запросе передать ИНН, введенный в текстовое поле и получить информацию о предприятии.
  • Вопрос задан
  • 517 просмотров
Решения вопроса 1
RomaZveR
@RomaZveR
CEO AlertMoney, PHP/Golang Developer
Через обычный GET запрос получайте.
$requestUrl = 'https://rmsp.nalog.ru/search-proc.json' . '?' . http_build_query([
              'query' => empty($_POST['query']) ? 0 : intval($_POST['query'])
              ], null, '&', PHP_QUERY_RFC3986);
$response = json_decode(file_get_contents($requestUrl));
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
YCLIENTS Москва
от 200 000 до 350 000 ₽
Ведисофт Екатеринбург
от 25 000 ₽
ИТЦ Аусферр Магнитогорск
от 100 000 до 160 000 ₽
24 апр. 2024, в 09:14
5000 руб./за проект
24 апр. 2024, в 09:13
2000 руб./в час