Tumblr JSON API实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tumblr JSON API实现相关的知识,希望对你有一定的参考价值。
<?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'; // Tumblr JSON doesn't come in standard form, some str replace needed // parameter 'true' is necessary for output as PHP array $content = $value['posts']; $blogInfo = $value['tumblelog']; // the number of items you want to display $item = 10; // Echo the blog info echo "<h3><a href="" . $baseurl . "">" . $blogInfo['title'] . "</a></h3> "; echo "<h4>" . $blogInfo['description'] . "</h4> <hr /> "; // 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> "; }else{ // otherwise use the slug echo "<p><a href="" . $content[$i]['url-with-slug'] . "">" . ucwords(str_replace("-"," ",$content[$i]['slug'])) . "</a></p> "; } // 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 echo "<a href="" . $content[$i]['url-with-slug'] . "">Read more</a></p> "; }else{ // echo the whole body if it's under 120 characters echo $content[$i]['regular-body']; } echo "<hr /> "; // 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'] !== ""){ echo "<p><a href="" . $content[$i]['link-url'] . "">" . $content[$i]['link-text'] . "</a></p>"; // otherwise, just use the link as the title }else{ echo "<p><a href="" . $content[$i]['link-url'] . "">" . $content[$i]['link-url'] . "</a></p>"; } // then echo the description if it has one if($content[$i]['url-description'] !== ""){ echo $content[$i]['link-description'] . " "; } echo "<hr /> "; // then check to see if it's a quote }else if($content[$i]['type'] == "quote"){ // echo the quote echo "<p>" . $content[$i]['quote-text'] . "</p> "; // then the source if it has one if($content[$i]['quote-source'] !== ""){ echo "<p>-" . $content[$i]['quote-source'] . "</p> "; } echo "<hr /> "; // 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> "; echo $content[$i]['photo-caption']; echo "<hr /> "; // then check for audio }else if($content[$i]['type'] == "audio"){ echo "<embed " . $audioPlayer . " />"; echo $content[$i]['audio-caption']; echo "<hr /> "; // then check if it's a video }else if($content[$i]['type'] == "video"){ // Tumblr uses JS to render video hosted by them echo $content[$i]['video-player']; if($content[$i]['video-caption'] !== ""){ echo $content[$i]['video-caption']; } echo "<hr />"; } } // end for ?> <p><a href="<?php echo $baseurl;?>">Read more posts.</a></p>
以上是关于Tumblr JSON API实现的主要内容,如果未能解决你的问题,请参考以下文章