IPinfo 如何使用 file_get_contents 显示 json 数据

How does IPinfo show json data using file_get_contents

今天我使用 api 获取特定 IP 地址的信息,它工作正常。

file_get_contents("https://ipinfo.io/172.217.19.206");

由此我得到正常的 json 输出,我可以 json_decode

{ "ip": "172.217.19.206", "hostname": "ams16s31-in-f14.1e100.net", "city": "", "region": "", "country": "US", "loc": "37.7510,-97.8220", "org": "AS15169 Google LLC" }

但我不明白的是,如果我使用浏览器访问页面 https://ipinfo.io/172.217.19.206,那么我根本看不到此输出。我一直以为file_get_contents获取页面的来源?

本网站使用 useragent 方法处理此问题。

你可以通过Postman测试一下。

仅在 Headers 部分为 User-Agent 设置空值并向 url 发送获取请求。

这在我们的文档中有所介绍:https://ipinfo.io/developers#json-response

We try to automatically detect when someone wants to call our API vs view our website, and send back the appropriate JSON response rather than HTML. We do this based on the user agent for known popular programming languages, tools and frameworks

"magic"还有一些其他的位。如果有 URL 回调参数或 AJAX header,我们将始终发送 JSON。

您还可以通过在 URL 末尾添加 /json 或发送 Accept: application/json header.[= 来强制执行 JSON 响应14=]