So i've got a list with local weather details, http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl. And I want to display some of that in formation via php on my site, but can't really find out how JSON is something completely new for me.
And the only thing i managed to do right now is this: http://jeroenonline.biz/JSON/index.php. So this is a simple script:
$getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");
$decode = json_decode($getData);
echo "<pre>";
print_r($decode);
using the link
http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl
without the "." gives me a response
{
"message": "Error: Not found city",
"cod": "404"
}
<?php
$getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");
$decode = json_decode($getData);
// accessing it through object
echo $decode->message;
echo "<br/>";
echo $decode->cod;
// accessit via array
// set true the second parameter or the json_decode($encoded_data, TRUE)
// to give you array
$decode = json_decode($getData, TRUE);
echo "<br/>";
echo $decode['message'];
echo "<br/>";
echo $decode['cod'];
so when use the link with the "."
http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl.
gives a response of :
{
"coord": {
"lon": 5.83,
"lat": 50.91
},
"sys": {
"message": 0.0287,
"country": "Netherlands",
"sunrise": 1430884846,
"sunset": 1430939149
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "Sky is Clear",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 284.923,
"temp_min": 284.923,
"temp_max": 284.923,
"pressure": 1012.18,
"sea_level": 1023.56,
"grnd_level": 1012.18,
"humidity": 67
},
"wind": {
"speed": 6.06,
"deg": 219.002
},
"clouds": {
"all": 0
},
"dt": 1430875602,
"id": 0,
"name": "Nuth",
"cod": 200
}
to show the the result
// sample to access coord
echo $decode->coord->lon;
echo $decode->coord->lat;
// sample to access sys
echo $decode->sys->message;
echo $decode->sys->country;
// sample to access weather
echo $decode->weather[0]->id;
echo $decode->weather[0]->main;
echo $decode->weather[0]->description;
// sample to access main
echo $decode->main->temp;
echo $decode->main->temp_min;
// sample to access wind
echo $decode->wind->speed;
// sample to access clouds
echo $decode->clouds->all;
echo $decode->id;
echo $decode->name;
echo $decode->cod;
If you want to use the JSON as an array instead, you just have to pass true as the second paramenter for json_decode.
$getData = file_get_contents( "http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl");
$decode = json_decode($getData, true);
echo "<pre>";
print_r($decode);
this API http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl. get some data like this
{
"coord": {
"lon": 5.83,
"lat": 50.91
},
"sys": {
"message": 0.039,
"country": "Netherlands",
"sunrise": 1430884846,
"sunset": 1430939149
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "Sky is Clear",
"icon": "01n"
}
],
"base": "stations",
"main": {
"temp": 284.923,
"temp_min": 284.923,
"temp_max": 284.923,
"pressure": 1012.18,
"sea_level": 1023.56,
"grnd_level": 1012.18,
"humidity": 67
},
"wind": {
"speed": 6.06,
"deg": 219.002
},
"clouds": {
"all": 0
},
"dt": 1430875157,
"id": 2749752,
"name": "Nuth",
"cod": 200
}
try this
<?php
$getData = file_get_contents("http://api.openweathermap.org/data/2.5/weather?q=Schimmert,nl."); // get your json data
$decode = json_decode($getData); // decode it to be an object
// echo "<pre>";
// print_r($decode);
// you can get the data with
echo "Country : " . $decode->sys->country; echo '<br>';
for($i=0;$i<count($decode->weather);$i++){
echo "ID : " . $decode->weather[0]->id; echo '<br>';
echo "Weather : " . $decode->weather[0]->main; echo '<br>';
echo "Description : " . $decode->weather[0]->description; echo '<br>';
echo "Icon : " . $decode->weather[0]->icon; echo '<br>';
}
echo "Temperature : " . $decode->main->temp; echo '<br>';
echo "Temp Min : " . $decode->main->temp_min; echo '<br>';
echo "Temp Max : " . $decode->main->temp_max; echo '<br>';
echo "Preassure : " . $decode->main->pressure; echo '<br>';
echo "Sea Level : " . $decode->main->sea_level; echo '<br>';
echo "Ground Level : " . $decode->main->grnd_level; echo '<br>';
echo "Humidity : " . $decode->main->humidity; echo '<br>';
echo "Wind Speed : " . $decode->wind->speed; echo '<br>';
echo "Wind Degrees : " . $decode->wind->deg; echo '<br>';
echo "Cloud : " . $decode->clouds->all; echo '<br>';
?>
Related
I am working on a small projet with a specific JSON API
I got this JSON from response of the API.
{
"3": {
"email": "email#domain.com",
"ext": "100",
"location": "remote",
"name": "Test Extension",
"protocol": "sip",
"status": "enabled",
},
"66": {
"email": "support#domain.com",
"ext": "101",
"location": "remote",
"name": "Test2",
"protocol": "sip",
"status": "enabled",
}
}
I want to get result like that:
Extension: 100
Nom: Test Extension
ID: 3
Status: enabled
Extension: 101
Nom: Test2
ID:66
Status: enabled
I cannot find the way to get the ID for each of the array...
My code is :
$json = file_get_contents($list);
$queries = json_decode($json);
foreach($queries as $query){
echo "<br>Extension: ";
echo $query->ext;
echo "<br>Nom: ";
echo $query->name;
echo "<br>";
echo "ID:";
echo $id;
echo "<br>";
echo " Status: ";
echo $query->status;
echo "<br>";
}
using foreach itself you can get index
foreach($queries as $id => $query){
echo "<br>Extension: ";
echo $query->ext;
echo "<br>Nom: ";
echo $query->name;
echo "<br>";
echo "ID:";
echo $id;
echo "<br>";
echo " Status: ";
echo $query->status;
echo "<br>";
}
I want each result to have its own unique URL like this:
https:/MySite.com/file_code/Interesting-video-title
https:/MySite.com/file_code/Interesting-video-title
It's possible ?
My goal is that every result can appear on google!
"ps: I'm a very beginner in the code"
<?php
error_reporting(0);
$json = '{ "message": "OK",
"result": {
"files": [
{
"code": "FILE_CODE",
"id": "FILE_ID",
"name": "Interesting video title",
"size": "215.7 MB",
"uploaded": "2019-11-07"
},
{
"code": "FILE_CODE",
"id": "FILE_ID",
"name": "Interesting video title",
"size": "215.7 MB",
"uploaded": "2019-11-07"
}
],
"pagination": {
"current_page": 1,
"first_page": 1,
"last_page": 1,
"next_page": "",
"previous_page": "",
"total": 2
},
"total": 2
},
"success": 1
}';
$data_arrays = json_decode($json, true);
foreach($data_arrays as $data){
foreach($data['files'] as $result){
echo "CODE: ".$result['code']."\n";
echo "ID: ".$result['id']."\n";
echo "NAME: ".$result['name']."\n";
echo "SIZE: ".$result['size']."\n";
echo "UPLOADED: ".$result['uploaded']."\n";
echo "----------\n";
#if($results){
# echo "<pre>";
# print_r($results);
#}
}
}
?>
I'd like to get the value of array from multi dimensional array. Let say I have json response from API which I decoded using json_decode() function in php and I want using loop to iterate the data.
$string = $response;
$json_a = json_decode($string, true);
foreach($json_a['withdrawals'] as $item) {
echo $item['withdrawalId']['amount']['currencyCode']. '<br/>';
echo $item['withdrawalId']['terminal']['tid']. '<br/>';
echo $item['withdrawalId']['user']['mid']. '<br/>';
echo $item['servicefee']['masterMerchant']['name']. '<br/>';
echo $item['servicefee']['masterMerchant']['address']['address1']. '<br/>';
}
}
print_r($json_a);
and here's the print out of arrays.
{
"limit": 1,
"offset": 0,
"totalTransactionsCount": 5040,
"acceptedTotalTransactionsCount": 4428,
"acceptedTotalTransactionsValue": 2438928.04,
"rejectedTotalTransactionsCount": 612,
"rejectedTotalTransactionsValue": 499294.04,
"withdrawals": [
{
"withdrawalId": "764353634316",
"status": "approval",
"dateTime": "2016-04-28T09:31:38.145Z",
"localDateTime": "2016-04-28 17:31:38",
"accountType": "CURRENT",
"additionalInfo": "approved",
"batch": "000029",
"smscount": 0
"amount": {
"currencyCode": "EUR",
"value": "399.99"
},
"terminal": {
"tid": "88133332",
"serialNumber": "45435435"
},
"user": {
"name": "John Doe",
"email": "jmeter#joe.com",
"phoneNumber": "546546546",
"role": "OPERATOR",
"subMerchant": {
"name": "Submerchant XXY",
"mid": "MID0000"
"serviceFee": {
"name": "rsf",
"merchantFee": "10.0"
},
"masterMerchant": {
"name": "Master Merchant",
"address": {
"address1": "Mainstreet",
"city": "Cork",
"postcode": "Cork",
"country": {
"countryName": "Philippines",
"countryCode": "PH",
"currencyName": "Philippines Peso",
"currencyCode": "PHP",
}
}
},
"address": {
"address1": "Mainstreet",
"city": "Cork",
"postcode": "Cork",
"country": {
"countryName": "Philippines",
"countryCode": "PH",
"currencyName": "Philippines Peso",
"currencyCode": "PHP",
}
}
}
}
}]}
Write it like this:-
$string = $response;
$json_a = json_decode($string, true);
foreach($json_a['withdrawals'] as $item) {
echo $item['withdrawalId']. '<br/>';
echo $item['amount']['currencyCode']. '<br/>';
echo $item['terminal']['tid']. '<br/>';
echo $item['user']['mid']. '<br/>';
echo $item['user']['masterMerchant']['name']. '<br/>';
echo $item['user']['masterMerchant']['address']['address1']. '<br/>';
}
}
print_r($json_a);
I need some help accessing an api. http://market.huobi.com/staticmarket/detail.html
in PHP.
$json = file_get_contents("http://market.huobi.com/staticmarket/detail.html");
$obj = json_decode($json);
Below is a small sample of the api response.
$obj =
{
"sells": [
{
"price": 3840,
"level": 0,
"amount": 1
},
{
"price": "3840.05",
"level": 0,
"amount": 0.287
},
{
"price": 3841,
"level": 0,
"amount": 0.1
} ],
"p_new": 3838,
"level": -72.12,
"amount": 82792,
"total": 321774060.34653,
"amp": -2,
"p_open": 3910.12,
"p_high": 3925,
"p_low": 3809.99,
"p_last": 3910.12
}
echo "Ask " . $obj->sells[0]->price; // does not work
echo "Volume" . $obj->amount;// does not work
Help will be appreciated.
The API you're calling is returning JSONP data, not JSON data.
JSONP data looks like:
somefunction(JSONdata)
You need to remove the function call wrapper.
$jsonp = file_get_contents("http://market.huobi.com/staticmarket/detail.html");
preg_match('/^.*?\((.*)\)/s', $jsonp, $match);
$json = $match[1];
$obj = json_decode($json);
echo "Ask " . $obj->sells[0]->price . '<br>';
echo "Volume " . $obj->amount;
I am using the following to retrieve comments from FB:
$url = urlencode("http://*****.com/pages/view?id=2153&item=Mens-Collection-Shoes");
$request_url ="https://graph.facebook.com/comments/?ids=" . $url;
$requests = file_get_contents($request_url);
when I print_r($requests); I get the following but cannot work out how to format it with a foreach loop:
{
"http:\/\/*****.com\/pages\/view?id=2153&item=Mens-Collection-Shoes": {
"comments": {
"data": [{
"id": "123456",
"from": {
"name": "Laurelle",
"id": "123456"
},
"message": "I love these",
"can_remove": false,
"created_time": "2012-11-20T10:20:16+0000",
"like_count": 0,
"user_likes": false
} {
"id": "123456",
"from": {
"name": "Dan",
"id": "123456"
},
"message": "I know, just stunning!",
"can_remove": false,
"created_time": "2012-11-20T14:24:15+0000",
"like_count": 0,
"user_likes": false
}],
"paging": {
"next": "https:\/\/graph.facebook.com\/*****\/comments?limit=25&offset=25&__after_id=*****"
}
}
}
}
I'm trying to get:
data->from->id
data->from->name
data->message
data->created_time
I've tried:
$fb_response = json_decode($requests);
foreach($fb_response->data as $item){
echo $item->from->id . '<br />';
echo $item->from->name . '<br />';
echo $item->message . '<br />';
echo $item->created_time . '<br /><br />';
}
But no luck
I think you're looking for:
$originalUrl = "http://*****.com/pages/view?id=2153&item=Mens-Collection-Shoes";
$url = urlencode($originalUrl);
...
foreach($fb_response->$originalUrl->comments->data as $item) {
data isn't the name of a property directly inside the JSON document; it's under http://*****.com/pages/view?id=2153&item=Mens-Collection-Shoes and comments.