<?
include 'https://dev.thecubecast.com/PHP/tumblr.php';
Tumblr_Read('TUMBLR_USERNAME', 6);
?>
include 'https://dev.thecubecast.com/PHP/tumblr.php';
does one important thing, it calls the file containing the script. Scroll down to "Customization" if you would like to customize it.
Tumblr_Read('TUMBLR_USERNAME', 6);
runs the function that does all the magic. just change the 'TUMBLR_USERNAME'
to the profile and the 6 to however many posts you want loaded.
<?
$request_url = 'https://'.$user.'.tumblr.com/api/read?start=-1&num='.$amount; //get xml file
$xml = simplexml_load_file($request_url); //load it
if ($xml->posts->post[$x]['type'] == 'regular') {
$title = $xml->posts->post[$x]->{'regular-title'}; //load post title into variable $title
$post = $xml->posts->post[$x]->{'regular-body'}; //load post body into variable $post
$timestamp = $xml->posts->post[$x]['date-gmt']; //load timestamp of post into variable $timestamp
$output = substr($post,0,-1); //make the post useable for output
echo // Output the variables above in some html
'<div id="Update_Wrapper"> <!-- Holds each individual post -->
<h2 style="text-align:center;">'.$title.'</h2>
<span>'.$output.'</span>
<div id="Update_footer"><br>'.$timestamp.'</div>
</div> '
;
}
?>
To make any sense of what the above code is doing, take a look at what a basic text post from tumblr's XML file would look like.
<tumblr>
<posts>
<post id="15234523452345" url="https://user.tumblr.com/post/15234523452345" url-with-slug="https://user.tumblr.com/post/15234523452345/finished-up-the-new-update-feed-also-added" type="regular" date-gmt="2016-04-30 18:23:09 GMT" date="Sat, 30 Apr 2016 14:23:09">
<regular-body>
<p>This is the first paragraph of the post. </p><p>The Second Paragraph</p>
</regular-body>
</post>
</posts>
</tumblr>
Let's say that a user named "user" made a post. This particular post has not title, or tags. In the above PHP code the first two lines retrieve the link to a users XML page containing all their most recent posts.
The if
statement below checks if the post is a regular (text) post or 1 of the 6 other types. In this case its a regular post so the if statement will run.
Each following line looks through the file using SimplePHP and asigns what it finds to a variable. The echo statement at the end of the if
statement puts the respective variables into html that will make the text drawn from the XML look decent.
PHP Manual Documentation of SimpleXML in php.
StackOverflow "Tumblr API in PHP File structure" asked by Dave on December 6, 2013.
StackOverflow "Integrating tumblr blog with website" awnser by Coasta on November 3, 2011
StackOverflow "tumblr PHP file_get_contents html not running" awnser by Chaim on May 2, 2016