SmashT_AU Posted November 13, 2012 Share Posted November 13, 2012 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; ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted November 13, 2012 Share Posted November 13, 2012 Is there a different post type for multiple photos than the "photo" one? If not, you'll have to add some logic in the first case there to check for multiple photos. Quote Link to comment Share on other sites More sharing options...
SmashT_AU Posted November 14, 2012 Author Share Posted November 14, 2012 (edited) 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 November 14, 2012 by SmashT_AU Quote Link to comment Share on other sites More sharing options...
Jessica Posted November 14, 2012 Share Posted November 14, 2012 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; Quote Link to comment Share on other sites More sharing options...
SmashT_AU Posted November 14, 2012 Author Share Posted November 14, 2012 That worked perfectly, thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.