2016-05-13 165 views
1

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

<?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; 
} 
+0

这是坏了'“{\ r \ n \”serviceCode \“:\”FNS001 \“,\ r \ n \”serviceData \“:'。''postman-token'?这是真实的头? –

回答

1

添加CURLOPT_FOLLOWLOCATION以遵循重定向。

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

其次你CURLOPT_POSTFIELDS => "{\r\n\"serviceCode\":\"FNS00似乎不正确的JSON数据串(JSON不包含\ r \ n在那里)。请在通过此处之前验证json数据。