使用 CURL 读取 JSON 数据


Reading JSON data with CURL

我正在尝试使用 CURL 读取 URL。当我在浏览器中运行此URL时,一个文件会下载到我的系统上,文件中的内容如下

{"Success":true,"Message":"Success","Response":0.0000}

现在我想获取此文件的内容。到目前为止,我已经使用了这个 -

$send_curl = curl_init($url);
curl_setopt($send_curl, CURLOPT_URL, $url);
curl_setopt($send_curl, CURLOPT_HEADER, false);
curl_setopt($send_curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($send_curl, CURLOPT_POST, TRUE);
curl_setopt($send_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($send_curl, CURLOPT_HTTPHEADER,array('Accept: application/json', "Content-type: application/json"));
curl_setopt($send_curl, CURLOPT_FAILONERROR, FALSE);
curl_setopt($send_curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($send_curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$json_response = curl_exec($send_curl);
$status = curl_getinfo($send_curl, CURLINFO_HTTP_CODE);
curl_close($send_curl);
$response = json_decode($json_response, true);

但我没有得到适当的回应。知道我哪里出错了吗?

在本地 json url 进行快速测试后,我发现如果您删除它,您会没事的:

curl_setopt($send_curl, CURLOPT_POST, TRUE);

我的意思是,如果您没有尝试实际发布。 否则,它总是给我一个"400 错误请求"错误。

希望这有帮助!