我正在将内容从PHP Curl脚本发送到API。 我正在使用这是在传递JSON标头的同时做POST做我的脚本
[HttpGet]
public ActionResult Download(string id)
{
string fileName = "yourfilename.pdf";
// other code here
if (TempData["FileToDownload"] != null)
{
byte[] content = TempData["FileToDownload"] as byte[];
return File(content, "application/pdf", fileName);
}
else
{
return new EmptyResult();
}
}
但是当我在API端跟踪请求的内容类型时,会得到
$query = new stdClass;
$query->test = 'test';
$query = json_encode($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost');
curl_setopt($ch, CURLOPT_HEADER, ['Content-Type: application/json', 'Content-Length: '.strlen($query)]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$res = curl_exec($ch);
curl_close($ch);
我不是要这个吗?
var_dump($_SERVER['CONTENT_TYPE']);
//application/x-www-form-urlencoded
答案 0 :(得分:2)
您应该使用CURLOPT_HTTPHEADER
而不是CURLOPT_HEADER
CURLOPT_HEADER
可以为true / false,并定义是否包含要响应的标头
仅供参考:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
这些行是多余的,因为您没有使用https