Return to Snippet

Revision: 34334
at November 16, 2010 07:47 by mloberg


Updated Code
<?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

$input = str_replace('var tumblr_api_read = ','',$input);
$input = str_replace(';','',$input);

// 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
echo "<h3><a href=\"" . $baseurl . "\">" . $blogInfo['title'] . "</a></h3>\n";
echo "<h4>" . $blogInfo['description'] . "</h4>\n<hr />\n";

// 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
		$postlength = strlen($content[$i]['regular-body']);
		if($postlength > 120){
			// if it's greater then 120, echo the first 120 characters and then add a read more link
			echo "<p>" . substr($content[$i]['regular-body'],3,123) . "... ";
			echo "<a href=\"" . $content[$i]['url-with-slug'] . "\">Read more</a></p>\n";
		}else{
			// echo the whole body if it's under 120 characters
			echo $content[$i]['regular-body'];
		}
		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'] !== ""){
			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'] . "\n";
		}
		echo "<hr />\n";
	// 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>\n";
		// then the source if it has one
		if($content[$i]['quote-source'] !== ""){
			echo "<p>-" . $content[$i]['quote-source'] . "</p>\n";
		}
		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 $content[$i]['photo-caption'];
		echo "<hr />\n";
	// then check for audio
	}else if($content[$i]['type'] == "audio"){
		$audioPlayer = trim($content[$i]['audio-player'],"></embed>");
		echo "<embed " . $audioPlayer . " />";
		echo $content[$i]['audio-caption'];
		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>";
		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>

Revision: 34333
at November 4, 2010 07:23 by mloberg


Updated Code
<?php
// make sure you replace 'yourtumblr' with your Tumblr blog.
$request = 'http://yourtumblr.tumblr.com/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

$input = str_replace('var tumblr_api_read = ','',$input);
$input = str_replace(';','',$input);

// 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
echo "<h3><a href=\"http://yourusername.tumblr.com\">" . $blogInfo['title'] . "</a></h3>\n";
echo "<h4>" . $blogInfo['description'] . "</h4>\n<hr />\n";

// 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
		$postlength = strlen($content[$i]['regular-body']);
		if($postlength > 120){
			// if it's greater then 120, echo the first 120 characters and then add a read more link
			echo "<p>" . substr($content[$i]['regular-body'],3,123) . "... ";
			echo "<a href=\"" . $content[$i]['url-with-slug'] . "\">Read more</a></p>\n";
		}else{
			// echo the whole body if it's under 120 characters
			echo $content[$i]['regular-body'];
		}
		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'] !== ""){
			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'] . "\n";
		}
		echo "<hr />\n";
	// 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>\n";
		// then the source if it has one
		if($content[$i]['quote-source'] !== ""){
			echo "<p>-" . $content[$i]['quote-source'] . "</p>\n";
		}
		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 $content[$i]['photo-caption'];
		echo "<hr />\n";
	// then check for audio
	}else if($content[$i]['type'] == "audio"){
		$audioPlayer = trim($content[$i]['audio-player'],"></embed>");
		echo "<embed " . $audioPlayer . " />";
		echo $content[$i]['audio-caption'];
		echo "<hr />\n";
	// support for video not yet implemented
	}
} // end for
?>
<p><a href="http://yourtumblr.tumblr.com">Read more posts.</a></p>

Revision: 34332
at October 23, 2010 06:51 by mloberg


Updated Code
<?php
// make sure you replace 'yourtumblr' with your Tumblr blog.
$request = 'http://yourtumblr.tumblr.com/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

$input = str_replace('var tumblr_api_read = ','',$input);
$input = str_replace(';','',$input);

// 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
echo "<h3><a href=\"http://" . $text . ".tumblr.com\">" . $blogInfo['title'] . "</a></h3>\n";
echo "<h4>" . $blogInfo['description'] . "</h4>\n<hr />\n";

// 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
		$postlength = strlen($content[$i]['regular-body']);
		if($postlength > 120){
			// if it's greater then 120, echo the first 120 characters and then add a read more link
			echo "<p>" . substr($content[$i]['regular-body'],3,123) . "... ";
			echo "<a href=\"" . $content[$i]['url-with-slug'] . "\">Read more</a></p>\n";
		}else{
			// echo the whole body if it's under 120 characters
			echo $content[$i]['regular-body'];
		}
		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'] !== ""){
			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'] . "\n";
		}
		echo "<hr />\n";
	// 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>\n";
		// then the source if it has one
		if($content[$i]['quote-source'] !== ""){
			echo "<p>-" . $content[$i]['quote-source'] . "</p>\n";
		}
		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 $content[$i]['photo-caption'];
		echo "<hr />\n";
	// then check for audio
	}else if($content[$i]['type'] == "audio"){
		$audioPlayer = trim($content[$i]['audio-player'],"></embed>");
		echo "<embed " . $audioPlayer . " />";
		echo $content[$i]['audio-caption'];
		echo "<hr />\n";
	// support for video not yet implemented
	}
} // end for
?>
<p><a href="http://yourtumblr.tumblr.com">Read more posts.</a></p>

Revision: 34331
at October 21, 2010 06:01 by mloberg


Initial Code
<?php
// make sure you replace 'yourtumblr' with your Tumblr blog.
$request = 'http://yourtumblr.tumblr.com/api/read/json';
$ci = curl_init($request);
curl_setopt($ci,CURLOPT_RETURNTRANSFER, TRUE);
$input = curl_exec($ci);

// Tumblr JSON doesn't come in standard form, some str replace needed

$input = str_replace('var tumblr_api_read = ','',$input);
$input = str_replace(';','',$input);

// 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
echo "<h3><a href=\"http://" . $text . ".tumblr.com\">" . $blogInfo['title'] . "</a></h3>\n";
echo "<h4>" . $blogInfo['description'] . "</h4>\n<hr />\n";

// 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
		$postlength = strlen($content[$i]['regular-body']);
		if($postlength > 120){
			// if it's greater then 120, echo the first 120 characters and then add a read more link
			echo "<p>" . substr($content[$i]['regular-body'],3,123) . "... ";
			echo "<a href=\"" . $content[$i]['url-with-slug'] . "\">Read more</a></p>\n";
		}else{
			// echo the whole body if it's under 120 characters
			echo $content[$i]['regular-body'];
		}
		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'] !== ""){
			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'] . "\n";
		}
		echo "<hr />\n";
	// 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>\n";
		// then the source if it has one
		if($content[$i]['quote-source'] !== ""){
			echo "<p>-" . $content[$i]['quote-source'] . "</p>\n";
		}
		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 $content[$i]['photo-caption'];
		echo "<hr />\n";
	// then check for audio
	}else if($content[$i]['type'] == "audio"){
		$audioPlayer = trim($content[$i]['audio-player'],"></embed>");
		echo "<embed " . $audioPlayer . " />";
		echo $content[$i]['audio-caption'];
		echo "<hr />\n";
	// support for video not yet implemented
	}
} // end for
?>
<p><a href="http://yourtumblr.tumblr.com">Read more posts.</a></p>

Initial URL

                                

Initial Description

                                

Initial Title
Tumblr JSON API Implementation

Initial Tags
php, json, api

Initial Language
PHP