Jump to content

Recommended Posts

I've found this script which pulls my tumblr posts and displays them using php, it's working fine but I'm not sure how to get it to pull all images from a photo set, it only seems to pull the first image from each post. I've put up a couple of quick tumblr posts to try and give a better explanation.

As you can see, there are 3 images in the tumblr blog but I am only able to pull the first one using this script. I've included the code below.

http://smashtau.tumblr.com

 

 

<?php
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://smashtau.tumblr.com/api/read/json");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($ch);
curl_close($ch);

$result = str_replace("var tumblr_api_read = ","",$result);
$result = str_replace(';','',$result);
$result = str_replace('\u00a0',' ',$result);

$jsondata = json_decode($result,true);
$posts = $jsondata['posts'];
//var_dump($posts);
$blogroll = "";
foreach($posts as $post){
 $blogroll .= "<p>";
 $date = date('d.m.Y',$post['unix-timestamp']);
 $blogroll .= $date."<br />";
 switch ($post['type']) {
  case 'photo':
   $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>Permalink</a>";
   $blogroll .= "<p>".$post['photo-caption']."</p>";
   $blogroll .= "<img src=".$post['photo-url-250'].">";  
             break;


  case 'link':
   $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".$post['link-text']."</a><br />";
   $blogroll .= substr(strip_tags($post['link-description']),0,100)."...";
   break;

  case 'regular':
   $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>Permalink</a>";
   $blogroll .= "<h2>".$post['regular-title']."</h2>";
   $blogroll .= "<p>".$post['regular-body']."</p>";
   break;

  case 'quote':
   $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
   $blogroll .= substr(strip_tags($post['quote-text']),0,100)."...";
   break;

  case 'conversation':
   if(empty($post['conversation-title'])){
 $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
   }else{
 $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".$post['conversation-title']."</a><br />";
   }
   $blogroll .= substr(nl2br($post['conversation-text']),0,100);
   break;

  case 'audio':
   if(empty($post['audio-caption'])){
 $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
   }else{
 $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".strip_tags($post['audio-caption'])."</a><br />";
   }
   break;

  default:
   $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a>";
   break;
 }
 $blogroll .= "</p>";
}

echo $blogroll;
?>

Link to comment
https://forums.phpfreaks.com/topic/270640-tumblr-php-blog-using-json/
Share on other sites

I am not entirely sure,I can see there is "photos" which looks like where I should be getting this data from but with virtually no knowledge of php I have no idea how I would go about grabbing photos>photo-url-250 from each offset 01,02,03,04,05 etc.

 


[{"id":35641382789,"url":"http:\/\/smashtau.tumblr.com\/post\/35641382789","url-with-slug":"http:\/\/smashtau.tumblr.com\/post\/35641382789\/here-are-three-different-coloured-squares-amazing","type":"photo","date-gmt":"2012-11-13 17:04:00 GMT","date":"Tue, 13 Nov 2012 12:04:00","bookmarklet":0,"mobile":0,"feed-item":"","from-feed-id":0,"unix-timestamp":1352826240,"format":"html","reblog-key":"T0QafW59","slug":"here-are-three-different-coloured-squares-amazing",

"photo-caption":"<p>Here are three different\u00a0coloured\u00a0squares, amazing right<\/p>",
"width":"692","height":"812",
"photo-url-1280":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_1280.png",
"photo-url-500":"http:\/\/25.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_500.png",
"photo-url-400":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_400.png",
"photo-url-250":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_250.png",
"photo-url-100":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_100.png",
"photo-url-75":"http:\/\/25.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_75sq.png","

photos":[
{"offset":"o1","caption":"green square","width":"692","height":"812","
photo-url-1280":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_1280.png",
"photo-url-500":"http:\/\/25.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_500.png",
"photo-url-400":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_400.png",
"photo-url-250":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_250.png",
"photo-url-100":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_100.png",
"photo-url-75":"http:\/\/25.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo1_75sq.png"},

{"offset":"o2","caption":"red square","width":"692","height":"812","photo-url-1280":"http:\/\/25.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo2_1280.png",
"photo-url-500":"http:\/\/25.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo2_500.png",
"photo-url-400":"http:\/\/25.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo2_400.png",
"photo-url-250":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo2_250.png",
"photo-url-100":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo2_100.png",
"photo-url-75":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo2_75sq.png"},

{"offset":"o3","caption":"blue square","width":"692","height":"812","photo-url-1280":"http:\/\/25.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo3_r1_1280.png",
"photo-url-500":"http:\/\/25.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo3_r1_500.png",
"photo-url-400":"http:\/\/25.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo3_r1_400.png",
"photo-url-250":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo3_r1_250.png",
"photo-url-100":"http:\/\/25.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo3_r1_100.png",
"photo-url-75":"http:\/\/24.media.tumblr.com\/tumblr_mdfrfmvBa91rl56bgo3_r1_75sq.png"}]},

Edited by SmashT_AU

That's the json? Can you post the var_dump of posts? I think within $post you have a type of photo, and you should be able to do this:

case 'photo':
if(is_array($post['photos'])){
	foreach($post['photos'] AS $photo){
		$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>Permalink</a>";
		$blogroll .= "<p>".$post['photo-caption']."</p>";
		$blogroll .= "<img src=".$photo['photo-url-250'].">";   //This is the important line. - $photo not $post.
	}
}else{
	$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>Permalink</a>";
        $blogroll .= "<p>".$post['photo-caption']."</p>";
        $blogroll .= "<img src=".$post['photo-url-250'].">";  
}
break;

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.