2017-08-30 113 views

回答

1

你读文件必须d o以下更改json_decode('https://api.darksky.net/forecast/92cf27941c6ea888652ba37de4da4044/37.8267,-122.4233', true);将尝试解码url字符串,它不会解码结果。为此你必须执行这个URL。

$url = "https://api.darksky.net/forecast/92cf27941c6ea888652ba37de4da4044/37.8267,-122.4233"; 
$json_data = file_get_contents($url); 
$data = json_decode($json_data, TRUE); 
echo $data['currently']['temperature']; 
+0

很高兴能帮到@RaselAhmed :) –

2

你不能直接调用json url。你需要一个文件功能。这是一个示例。

$json_url = "http://awebsites.com/file.json"; 
$json = file_get_contents($json_url); 
$data = json_decode($json, TRUE); 
echo "<pre>"; 
print_r($data); 
echo "</pre>"; 
2

请试试这个

<?php 
     $data = json_decode(file_get_contents('https://api.darksky.net/forecast/92cf27941c6ea888652ba37de4da4044/37.8267,-122.4233', true)); 

     echo $data->currently->temperature; 
    ?> 
+0

简单和简单。谢谢。 –

2

您不能直接在不使用卷曲的file_get_contents

下面摘录获取数据

<?php 

$your_url = "https://api.darksky.net/forecast/92cf27941c6ea888652ba37de4da4044/37.8267,-122.4233"; 
$get_data = file_get_contents($your_url); 
$data = json_decode($get_data, TRUE); 
echo $data['currently']['temperature']; 

?> 
1

您需要先使用curl或file_get_contents获取页面的内容。试试以下代码

$url = 'https://api.darksky.net/forecast/92cf27941c6ea888652ba37de4da4044/37.8267,-122.4233'; 

    $result = file_get_contents($url); 
    $data = json_decode($result, true); 

    echo $data['currently']['temperature']; 
+0

谢谢。我错过了file_get_contents。 –