使用 cURL 获取 NULL,但使用 file_get_contents() 获取数组

发布于 2024-12-20 11:22:52 字数 645 浏览 1 评论 0原文

我在使用链接的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御守 2024-12-27 11:22:52

调用后您不会进行任何错误检查,因此如果出现问题,您将永远不会听到。

其中之一可能会揭示问题所在。例如,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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文