/ Published in: PHP
Expand |
Embed | Plain Text
<?php $tumblog = 'username'; // change to your username // if your Tumblog is self hosted, you need to change the base url to the location of your tumblog $baseurl = 'http://' . $tumblog . '.tumblr.com'; $request = $baseurl . '/api/read/json'; $ci = curl_init($request); curl_setopt($ci,CURLOPT_RETURNTRANSFER, TRUE); $input = curl_exec($ci); curl_close($ci); // Tumblr JSON doesn't come in standard form, some str replace needed // parameter 'true' is necessary for output as PHP array $value = json_decode($input,true); $content = $value['posts']; $blogInfo = $value['tumblelog']; // the number of items you want to display $item = 10; // Echo the blog info // then loop the blog contents for($i=0;$i<$item;$i++){ // we need to find out what the post type is, so we can format it appropriately // first check to see if it is a regular post if($content[$i]['type'] == "regular"){ // echo title if($content[$i]['regular-title'] !== ""){ echo "<p><a href=\"" . $content[$i]['url-with-slug'] . "\">" . $content[$i]['regular-title'] . "</a></p>\n"; }else{ // otherwise use the slug echo "<p><a href=\"" . $content[$i]['url-with-slug'] . "\">" . ucwords(str_replace("-"," ",$content[$i]['slug'])) . "</a></p>\n"; } // then echo the body // grab the string length of the post if($postlength > 120){ // if it's greater then 120, echo the first 120 characters and then add a read more link }else{ // echo the whole body if it's under 120 characters } echo "<hr />\n"; // then check if it's a link }else if($content[$i]['type'] == "link"){ // if it has a title, use that as the title if($content[$i]['link-text'] !== ""){ // otherwise, just use the link as the title }else{ } // then echo the description if it has one if($content[$i]['url-description'] !== ""){ } echo "<hr />\n"; // then check to see if it's a quote }else if($content[$i]['type'] == "quote"){ // echo the quote // then the source if it has one if($content[$i]['quote-source'] !== ""){ } echo "<hr />\n"; // then check to see if it's a photo }else if($content[$i]['type'] == "photo"){ // I know it's not valid to not to specify the width and height, but I was having issues without making them the original size echo "<p><a href=\"" . $content[$i]['url-with-slug'] . "\"><img src=\"" . $content[$i]['photo-url-250'] . "\" alt=\"" . $content[$i]['slug'] . "\" /></a></p>\n"; echo "<hr />\n"; // then check for audio }else if($content[$i]['type'] == "audio"){ echo "<hr />\n"; // then check if it's a video }else if($content[$i]['type'] == "video"){ // Tumblr uses JS to render video hosted by them echo "<script src=\"http://assets.tumblr.com/javascript/tumblelog.js\"></script>"; if($content[$i]['video-caption'] !== ""){ } echo "<hr />"; } } // end for ?>
Comments
Subscribe to comments
You need to login to post a comment.

Hey, any idea if we can get the post's date to show up?
Ok, I figured that out. easy.
echo "" . $content[$i]['date'] . "\n";
But now I'm wondering if there's a way to ge the tags to show up. if you replace "date" with "tags" it prints "array" which makes sense. How do you parse that array and possibly make each a link to other posts tagged the same?
I don't care about tags anymore, haha.. Now I need a plan B implemented that can print out a message saying the tumblr API is down, or maybe use the tumblr embed javascript as a backup in that situation. Any one have any ideas?
You could check if $input has anything in it first. If it doesn't then Tumblr is down.