<?php
$feedURL = 'http://########.tumblr.com/api/read/';
$xml = simplexml_load_file($feedURL);
// $posts = $xml->xpath("/tumblr/posts/post");
foreach($xml->posts->post as $post){
$post= $xml->posts->post->{'photo-url'};
}
echo "<pre>";
print_r($post);
echo "</pre>";
?>
This is the script that I have made. I am able to fetch only the first post, but I want all posts to be displayed and the post image is not showing up.
I've got it working using
foreach($xml->posts->post as $post){
$img = (string) $post->{'photo-url'};
echo '<img src="' . $img . '" />';
}
But its only showing a couple image not all of them any ideas ?
Try this:
foreach($xml->posts->post as $post){
$post_urls[] = (string) $post->{'photo-url'};
}
echo "<pre>";
print_r($post_urls);
echo "</pre>";
Update for comment:
foreach($xml->posts->post as $post){
$posts[] = (array)$post->attributes() + (array) $post->children();
}
echo "<pre>";
print_r($posts);
echo "</pre>";
Update 2:
foreach($xml->posts->post as $post){
$img = (string) $post->{'photo-url'};
echo '<img src="' . $img . '" />';
}
Related
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;
I am using this code to get related videos. With this code I get title and link of the video. How can I get the thumb of the video?
<?php
$JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$title = $JSON_Data->{'feed'}->{'entry'};
for ($i = 1; $i < 25; $i++)
{
echo ($title[$i]->{'title'}->{'$t'}) . "<br />";
echo $title[$i]->{'link'}[0]->{'href'} . "<br /><br />";
}
?>
When using the json_decode, the best way is to print the entire result for yourself and then see the necessary data you have to get...
Do this:
`<?php
$JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$JSON_Data = json_decode($JSON);
echo '<pre>';
print_r($JSON_Data);
?>`
You'll get to know what exactly you have to extract...
I leave that to you... :)
I would :-
<?php
$json = file_get_contents("http://gdata.youtube.com/feeds/api/videos/FYpunY-gXxU/related?v=2&alt=json");
$json_data = json_decode($json, true);
foreach((array)$json_data['feed']['entry'] as $video){
echo $video['title']['$t'].'<br/>';
echo $video['link'][0]['href'].'<br/>';
?>
<img src="<?php echo $video['media$group']['media$thumbnail'][0]['url']?>" />
<img src="<?php echo $video['media$group']['media$thumbnail'][1]['url']?>" />
<?php
}
?>
I'm using the Advanced Custom Fields Repeater to pump out the image URL. But, it shows the ID, not the URL. Any help is greatly appreciated, thanks. Here is my code:
<?php
$rows = get_field('images');
foreach($rows as $row){
$image = wp_get_attachment_image_src(get_sub_field('image'), 'full');
//var_dump($row['image']);
echo '<img src="'. $row['image']['url'] . '" class="shadowed forced">';
}
?>
In response to your question, it doesn't work because get_sub_field has to be used in a while statement in conjunction with has_sub_field(), and because your $row['image']['url'] is referring to an the image object (set via return value on the field menu), while is seems as if you are actually having the field return the 'Attachment ID'.
If you want to use a foreach with the return value set to 'Attachment ID', you need to use the object/array that is being returned like so:
<?php
$rows = get_field('images');
foreach($rows as $row){
$image = wp_get_attachment_image_src($row['image'], 'full');
echo '<img src="'. $image[0] . '" class="shadowed forced">';
}
?>
If you want to use the foreach method, with the return value set to 'Image Object', it should look something like this:
<?php
$rows = get_field('images');
foreach($rows as $row){
echo '<img src="'. $row['image']['url'] . '" class="shadowed forced">';
}?>
Another solution, working of your answer/suggestion would be to set the fields return value to 'Image URL', and then you could simply call the image like so:
<?php $rows = get_field('images');
if ($rows)
{
while (has_sub_field('images'))
{
$image = get_sub_field('image');
echo '<img src="'. $image . '" class="shadowed forced">';
}
} ?>
Alternatively if you wanted to show a different size of the image, you could also set the 'Return Value' to 'Image Object', and do something like this:
<?php $rows = get_field('images');
if ($rows)
{
while (has_sub_field('images'))
{
$image = get_sub_field('image');
echo '<img src="'. $image['sizes']['large'] . '" class="shadowed forced">';
}
} ?>
Looks like what I needed to do was not use a foreach, and instead just use and if & while statement. I was calling in the repeater, not the subfield with the old code. Here's a reference if anyone runs into this issue -
<?php
$rows = get_field('images');
if ($rows)
{
while (has_sub_field('images'))
{
$image = wp_get_attachment_image_src(get_sub_field('image'), 'full');
echo '<img src="'. $image[0] . '" class="shadowed forced">';
}
}
?>
I have an array in the following format.
$data = array(
1=>array('img'=>'1.png','title'=>'title1','desc'=>'desc1'),
2=>array('img'=>'2.png','title'=>'title2','desc'=>'desc2'),
1=>array('img'=>'3.png','title'=>'title3','desc'=>'desc3'),
);
Here is the final output I need,
<img src="1.png">
<h1>title1</h1>
<p>desc1</p>
<img src="2.png">
<h1>title2</h1>
<p>desc2</p>
.........
How can i create it? Thanks for the help.
Use a foreach loop, like so:
foreach( $data as $item) {
echo '<img src="' . $item['img'] . '">';
echo '<h1>' . $item['title'] . '</h1>';
echo '<p>' . $item['desc'] . '</p>';
echo "\n";
}
Simply use your array like
foreach($data as $item){
echo $item['title'];
.....
}
This will give you the logic. Now you can apply the img and tags properly
<?php
foreach($data as $item) {
?>
<img src="<?=$item['img']?>">
<h1><?=$item['title']?></h1>
<p><?=$item['desc']?></p>
<br />
<?php } ?>
Another option here.
My goal is to embed Tumblr posts into a website using their provided XML. The problem is that Tumblr saves 6 different sizes of each image you post. My code below will get the first image, but it happens to be too large. How can I select one of the smaller-sized photos out of the XML if all the photos have the same tag of <photo-url>?
→ This is the XML from my Tumblr that I'm using: Tumblr XML.
→ This is my PHP code so far:
<?php
$request_url = "http://kthornbloom.tumblr.com/api/read?type=photo";
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{'photo-caption'};
$photo = $xml->posts->post->{'photo-url'};
echo '<h1>'.$title.'</h1>';
echo '<img src="'.$photo.'"/>"';
echo "…";
echo "</br><a target=frame2 href='".$link."'>Read More</a>";
?>
The function getPhoto takes an array of $photos and a $desiredWidth. It returns the photo whose max-width is (1) closest to and (2) less than or equal to $desiredWidth. You can adapt the function to fit your needs. The important things to note are:
$xml->posts->post->{'photo-url'} is an array.
$photo['max-width'] accesses the max-width attribute on the <photo> tag.
I used echo '<pre>'; print_r($xml->posts->post); echo '</pre>'; to find out $xml->posts->post->{'photo-url'} was an array.
I found the syntax for accessing attributes (e.g., $photo['max-width']) at the documentation for SimpleXMLElement.
function getPhoto($photos, $desiredWidth) {
$currentPhoto = NULL;
$currentDelta = PHP_INT_MAX;
foreach ($photos as $photo) {
$delta = abs($desiredWidth - $photo['max-width']);
if ($photo['max-width'] <= $desiredWidth && $delta < $currentDelta) {
$currentPhoto = $photo;
$currentDelta = $delta;
}
}
return $currentPhoto;
}
$request_url = "http://kthornbloom.tumblr.com/api/read?type=photo";
$xml = simplexml_load_file($request_url);
foreach ($xml->posts->post as $post) {
echo '<h1>'.$post->{'photo-caption'}.'</h1>';
echo '<img src="'.getPhoto($post->{'photo-url'}, 450).'"/>"';
echo "...";
echo "</br><a target=frame2 href='".$post['url']."'>Read More</a>";
}
To get the photo with max-width="100":
$xml = simplexml_load_file('tumblr.xml');
echo '<h1>'.$xml->posts->post->{'photo-caption'}.'</h1>';
foreach($xml->posts->post->{'photo-url'} as $url) {
if ($url->attributes() == '100')
echo '<img src="'.$url.'" />';
}
Maybe this:
$doc = simplexml_load_file(
'http://kthornbloom.tumblr.com/api/read?type=photo'
);
foreach ($doc->posts->post as $post) {
foreach ($post->{'photo-url'} as $photo_url) {
echo $photo_url;
echo "\n";
}
}