【发布时间】:2015-10-05 09:02:54
【问题描述】:
我有一个在终端上运行良好的 api 链接。我想使用相同的 api 链接在 php 中使用 CURL 检索数据。我试过下面的代码:
<?php
// set up the curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'urlxxxxx');
// execute the request
$output = curl_exec($ch);
// output the profile information - includes the header
echo "curl response is : <br> ".($output). PHP_EOL;
// close curl resource to free up system resources
curl_close($ch);
?>
但我得到的回应是:
卷曲响应是: { "error" : "JSON 语法错误:JSON 字符串格式错误,既不是数组、对象、数字、字符串也不是原子,在 /usr/share/perl5/JSON 的字符偏移量 0 之前(在 \"(字符串结尾)\" 之前) .pm 第 171 行。\n" }
我是 CURL 新手,请指导我解决问题。
【问题讨论】:
-
您没有
CURLOPT_POSTFIELDS发送参数的选项。 -
我不想发送任何参数,我只想检索数据。
-
API 需要 JSON 格式的参数。也许您需要发送一个空数组或对象。
-
如果我删除 curl_setopt($ch, CURLOPT_POST, true);我得到错误 404
-
你能显示运行正常的命令行版本吗?