[英]Php CURL Though I set the header as application/json the request goes as application/x-www-form-urlencoded
我想在我的代碼中POST JSON。
雖然我設置了標題Content_Type:application/json
接收服務器獲取為Content-Type: application/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);
但接收POST呼叫的服務器獲取為
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 >
你有一個CURLOPT_HTTPHEADER
是一個包含單個項目的數組,它是一個字符串或逗號分隔的標題。
您應該將其設置為一個關聯數組,其中每個鍵/值對都是單個標頭。
您還需要正確拼寫標題名稱。 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.