【问题标题】:PHP file() function for extracting data from URL用于从 URL 中提取数据的 PHP file() 函数
【发布时间】:2015-05-08 07:59:32
【问题描述】:

虽然这似乎是一个普遍的问题,但如何从 URL 中提取内容,例如 (http://api.openweathermap.org/data/2.5/weather?q=London,uk),以便在 PHP 中以数组的形式格式化和使用它的每个元素?更具体地说,我一般如何从网站中提取信息?

【问题讨论】:

    标签: php weather-api


    【解决方案1】:

    从 URL 获取数据:

    $content = file_get_contents('http://api.openweathermap.org/data/2.5/weather?q=London,uk');
    

    将其转换为简单数组:

    $new_data = json_decode($content, true);
    print_r($new_data);
    

    【讨论】:

      【解决方案2】:
      $json = file_get_contents('http://api.openweathermap.org/data/2.5/weather?q=London,uk');
      $array = json_decode($json, true);
      var_dump($array);
      

      【讨论】:

      • 虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
      猜你喜欢
      • 1970-01-01
      • 2012-05-24
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      相关资源
      最近更新 更多