繁体   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