使用发布请求时的状态为301

时间:2016-05-13 19:01:04

标签: php curl

我正在发送包含JSON数据的POST请求。我的PHP代码返回301状态。我尝试在JS和C#中这样做,但它仍然无法正常工作。但!!使用Postman我收到了我需要的数据的正确答案。不过,我是复制粘贴Postman生成的代码,它也不起作用!!

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "https://api-dev.gu.spb.ru/hakatonRest/checkInn",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n\"serviceCode\":\"FNS001\",\r\n\"serviceData\":           {\r\n            \"firstName\":\"иван\",\r\n            \"lastName\":\"иванов   \",\r\n            \"secondName\":\"иванович\",\r\n            \"birthday\":\"22.07.1989\",\r\n            \"documentType\":\"21\",\r\n            \"documentSeries\":\"4112\",\r\n            \"documentNumber\":\"412512\"\r\n}\r\n\r\n}\r\n",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/json",
"postman-token: 1dcb6bfa-531c-aff8-535e-70ba5c799775"
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

1 个答案:

答案 0 :(得分:1)

添加CURLOPT_FOLLOWLOCATION以关注重定向。

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

其次你的CURLOPT_POSTFIELDS => "{\r\n\"serviceCode\":\"FNS00似乎是不正确的json数据字符串(json中不包含\ r \ n)。请在传递之前验证json数据。