2017-08-30 142 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。 –