2016-03-31 18 views
1

我想在我的代碼中發佈JSON。
雖然我設置頁眉Content_Type:application/json
接收服務器獲取作爲Content-Type: application/x-www-form-urlencodedPHP CURL雖然我設置標頭爲應用程序/ json的請求作爲應用程序/ x-www-form-urlencoded

$Content_Type = "application/json"; 
$header_info = "X_RESTBUS_MESSAGE_ID:".$X_RESTBUS_MESSAGE_ID.","."X_BU_ID:".$X_BU_ID.","."Content_Type:".$Content_Type; 
$url = $server_url; 
    $content = json_encode($body_data); 

    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_HEADER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, 
     array($header_info)); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $content); 

    $json_response = curl_exec($curl); 

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); 

13 > X_RESTBUS_MESSAGE_ID: <aaaaa>,X_BU_ID:<xxxx>,Content_Type:application/json 
13 > Accept: */* 
13 > Content-Type: application/x-www-form-urlencoded 
13 > Content-Length: 641 
13 > Host: localhost:49111 
13 > 

回答

0

您已經CURLOPT_HTTPHEADER是在單個項目的數組服務器接收POST調用得到它,它是一個字符串或逗號分隔的標題。

您應該將其設置爲關聯數組,其中每個鍵/值對都是單個標題。

您還需要正確拼寫標題名稱。 Content-Type在中間有一個連字符,而不是下劃線。

$headers = array(
    "X_RESTBUS_MESSAGE_ID" => $X_RESTBUS_MESSAGE_ID, 
    "X_BU_ID"    => $X_BU_ID, 
    "Content-Type"   => $Content_Type 
); 

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
+0

是的。我想到了。謝謝 –

相關問題