Posted By

mloberg on 10/21/10


Tagged

php json api tumblr


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

bobbym245
laurentzziu
bmeraly
kantiano


Tumblr JSON API Implementation


 / Published in: PHP
 

  1. <?php
  2. $tumblog = 'username'; // change to your username
  3. // if your Tumblog is self hosted, you need to change the base url to the location of your tumblog
  4. $baseurl = 'http://' . $tumblog . '.tumblr.com';
  5. $request = $baseurl . '/api/read/json';
  6. $ci = curl_init($request);
  7. curl_setopt($ci,CURLOPT_RETURNTRANSFER, TRUE);
  8. $input = curl_exec($ci);
  9. curl_close($ci);
  10.  
  11. // Tumblr JSON doesn't come in standard form, some str replace needed
  12.  
  13. $input = str_replace('var tumblr_api_read = ','',$input);
  14. $input = str_replace(';','',$input);
  15.  
  16. // parameter 'true' is necessary for output as PHP array
  17.  
  18. $value = json_decode($input,true);
  19. $content = $value['posts'];
  20. $blogInfo = $value['tumblelog'];
  21.  
  22. // the number of items you want to display
  23. $item = 10;
  24.  
  25. // Echo the blog info
  26. echo "<h3><a href=\"" . $baseurl . "\">" . $blogInfo['title'] . "</a></h3>\n";
  27. echo "<h4>" . $blogInfo['description'] . "</h4>\n<hr />\n";
  28.  
  29. // then loop the blog contents
  30. for($i=0;$i<$item;$i++){
  31. // we need to find out what the post type is, so we can format it appropriately
  32. // first check to see if it is a regular post
  33. if($content[$i]['type'] == "regular"){
  34. // echo title
  35. if($content[$i]['regular-title'] !== ""){
  36. echo "<p><a href=\"" . $content[$i]['url-with-slug'] . "\">" . $content[$i]['regular-title'] . "</a></p>\n";
  37. }else{
  38. // otherwise use the slug
  39. echo "<p><a href=\"" . $content[$i]['url-with-slug'] . "\">" . ucwords(str_replace("-"," ",$content[$i]['slug'])) . "</a></p>\n";
  40. }
  41. // then echo the body
  42. // grab the string length of the post
  43. $postlength = strlen($content[$i]['regular-body']);
  44. if($postlength > 120){
  45. // if it's greater then 120, echo the first 120 characters and then add a read more link
  46. echo "<p>" . substr($content[$i]['regular-body'],3,123) . "... ";
  47. echo "<a href=\"" . $content[$i]['url-with-slug'] . "\">Read more</a></p>\n";
  48. }else{
  49. // echo the whole body if it's under 120 characters
  50. echo $content[$i]['regular-body'];
  51. }
  52. echo "<hr />\n";
  53. // then check if it's a link
  54. }else if($content[$i]['type'] == "link"){
  55. // if it has a title, use that as the title
  56. if($content[$i]['link-text'] !== ""){
  57. echo "<p><a href=\"" . $content[$i]['link-url'] . "\">" . $content[$i]['link-text'] . "</a></p>";
  58. // otherwise, just use the link as the title
  59. }else{
  60. echo "<p><a href=\"" . $content[$i]['link-url'] . "\">" . $content[$i]['link-url'] . "</a></p>";
  61. }
  62. // then echo the description if it has one
  63. if($content[$i]['url-description'] !== ""){
  64. echo $content[$i]['link-description'] . "\n";
  65. }
  66. echo "<hr />\n";
  67. // then check to see if it's a quote
  68. }else if($content[$i]['type'] == "quote"){
  69. // echo the quote
  70. echo "<p>" . $content[$i]['quote-text'] . "</p>\n";
  71. // then the source if it has one
  72. if($content[$i]['quote-source'] !== ""){
  73. echo "<p>-" . $content[$i]['quote-source'] . "</p>\n";
  74. }
  75. echo "<hr />\n";
  76. // then check to see if it's a photo
  77. }else if($content[$i]['type'] == "photo"){
  78. // 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
  79. echo "<p><a href=\"" . $content[$i]['url-with-slug'] . "\"><img src=\"" . $content[$i]['photo-url-250'] . "\" alt=\"" . $content[$i]['slug'] . "\" /></a></p>\n";
  80. echo $content[$i]['photo-caption'];
  81. echo "<hr />\n";
  82. // then check for audio
  83. }else if($content[$i]['type'] == "audio"){
  84. $audioPlayer = trim($content[$i]['audio-player'],"></embed>");
  85. echo "<embed " . $audioPlayer . " />";
  86. echo $content[$i]['audio-caption'];
  87. echo "<hr />\n";
  88. // then check if it's a video
  89. }else if($content[$i]['type'] == "video"){
  90. // Tumblr uses JS to render video hosted by them
  91. echo "<script src=\"http://assets.tumblr.com/javascript/tumblelog.js\"></script>";
  92. echo $content[$i]['video-player'];
  93. if($content[$i]['video-caption'] !== ""){
  94. echo $content[$i]['video-caption'];
  95. }
  96. echo "<hr />";
  97. }
  98. } // end for
  99. ?>
  100. <p><a href="<?php echo $baseurl;?>">Read more posts.</a></p>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: zearoh on November 28, 2010

Hey, any idea if we can get the post's date to show up?

Posted By: zearoh on November 28, 2010

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?

Posted By: zearoh on November 29, 2010

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?

Posted By: mloberg on February 12, 2011

You could check if $input has anything in it first. If it doesn't then Tumblr is down.

You need to login to post a comment.