How to display each thumbnail from the JSON string - php

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.

Related

php multidimensional array to string or table

how can I output this array into html table?
for each row I would like to output it like this, within the foreach;
echo "<td>".$lat."</td><td>".$long."</td>";
as per example on https://developer.here.com/documentation/routing-waypoints/topics/quick-start-simple-car.html
I have tried the code
$api_url = "https://wse.api.here.com/2/findsequence.json?start=Berlin-Main-Station;52.52282,13.37011&destination1=East-Side-Gallery;52.50341,13.44429&destination2=Olympiastadion;52.51293,13.24021&end=HERE-Berlin-Campus;52.53066,13.38511&mode=fastest;car&app_id=ID&app_code=CODE";
$api_response = file_get_contents($api_url);
$api_response_decoded = json_decode($api_response, true);
foreach($api_response_decoded as $api_response_decoded_row){
print_r($api_response_decoded_row[0][waypoints]);
}
and also tried
print_r($api_response_decoded_row[0][waypoints][id]);
and also tried
echo($api_response_decoded_row[0][waypoints][id]);
and also tried
implode($api_response_decoded_row[0][waypoints][id]);
Here's one way you could do it if the comments didn't already help you enough.
foreach($api_response_decoded as $api_response_decoded_rows){
foreach ($api_response_decoded_rows[0]['waypoints'] as $waypoint) {
$html = '
<td>'.$waypoint['lat'].'</td>
<td>'.$waypoint['lng'].'</td>
';
echo $html;
}
}
Thanks to commenters and answerers. In case it helps someone else, full working code is therefore;
$api_url = "https://wse.api.here.com/2/findsequence.json?start=Berlin-Main-Station;52.52282,13.37011&destination1=East-Side-Gallery;52.50341,13.44429&destination2=Olympiastadion;52.51293,13.24021&end=HERE-Berlin-Campus;52.53066,13.38511&mode=fastest;car&app_id=ID&app_code=CODE";
$api_response = file_get_contents($api_url);
$api_response_decoded = json_decode($api_response, true);
echo "<table>";
foreach($api_response_decoded as $api_response_decoded_rows){
foreach ($api_response_decoded_rows[0]['waypoints'] as $waypoint) {
$html = '<tr><td>'.$waypoint['sequence'].'</td><td>'.$waypoint['id'].'</td><td>'.$waypoint['lat'].'</td><td>'.$waypoint['lng'].'</td></tr>';
echo $html;
}
}
echo "</table>";

Facebook Graph 2.9 Have Square Images

I have a Facebook API generated feed on my website that shows the last post of whatever I posted on Facebook. In this case, it was an article I wrote about Launtua Island. When I visit my Facebook page, the image looks like this:
But when I visited my website, the image looks like this:
It appears Facebook is cropping my image, but I don't know how to prevent it. This is the code I'm using:
$feed = 'Feed Id';
$a_token = 'Secret Access Token';
$maximum = 1;
$url = "https://graph.facebook.com/v2.9/{$feed}/feed?access_token={$a_token}&limit={$maximum}";
$response = file_get_contents($url);
$facebookString = $response;
$facebookArray = json_decode($facebookString);
$array = array();
foreach ( $facebookArray->data as $each ) {
$each->message = preg_replace("/(https?:\/\/.*) ?$/", "<a target='_blank' href='$1'>$1</a>", $each->message);
$row = array();
$response = file_get_contents("https://graph.facebook.com/v2.9/{$each->id}/attachments?access_token={$a_token}");
$image = json_decode($response);
$src = $image->data[0]->media->image->src;
if ($src == '') {
$src = $image->data[0]->subattachments->data[0]->media->image->src;
}
$row['post'] = $each->message;
$row['image'] = $src;
$row['time'] = strtotime($each->created_time);
$array[] = $row;
}
$string = "";
foreach ($array as $entry) {
$string .= "<div class='post'>";
if ($entry['image'] != '') {
$string .= "<img src='{$entry['image']}'>";
}
$string .= "<p>" . $entry['post'] . "</p>";
$string .= "</div>";
}
echo $string;
There is a lot of similar posts to my problem, but I'm not sure this is the same issue (not asking for image when making request) or a different one.
It would be great if Facebook stopped changing their API all the time!
Any help would be appreciate.
Thank you.

Decoding data into HTML

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;

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;

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