簡體   English   中英

在發布請求中發送json字符串並獲取標頭響應

[英]send json string in post request and get the header response

我正在嘗試將json對象發送到服務器,並從響應的標頭中獲取變量。 當我嘗試使用郵遞員發出此請求時,它的工作很棒。 但是在PHP中我得到了錯誤。

在此處輸入圖片說明

我的PHP代碼:

$url = 'https://test.com/login';
$postArray = array('email' => 'test@gmail.com', 'password' => 'test');  

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($postArray));
$response = curl_exec ($curl);

$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);

var_dump($header);

但是我在php中獲得標頭的響應:

string(370)“ HTTP / 1.1 500 Internal Server Error Cache-Control:no-cache,no-store,max-age = 0,必須重新驗證Content-Type:application / json; charset = UTF-8日期:Wed, 2017年12月13日09:48:26 GMT過期:0語法:無緩存服務器:nginx / 1.10.1 X-Content-Type-Options:nosniff X-Frame-Options:DENY X-XSS-Protection:1; mode =阻止Content-Length:320連接:keep-alive“

如何修復代碼以使其正常工作? 非常感謝你。

更新:我添加到代碼:

curl_setopt($curl, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain')); 

問題就解決了 謝謝

由於內容是json,因此您需要使用multipart/form-data

curl_setopt($curl, CURLOPT_HTTPHEADER, array("content-type: multipart/form-data;"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM