【问题标题】:php Get JSON from URL [closed]php 从 URL 获取 JSON [关闭]
【发布时间】:2019-09-20 13:09:22
【问题描述】:

有一个 API 的结果是这样的:

// 20190920173100
//api.opencagedata.com/geoc

{
  "documentation": "https://opencagedata.com/api",
  "licenses": [
    {
      "name": "see attribution guide",
      "url": "https://opencagedata.com/credits"
    }
  ],
  "rate": {
    "limit": 2500,
    "remaining": 2493,
    "reset": 1569024000
  },
  "results": [
    {
      "bounds": {
        "northeast": {
          "lat": 51.9528202,
          "lng": 7.6325938
        },
        "southwest": {
          "lat": 51.9525445,
          "lng": 7.6323594
        }
      },
      "components": {
        "ISO_3166-1_alpha-2": "DE",
        "ISO_3166-1_alpha-3": "DEU",
        "_type": "building",
        "city": "Münster",
        "city_district": "Münster-Mitte",
        "continent": "Europe",
        "country": "Germany",
        "country_code": "de",
        "county": "Münster",
        "house_number": "7",
        "neighbourhood": "Josef",
        "political_union": "European Union",
        "postcode": "48153",
        "road": "Friedrich-Ebert-Straße",
        "state": "North Rhine-Westphalia",
        "state_district": "Regierungsbezirk Münster",
        "suburb": "Innenstadtring"
      },
      "confidence": 10,
      "formatted": "Friedrich-Ebert-Straße 7, 48153 Münster, Germany",
      "geometry": {
        "lat": 51.9526599,
        "lng": 7.632473
      }
    }
  ],
  "status": {
    "code": 200,
    "message": "OK"
  },
  "stay_informed": {
    "blog": "https://blog.opencagedata.com",
    "twitter": "https://twitter.com/opencagedata"
  },
  "thanks": "For using an OpenCage API",
  "timestamp": {
    "created_http": "Fri, 20 Sep 2019 13:01:02 GMT",
    "created_unix": 1568984462
  },
  "total_results": 1
}

我想用上面的数据。

我尝试了以下代码:

$adress  = json_decode(file_get_contents("https://api...."));

$detail = $adress ->rate->limit;

没关系

问题是当我使用此代码获取结果数据时,例如它为空

$adress  = json_decode(file_get_contents("https://api...."));

$detail = $adress ->result>formatted;

我猜是因为对象和数组的原因

【问题讨论】:

  • 你的 json 有 no result 属性。
  • "结果" != "结果"

标签: php json api


【解决方案1】:

不要使用file_get_contents函数 (该函数将整个文件读入字符串)

因为如果fopen wrappers 仅按照here 的描述启用,则可以将 URL 用作此函数的文件名。那是因为有时这是关闭的(取决于您的主机,例如共享主机),因此您的应用程序将无法运行。

所以我最好的建议是使用curl()(客户端网址)

因此,根据您的情况,您正在从 API 请求数据。所以,我们可以使用GET方法来请求带有curl()的数据,如下所示,带有一些headers,

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api....",
  CURLOPT_RETURNTRANSFER => true, // To get actual result from the successful operation
  CURLOPT_TIMEOUT => 30, // Set maximum time the request is allowed to take (in seconds)
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, // Specify HTTP protocol version to use
  CURLOPT_CUSTOMREQUEST => "GET", // Here the request method
  CURLOPT_HTTPHEADER => array( // Set custom HTTP headers
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl); // Here you will get the response after executing
$error = curl_error($curl); // Return a string containing the last error for the current session

curl_close($curl); // Close the cURL session

所以现在响应以JSON 对象返回。现在您可以使用json_decode() 将它放入一个可用的对象或数组中,就像您已经这样做了一样。

$response = json_decode($response, true); // Because of true, it's in an array

正如will 已经在他的answer 中回答的那样,与rate 不同,results 对象是一个包含您的数据的数组。所以你必须将result 更改为results[0] 才能读取formatted 数据。

$detail = $response->results[0]->formatted;

【讨论】:

  • 感谢您对 file_get_contents 的帮助,这很有帮助
  • 很高兴知道它有帮助。
【解决方案2】:

$results 是一个数组,所以你必须这样做:$detail = $adress ->results[0]->formatted;

【讨论】:

  • 你说得对,谢谢
猜你喜欢
  • 2013-12-15
  • 2016-07-28
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
相关资源
最近更新 更多