Tumblr JSON API实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tumblr JSON API实现相关的知识,希望对你有一定的参考价值。

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

以上是关于Tumblr JSON API实现的主要内容,如果未能解决你的问题,请参考以下文章

进程Tumblr JSON提要

Tumblr API包装器

使用 php 和 Tumblr API 发布到 Tumblr

PHP 处理Tumblr JSON Feed

json Tumblr用户列表

Tumblr API v2 的“默认回调 URL:”是啥?