2016-05-13 190 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數據。