使用 cURL 获取 NULL,但使用 file_get_contents() 获取数组
我在使用链接的 CURL 时遇到问题。我可以使用 file_get_contents();
获得输出,但是
使用 CURL 时遇到问题 json_decode
我使用 cURL
得到 NULL,但是使用 获取数组
file_get_contents()
我使用 cURL
$url="https://example.com/"
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json= json_decode(curl_exec($ch),true);
echo $json; //outputs NULL
使用 file_get_contents();
$json_pi = file_get_contents($url);
echo json_decode($json_pi,true);
谁能帮助我理解 cURL?为什么我可能会得到这两个相互矛盾的结果?
谢谢你!
I'm having problem with CURL from a link. I'm able to get an output with file_get_contents();
But having problems with CURL
use json_decode
I get a NULL with cURL
, but with file_get_contents()
I get an Array
Using cURL
$url="https://example.com/"
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json= json_decode(curl_exec($ch),true);
echo $json; //outputs NULL
Using file_get_contents();
$json_pi = file_get_contents($url);
echo json_decode($json_pi,true);
Can anyone help me understand cURL? And why I might be getting these two conflicting results?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用后您不会进行任何错误检查,因此如果出现问题,您将永远不会听到。
使用
curl_error()检查CURL调用的结果
使用
json_last_error()
(PHP >= 5.3)其中之一可能会揭示问题所在。例如,curl 调用可能会获取非 UTF-8 字符集中的数据,这将导致 json_decode() 中断 - 它始终需要 UTF-8 数据。
You are not doing any error checking after your calls, so if something goes wrong, you will never hear about it.
Check the result of the CURL call using
curl_error()
Check the result of the json_encode() call using
json_last_error()
(PHP >= 5.3)one of these will probably reveal what the problem is. For example, it could be that the curl call fetches the data in a non-UTF-8 character set, which will cause
json_decode()
to break - it expects UTF-8 data at all times.