How to format a json from an URL on php? [duplicate] - php

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I got a JSON from an URL like this:
http://myweb.com/ws_clc/ws_info.php?uid=77317
JSON:
{
"status":1,
"info":[
{
"uid":"77317",
"name":"Jack",
"lastname":"Black",
"email":"jackblack#mail.com"
}
]
}
I need to format that JSON on HTML.
¿How can I do that?
THANKS!!! I did it like this:
<?php
$uid =$_GET['uid'];
$json = file_get_contents('http://myweb.com/ws_clc/ws_info.php?uid='.$uid);
$obj = json_decode($json);
$uid= $obj->info[0]->uid;
$name = $obj->info[0]->name;
$lastname = $obj->info[0]->lastname;
echo $name;
echo $lastname;
?>

Related

I got this kind of JSON data, how can I extract one-by-one in PHP? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
The Json
data ={
"2":
{
"routtype":"Transactional",
"credit":40000,
"validity":"4616",
"availablecredit":"39457"
}
}
how to extract this data into variable one by one
assuming $json_array as your array.
$data = json_decode($json_array);
foreach($data as $dt){
$routtype = $dt->routtype;
$credit = $dt->credit;
$validity = $dt->validity;
$availablecredit = $dt->availablecredit;
echo $routtype;
echo $credit;
echo $validity;
echo $availablecredit;
}

How to get description in this json format using php? [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 5 years ago.
How to get description in this json format using php ?
This is json data
{
"items": [
{
"id": "DHHlQxea3Pg",
"snippet": {
"description": "Private Jet trip to the Rocky Mountains 1080p"
}
}
]
}
and this is my code
$curl_json_video_description = curl_init('some url');
curl_setopt($curl_json_video_description, CURLOPT_RETURNTRANSFER, true);
$json_video_description = curl_exec($curl_json_video_description);
$result_json_video_description = json_decode($json_video_description);
$video_description = $result_json_video_description->items->snippet->description;
echo $video_description;
When i test my code it's not echo anything, how can i do for get description in json ?
You are missing 0 item key, because items is array of items.
Use $result_json_video_description->items[0]->snippet->description
OR Decode it as array json_decode($json, true) and then:
$result_json_video_description = json_decode();
$result_json_video_description['items'][0]['snippet']['description']
This code works for me :)
$content = file_get_contents('url');
$jsonize = json_decode($content, true);
echo $jsonize['items'][0]['snippet']['description'];

How to extract json by php [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
How to extract json
{"scoreboard":{"lastUpdatedOn":"2017-08-21 6:58:49 PM","gameScore":[{"game":{"ID":"40380","week":"1","scheduleStatus":"Normal","originalDate":null,"originalTime":null,"delayedOrPostponedReason":null,"date":"2017-09-11","time":"7:10PM","awayTeam":{"ID":"70","City":"New Orleans","Name":"Saints","Abbreviation":"NO"},"homeTeam":{"ID":"63","City":"Minnesota","Name":"Vikings","Abbreviation":"MIN"},"location":"TCF Bank Stadium"},"isUnplayed":"true","isInProgress":"false","isCompleted":"false","quarterSummary":null}
How to extract above json by php
$allnfls = $nfls['scoreboard']['gameScore'];
foreach($allnfls as $allnfl)
{
//foreach($allnfld as $allnfl) {
$gameid = $allnfl['ID'];
echo "<br>".$week = $allnfl['game']['week'];
}
Define a variable and use json_decode(YourJSONString,true)

How can I access the name value in nested JSON using PHP? [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 6 years ago.
JSON:
{"location":{"name":"Tirana","region":"Tirane","country":"Albania","lat":41.33,"lon":19.82,"tz_id":"Europe/Tirane","localtime_epoch":1484543668,"localtime":"2017-01-16 5:14"},"current":{"last_updated_epoch":1484543668,"last_updated":"2017-01-16 05:14","temp_c":4.0,"temp_f":39.2,"is_day":0,"condition":{"text":"Overcast","icon":"//cdn.apixu.com/weather/64x64/night/122.png","code":1009},"wind_mph":6.9,"wind_kph":11.2,"wind_degree":150,"wind_dir":"SSE","pressure_mb":1009.0,"pressure_in":30.3,"precip_mm":0.0,"precip_in":0.0,"humidity":60,"cloud":0,"feelslike_c":1.2,"feelslike_f":34.2}}
PHP:
$response = file_get_contents('http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Paris');
var_dump(json_decode($response));
echo $response->location[0]->name;
API Call: http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Tirana
Try like this.You get the contents in json format.Use json_decode() with second parameter true to convert into array.
<?php
$json = '{"location":{"name":"Tirana","region":"Tirane","country":"Albania","lat":41.33,"lon":19.82,"tz_id":"Europe/Tirane","localtime_epoch":1484543668,"localtime":"2017-01-16 5:14"},"current":{"last_updated_epoch":1484543668,"last_updated":"2017-01-16 05:14","temp_c":4.0,"temp_f":39.2,"is_day":0,"condition":{"text":"Overcast","icon":"//cdn.apixu.com/weather/64x64/night/122.png","code":1009},"wind_mph":6.9,"wind_kph":11.2,"wind_degree":150,"wind_dir":"SSE","pressure_mb":1009.0,"pressure_in":30.3,"precip_mm":0.0,"precip_in":0.0,"humidity":60,"cloud":0,"feelslike_c":1.2,"feelslike_f":34.2}}
';
$array = json_decode($json,true);
//print_r($array);
$location = $array['location'];
echo $location['name'];
?>
use json_decode to parse the json string to array, then access it with the index.
the demo
$response = file_get_contents('http://api.apixu.com/v1/current.json?key=a54959ce2b294134bda34330171601&q=Paris');
$array = json_decode($response, true);
echo $array['location']['name'];

Want to get one value from json result using php [duplicate]

This question already has answers here:
Get data from JSON file with PHP [duplicate]
(3 answers)
Closed 7 years ago.
How can i get ID value only?
like so here: $fo = "11890408";
{"success":true,"result":{"success":[{"id":"11890408","device_id":"17183","message":"okey","status":"pending","send_at":1455046054,"queued_at":0,"sent_at":0,"delivered_at":0,"expires_at":1455049654,"canceled_at":0,"failed_at":0,"received_at":0,"error":"","created_at":1455046054,"contact":{"id":"2522330","name":"923336458112","number":"923336458112"}}],"fails":[]}}
Convert the json string to a php data structure and then navigate through the data tree of objects and arrays:
$json = '{
"success":true,
"result":{
"success[
{
"id":"11890408",
"device_id":"17183",
"message":"okey",
"status":"pending",
"send_at":1455046054,
"queued_at":0,
"sent_at":0,
"delivered_at":0,
"expires_at":1455049654,
"canceled_at":0,
"failed_at":0,
"received_at":0,
"error":"",
"created_at":1455046054,
"contact":{
"id":"2522330",
"name":"923336458112",
"number":"923336458112"
}
}
],
"fails":[]
}
}';
$data = json_decode($json);
$fo = $data->result->success[0]->id;

Categories