How can I extract the value of this json in php? - php

I would like to know, how take the value "Tile32px" from this Json:
(http://360api.chary.us/?gamertag=superdeder) with PHP.
I tried this way:
<?php
$json = file_get_contents('http://360api.chary.us/?gamertag=superdeder');
$gamer = json_decode($json);
echo $gamer->Gamertag;
echo "<br><br>";
$data = json_decode($json, true);
$users=$data['LastPlayed'];
foreach ($users as $user)
{
echo $user['Title'];
echo "<br>";
}
echo "<br>";
$users2=$data['Pictures'];
foreach ($users2 as $user2)
{
echo $user2['Tile32px'];
echo "<br>";
}
?>
This is the result:
SuperDeder
Skyrim
Grand Theft Auto V
FIFA 13
L.A. Noire
WSOP: Full House Pro
h
h
h
Thanks a lot.
Andrea.

Try this
<?php
$json = file_get_contents('http://360api.chary.us/?gamertag=superdeder');
$gamer = json_decode($json, true);
$users = $gamer['LastPlayed'];
foreach($users as $user){
echo $user['Pictures']['Tile32px'];
echo '<br>';
}
?>

Try this way.
<?php
$json = file_get_contents('http://360api.chary.us/?gamertag=superdeder');
$data = json_decode($json, true);
foreach ($data['LastPlayed'] as $val)
{
echo $val['Pictures']['Tile32px'];
echo "<br>";
}
?>

Related

PHP rest api display an array of values

I am trying to display all the items from an API. How can i display in html or store them in variables. In my code
$api_url = 'demo API';
// Read JSON file
$json_data = file_get_contents($api_url);
// Decode JSON data into PHP array
$response_data = json_decode($json_data, true);
$result = array_values($response_data);
var_dump($result);
but unfortunately i dont know how to display the datas in front
i found a way
<?php
// JSON string
$json_string = '(API)';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
foreach($obj as $row => $innerArray){
foreach($innerArray as $innerRow => $value){
// echo $innerRow.' = '.$value . "<br/>";
if ($innerRow == "thumb_url")
{
?>
<img src="<?php echo $value; ?>" width="280" height="360">
<?php
}
elseif ($innerRow == "title"){
?>
<?php echo $value; ?>
<?php
}
}
}
?>

I want to Echo IMDB ID, Title, Quality and Embed URL From This JSON URL

Now I have this JSON URL https://vidsrc.me/movies/latest/page-1.json I want to echo IMDB ID, Title, Quality and Embed URL for each array as this sounds like a multidimensional array. But every time I try to do this. I get one of the following errors:
- Undefined Index
- Undefined offset
So I want to loop through it and echo each of those items and break line between each,
This is the code
<?php
$url = file_get_contents('https://vidsrc.me/movies/latest/page-1.json');
$decode = json_decode($url, TRUE);
$res = $decode->result;
foreach($decode as $value){
$imdb = $res->imdb_id;
$title = $res->title;
$quality = $res->quality;
$url = $res->embed_url;
echo $imdb;
echo "<br>";
echo $tite;
echo "<br>";
echo $quality;
echo "<br>";
echo $url;
}
?>
json_decode() returns you array but you process it like object, you should process it as array:
<?php
$url = file_get_contents('https://vidsrc.me/movies/latest/page-1.json');
$decode = json_decode($url, TRUE);
foreach($decode['result'] as $value){
$imdb = $value['imdb_id'];
$title = $value['title'];
$quality = $value['quality'];
$url = $value['embed_url'];
echo $imdb;
echo "<br>";
echo $title;
echo "<br>";
echo $quality;
echo "<br>";
echo $url;
}
?>

Loop JSON String response in PHP Foreach

I'm trying to print values of a JSON response in PHP. Inside the contents are:
data
local
acao
detalhes
Codes:
<?php
header('Content-type: text/html; charset=UTF-8');
$url = "http://developers.agenciaideias.com.br/correios/rastreamento/json/RJ290474594CN";
$c_url = file_get_contents($url);
$c_url = utf8_encode($c_url);
$results = json_decode($c_url);
echo $c_url;
?>
I've tried using foreach but wasn't successful in implementing them in said JSON response.
Here is a JSON String Example:
[{"data":"07\/12\/2014 11:19","local":"CHINA - CHINA\/CN","acao":"postado","detalhes":"-"},{"data":"09\/12\/2014 12:03","local":"CHINA - CHINA\/CN","acao":"encaminhado","detalhes":"Em tr\u00e2nsito para Unidade de Tratamento Internacional - BRASIL\/BR"},{"data":"29\/12\/2014 12:34","local":"UNIDADE TRAT INTERNACIONAL PARANA - Curitiba\/PR","acao":"conferido","detalhes":"Recebido\/Brasil "}]
If you just want to print values just print them accordingly using foreach. After getting the response, use json_decode(). That ut8f_encode() is superfluous.
$url = "http://developers.agenciaideias.com.br/correios/rastreamento/json/RJ290474594CN";
$data = json_decode(file_get_contents($url), true);
foreach($data as $values) {
echo $values['data'] . '<br/>';
echo $values['local'] . '<br/>';
echo $values['acao'] . '<br/>';
echo $values['detalhes'] . '<br/>';
}
Sample Output
Or:
$url = "http://developers.agenciaideias.com.br/correios/rastreamento/json/RJ290474594CN";
$data = json_decode(file_get_contents($url), true);
foreach($data as $values) {
foreach($values as $key => $value) {
echo "{$key}: $value <br/>";
}
echo '<hr/>';
}

Data from JSON to php?

I need help with fetching only attraction data,
i have this php script on my website:
<?php $json_string = 'http://framecreators.nl/efteling/data.php'; $jsondata file_get_content($json_string); $json_o=json_decode(utf8_encode($data)); $attractie = $json_o['Id']['Type']; echo $attractie ?>
and this json data:
http://framecreators.nl/efteling/data.php
I need to convert only a Id and type, ff somebody can help me.
To access the data you're looking for, you'll need to do something like this:
foreach ($json_o['AttractionInfo'] as $a) {
echo $a['Id']; //or whatever you're planning to do with this data
ech0 $a['Type'];
}
<?php
$json_data = file_get_contents('http://framecreators.nl/efteling/data.php');
$data = json_decode($json_data);
$array = [];
foreach($data->AttractionInfo as $item) {
$array['id'][] = $item->Id;
$array['type'][] = $item->Type;
}
echo "<pre>";
print_r($array);
echo "</pre>";
$url = "http://framecreators.nl/efteling/data.php";
$content = file_get_contents($url);
$data = json_decode($content,TRUE);
$array = $data['AttractionInfo'];
foreach($array as $r)
{
echo "ID->".$r['Id'];
echo '';
echo "Type->".$r['Type'];
echo '';
}
$url = "http://framecreators.nl/efteling/data.php";
$content = file_get_contents($url);
$data = json_decode($content,TRUE);
$array = $data['AttractionInfo'];
if(is_array($array))
{
foreach($array as $r)
{
echo $r['Id'];
echo $r['Type'];
}
}

json and PHP parse

Why i can't get out the values from the json file with PHP?
I get zero values? Have tried for hours.
<?php
$json_string = 'http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=44';
$jsondata = file_get_contents($json_string);
$data = json_decode($jsondata, TRUE);
print_r($data);
echo "<br><br><br><br>";
foreach ($data as $recenttrades) {
echo "VALUES('{$recenttrades->quantity}', '{$recenttrades->price}' ";
}
?>
Update: but can't get the value from primaryname and primarycode.
I have tried this:
$json_string = 'http://pubapi.cryptsy.com/api.php?method=marketdatav2';
$jsondata = file_get_contents($json_string);
$data = json_decode($jsondata, TRUE);
//print_r($data);
foreach ($data["market"] as $markets) {
echo "Primary code: <strong>{$markets['primarycode']}</strong><br>";
foreach($markets as $market) {
foreach($market as $attributes) {
foreach($attributes["recenttrades"] as $recenttrade) {
echo "quantity: " . $recenttrade['quantity'] .", price: " . $recenttrade['price'] . "<br>";
}
}
}
}
Others have mentioned that you're dealing with nested arrays, not objects. Along with pointing out the issue of being nested rather deeply, I would suggest digging down with foreach (I will probably be crucified for this):
<?php
$json_string = 'http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=44';
$jsondata = file_get_contents($json_string);
$data = json_decode($jsondata, TRUE);
//print_r($data);
echo "<br><br><br><br>";
foreach ($data as $markets) {
foreach($markets as $market) {
foreach($market as $attributes) {
foreach($attributes["recenttrades"] as $recenttrade) {
//echo "<pre>";
//print_r($recenttrade);
//echo "</pre>";
echo "VALUES('{quantity: " . $recenttrade['quantity'] ."}', 'price: {" . $recenttrade['price'] . "}')";
}
}
}
}
?>
This will ensure that you grab every recentrades item at this level of the array. This way you are prepared for other markets to be added to the API and your code isn't locked into searching only in the item named "FST".
recenttrades is nested pretty deeply in that array. Try
foreach ($data['return']['markets']['FST']['recenttrades'] as $recenttrades) {
recenttrades is several levels nested, so simply doing foreach($data as $recenttrades) is not sufficient.
You need to do:
$recentTrades = $data['return']['markets']['FST']['recenttrades'];
foreach($recentTrades as $recentTrade) {
...
}
recenttrades is nested deeply and you're asking for arrays, not objects. This seems to work:
foreach ($data['return']['markets']['FST']['recenttrades'] as $recenttrades) {
echo "VALUES('{$recenttrades['quantity']}', '{$recenttrades['price']}' ";
}
In response to your update, where you want to loop over markets, try this:
foreach ($data['return']['markets'] as $market) {
echo "Primary code: <strong>{$market['primarycode']}</strong><br>";
foreach ($market["recenttrades"] as $recenttrade) {
echo "quantity: " . $recenttrade['quantity'] .", price: " . $recenttrade['price'] . "<br>";
}
}

Categories