我正在尝试使用cURL发布一些JSON数据,但我在设置标题时遇到问题。使用cURL在PHP 5中发布JSON数据
我当前的代码看起来像这样:测试使用本地主机(PHP 7)当
$ch = curl_init('https://secure.example.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
]);
if (!$result = curl_exec($ch))
{
echo 'Failed: ' . curl_error($ch);
curl_close($ch);
die;
}
curl_close($ch);
此代码工作正常。但是,我们的Web服务器仅运行PHP 5,因此不支持CURLOPT_HTTPHEADER
选项。
当我将它放在我的代码中时,出现“500内部错误”。 当我将它取出时,我的curl_exec()
未运行,并且收到错误消息“失败:”,但没有显示curl_error()
。
有没有办法设置cURL期望JSON数据没有这个选项?
你在网络服务器上有什么cURL版本? – apokryfos
如果你得到一个500,你会看看服务器的错误日志中的细节。 –
实际上,PHP 5中不支持声明*'CURLOPT_HTTPHEADER'选项*为false。 – apokryfos