Decoding data into HTML - php

I have a URL that returns JSON data and is constantly being updated. I'm trying to get the contents of this data to show up on my site. Here is JSON file (url)
I only need the contents from the 'images' (pretty far down there) array ... when I try and use just simple php code:
$json_data = file_get_contents($url);
$json = json_decode($str, true);
$result = array();
foreach($json['designs']['images'] as $image); {
$result[] = $image['url'];
}
echo $result;
I just keep returning 'Array' on the browser. If I replace echo with print_r I get this: Array ( [0] => SWZ51F ) which is correct, but i would like it to display in HTML format.

After the loop, $result is an array of all image URLs. You can loop through it to output the html. A basic approach would be :
foreach ($result as $url) {
echo '<img src="' . $url . '"/><br/>';
}
Or use a single loop:
foreach($json['designs']['images'] as $image); {
echo '<img src="' . $image['url'] . '"/><br/>';
}

Try this:
This is for ALL IMAGES URL:
$result = array();
foreach($json['designs']['images'] as $image){
$result[] = $image['url'];
}
var_dump($result);
This is for single IMAGE URL:
$result=$json['designs']['images'][0]['url'];
echo $result;

Related

How to display each thumbnail from the JSON string

So this may end up being a simple question but I have some code that looks at a JSON string and currently returns the image at array point 0 but I would like it to iterate for all the "hits" for this example there are 15 hits.
I imagine its a for each loop but i've searched and tried and cant figure it out. Any help would be greatly appreciated.
Here is my code
$content = curl_exec($ch);
$data = json_decode($content);
$jim = $data->hits[0]->thumbnailUrl;
echo "<img src='" . $jim . "' alt='error'>";
currently this code displays one image but I would like it to display for all the images its hits on. I can manually change which image it displays by changing the value hits[0] to be hits[2] for example.
Simple foreach will do:
$content = curl_exec($ch);
$data = json_decode($content);
foreach ($data->hits as $thumb) {
printf('<img src="%s" alt="error">', $thumb->thumbnailUrl);
}
$content = curl_exec($ch);
$data = json_decode($content);
$hits = $data->hits;
foreach ($hits as $hit)
{
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
}
You can try this with a foreach loop to loop through your JSON data.

how to echo fixer.io json php

Hi I'm trying get a json from fixer.io and then for each rates echo it but cant get it to work.
the code are
<?php
function usd(){
echo 'HEJ test';
$fixer_access_key = my_access_key;
$url= 'https://data.fixer.io/api/latest?access_key=' . $fixer_access_key;
echo $url;
$json = file_get_contents($url);
$data = json_decode($json);
echo $url . "<br>";
echo 'printing json foreach <br>';
foreach($data as $obj){
echo '...';
$prefix = $obj;
echo $prefix;
echo '<br>';}
echo 'done printing json foreach';
}
usd(); ?>
and the result are:
https://data.fixer.io/api/latest?access_key=my_fixer_key
printing json foreach
done printing json foreach
instead of
$data = json_decode($json);
use
$data = json_decode($json, true);
This should allow foreacha to works - however you will only see first level of json object keys (not nested ones). The second parameter of json_decode change result from object to array.
You will also need to change foreach - to following: foreach($data as $key => $obj) and inside it echo $obj to echo $key;.
Here is simplified working example.
ALTERNATIVE SOLUTION
If working foreach is not your goal but rather pretty printed json, then instead use following code:
$json_string = json_encode($data, JSON_PRETTY_PRINT);
echo $json_string;

Echo JSON PHP array WIKI API

I don't know who to export API
So the URL is:
https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&pithumbsize=200&titles=Jean-Claude%20Van%20Damme
And I get
{"batchcomplete":"","query":{"pages":{"89265": {"pageid":89265,"ns":0,"title":"Jean-Claude Van Damme","thumbnail":{"source":"https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Jean-Claude_Van_Damme_2012.jpg/141px-Jean-Claude_Van_Damme_2012.jpg","width":141,"height":200},"pageimage":"Jean-Claude_Van_Damme_2012.jpg"}}}}
Who can I only export the source?
Now I have this code:
$json=file_get_contents("https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&pithumbsize=200&titles=$title");
$details=json_decode($json);
echo $details['thumbnail']['source'];
So who I do?
You could use json_decode and pass true as the second parameter.
From the documentation:
When TRUE, returned objects will be converted into associative arrays.
Then I think what you are looking for is this:
$json=file_get_contents("https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&pithumbsize=200&titles=Jean-Claude%20Van%20Damme");
$details=json_decode($json, true);
$source = $details['query']['pages']['89265']['thumbnail']['source'];
echo $source;
Will result in:
https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Jean-Claude_Van_Damme_2012.jpg/141px-Jean-Claude_Van_Damme_2012.jpg
Try this.
$json = file_get_contents("https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&pithumbsize=200&titles=Jean-Claude%20Van%20Damme");
$details = json_decode($json);
$pages = $details->query->pages;
$output = '';
foreach ($pages as $pageid => $content){
$image_url = $content->thumbnail->source;
$title = $content->title;
$output .= '<img src="'.$image_url.'"/>'.$title;
}
echo $output;

How to parse json in php from url

Im new to json, so i gotta ask you a maybe really simple questions.
I've tried searching around but have not found anything I can get to work.
I have called an API and received the data in json.
And now comes my problem parsing it through my php, it "will not find anything."
My code looks like this:
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept: application/vnd.travify.v1+json\r\n"
)
);
$context = stream_context_create($opts);
$url = 'http://oda.ft.dk/api/Sag?$select=titel,Sagskategori/kategori&$expand=Sagskategori';
$output = file_get_contents($url, false, $context);
$string = file_get_contents($url, false, $context);
$result = json_decode($string, true);
$i = -1;
foreach ($result as $data) {
$i++;
echo "#";
echo $i;
echo "<br>";
echo "<b>Test 1:</b>";
echo "<br>";
if(!empty($result[$i]['value']['Sagskategori']['kategori'])){
echo $result[$i]['value']['Sagskategori']['kategori'];
}else{
echo "Intet fundet.";
}
echo "<hr>";
}
The json code can be find here: http://oda.ft.dk/api/Sag?$select=titel,Sagskategori/kategori&$expand=Sagskategori
Can anyone of you see my fail in the code, and get me on the right way :-) ?
Please replace
foreach ($result as $data) {
by
foreach ($result["value"] as $data) {
And now you can iterate to your value array and get all informations from $data
You don't need use $i, $data contains correct $result[$i] value
foreach ($result["value"] as $data) {
echo "#";
echo "<br>";
echo "<b>Test 1:</b>";
echo "<br>";
if(!empty($data['Sagskategori']['kategori'])){
echo $data['Sagskategori']['kategori'];
}else{
echo "Intet fundet.";
}
echo "<hr>";
}
The JSON starts like this:
{
"odata.metadata":"...snip...","value":[
{
So the array is inside the value object.
The correct code should be:
foreach ($result['value'] as $data) {
// snip
if(!empty($result['value'][$i]['Sagskategori']['kategori'])){
echo $result['value'][$i]['Sagskategori']['kategori'];
}
Also, inside the loop, $result['value'][$i]['Sagskategori']['kategori']; is strictly the same as using $data['Sagskategori']['kategori'];.

json get the right code

Currently trying to get this code to print everything in the array but there is a small
problem.
$json = file_get_contents('https://api.twitch.tv/kraken/channels/zettslive/follows?limit=10&offset=0');
$vid_arr = json_decode($json,true);
$vid_count = count($vid_arr);
foreach ($vid_arr as $vid)
{
echo '<img src="'.$vid[1]['user']['logo'].'">';
}
I have to use the [1] to even get it to work. and I want to be able to echo them all. and not do them one at a time.
$json = file_get_contents('https://api.twitch.tv/kraken/channels/zettslive/follows?limit=10&offset=0');
$vid_arr = json_decode($json,true);
$vid_count = count($vid_arr);
foreach ($vid_arr['follows'] as $follow)
{
echo '<img src="'.$follow['user']['logo'].'">';
}

Categories