简体   繁体   English

在发布请求中发送json字符串并获取标头响应

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

I'm trying to send json object to server and to get variable from the header of response. 我正在尝试将json对象发送到服务器,并从响应的标头中获取变量。 when I tried to do this request using postman, it's work great. 当我尝试使用邮递员发出此请求时,它的工作很棒。 but in PHP I get errors. 但是在PHP中我得到了错误。

在此处输入图片说明

my php code: 我的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);

but the response of header I get in php: 但是我在php中获得标头的响应:

string(370) "HTTP/1.1 500 Internal Server Error Cache-Control: no-cache, no-store, max-age=0, must-revalidate Content-Type: application/json;charset=UTF-8 Date: Wed, 13 Dec 2017 09:48:26 GMT Expires: 0 Pragma: no-cache Server: nginx/1.10.1 X-Content-Type-Options: nosniff X-Frame-Options: DENY X-XSS-Protection: 1; mode=block Content-Length: 320 Connection: keep-alive " 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“

how to fix the code to make it work? 如何修复代码以使其正常工作? thank you very much. 非常感谢你。

update: I add to code: 更新:我添加到代码:

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

and the problem solved. 问题就解决了 thank you 谢谢

由于内容是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