我正在发送POST请求,其中包含JSON数据。我的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;
}
添加CURLOPT_FOLLOWLOCATION
以遵循重定向。
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
其次,您的CURLOPT_POSTFIELDS => "{'r'n'"serviceCode'":'"FNS00
似乎不正确的json数据字符串(json中不包含''r''n)。在传递到这里之前,请验证json数据。