Reputation: 305
I'd like to get specific value from JSON.
URL: https://api.darksky.net/forecast/92cf27941c6ea888652ba37de4da4044/37.8267,-122.4233
I am trying like below
<?php
$data = json_decode('https://api.darksky.net/forecast/92cf27941c6ea888652ba37de4da4044/37.8267,-122.4233', true);
echo $data['currently']['temperature'];
?>
But it is not working. Can anyone help me on it? I'd like to get currently > temperature value only.
Upvotes: 0
Views: 334
Reputation: 2495
You need to get contents of page first using curl or file_get_contents. Try Following Code
$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'];
Upvotes: 1
Reputation: 16436
You have to do following changes json_decode('https://api.darksky.net/forecast/92cf27941c6ea888652ba37de4da4044/37.8267,-122.4233', true);
will try to decode url string it will not decode result. For that you have to execute this 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'];
Upvotes: 1
Reputation: 1382
You can't read file directly without using curl or file_get_contents
Snippet below to get data
<?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'];
?>
Upvotes: 2
Reputation: 1265
Please try this
<?php
$data = json_decode(file_get_contents('https://api.darksky.net/forecast/92cf27941c6ea888652ba37de4da4044/37.8267,-122.4233', true));
echo $data->currently->temperature;
?>
Upvotes: 2
Reputation: 403
You can't call json url directly. You need a file function for this. Here is a sample.
$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>";
Upvotes: 2