使用cURL发布文件,但不知道POST的密钥

时间:2013-07-29 02:21:19

标签: php file post curl

我正在尝试使用WoT Dossier服务将档案转换为JSON,在他们的FAQ中,他们会显示以下cURL示例:

curl --data-binary @dossier.dat http://wot-dossier.appspot.com/service/dossier-to-json

经过几个小时的摸索,我发现了什么问题,我没有运气。这是我使用的代码(PHP)尝试复制命令:

$url = "http://wot-dossier.appspot.com/service/dossier-to-json";

$file = realpath('dossier_temp/' . $full_fn);

echo $file;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/octet-stream"));
curl_setopt($ch, CURLOPT_POSTFIELDS, array("@$file"));

$curl_res = curl_exec($ch);

curl_close($ch);

$json = json_decode($curl_res, true);

json的var_dump()显示为NULL,但这怎么可能?在某个地方的帖子中是否有错误?

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

在postfields中不包含数组,试试这个。看来你的帖子只需要是一个原始的数据流。

$data = file_get_contents($file);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);