Process Tumblr JSON Feed


/ Published in: PHP
Save to your folder(s)

Tumblr API: http://www.tumblr.com/docs/de/api


Copy this code and paste it in your HTML
  1. <?php
  2. $ch = curl_init();
  3. curl_setopt($ch,CURLOPT_URL,"http://demo.tumblr.com/api/read/json");
  4. curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
  5. $result = curl_exec($ch);
  6. curl_close($ch);
  7.  
  8. $result = str_replace("var tumblr_api_read = ","",$result);
  9. $result = str_replace(';','',$result);
  10. $result = str_replace('\u00a0','&nbsp;',$result);
  11.  
  12. $jsondata = json_decode($result,true);
  13. $posts = $jsondata['posts'];
  14. var_dump($posts);
  15. $blogroll = "";
  16. foreach($posts as $post){
  17. $blogroll .= "<p>";
  18. $date = date('d.m.Y',$post['unix-timestamp']);
  19. $blogroll .= $date."<br />";
  20. switch ($post['type']) {
  21. case 'photo':
  22. $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a>";
  23. break;
  24.  
  25. case 'link':
  26. $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".$post['link-text']."</a><br />";
  27. $blogroll .= substr(strip_tags($post['link-description']),0,100)."...";
  28. break;
  29.  
  30. case 'regular':
  31. $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".$post['regular-title']."</a><br />";
  32. $blogroll .= substr(strip_tags($post['regular-body']),0,100)."...";
  33. break;
  34.  
  35. case 'quote':
  36. $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
  37. $blogroll .= substr(strip_tags($post['quote-text']),0,100)."...";
  38. break;
  39.  
  40. case 'conversation':
  41. if(empty($post['conversation-title'])){
  42. $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
  43. }else{
  44. $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".$post['conversation-title']."</a><br />";
  45. }
  46. $blogroll .= substr(nl2br($post['conversation-text']),0,100);
  47. break;
  48.  
  49. case 'audio':
  50. if(empty($post['audio-caption'])){
  51. $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
  52. }else{
  53. $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".strip_tags($post['audio-caption'])."</a><br />";
  54. }
  55. break;
  56.  
  57. default:
  58. $blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a>";
  59. break;
  60. }
  61. $blogroll .= "</p>";
  62. }
  63.  
  64. echo $blogroll;
  65. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.