PHP & Tumblr

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Stela
Forum Commoner
Posts: 46
Joined: Tue Aug 12, 2008 12:38 pm

PHP & Tumblr

Post by Stela »

Hello…

I'm trying to insert the 3 last posts (just a part of them) of a blog from tumblr in a website, I'm using this code

Code: Select all

<?php
$request_url = “http://finlay.tumblr.com/api/read?type=post&start=0&num=1”;
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{‘regular-title’};
$post = $xml->posts->post->{‘regular-body’};
$link = $xml->posts->post[‘url’];
$small_post = substr($post,0,320);
echo ‘<h1>’.$title.’</h1>’;
echo ‘<p>’.$small_post.’</p>’;
echo “…”;
echo “</br><a target=frame2 href=’”.$link.”’>Read More</a>”; 
?>
After many tries I just can't do it. It only appears the first one. I know how to insert all of them, but I just want the beginning of the first three of the posts.

Thanks!
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: PHP & Tumblr

Post by twinedev »

Browse to the URL you are calling from and you will see there IS only one item. So you won't be able to get 3.

-Greg

PS. If that is your blog, contrary to what is listed in the one article that did show up in the feed, yes you CAN use PHP in a style sheet without doing inline. You just have the site call a .php script that outputs the css code you want.
Stela
Forum Commoner
Posts: 46
Joined: Tue Aug 12, 2008 12:38 pm

Re: PHP & Tumblr

Post by Stela »

Oooops, sorry, my mistake, here is the "real" code

Code: Select all

$request_url = "http://wineinsights.tumblr.com/api/read?type=post&start=0&num=3";
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{'regular-title'};
$post = $xml->posts->post->{'regular-body'};
$small_post = substr($post,0,320);
echo '<h1>'.$title.'</h1>';
echo '<p>'.$small_post.'</p>';
echo "…";
This code extracts the last post but just a small portion. What I needed was a small portion of the last 3 posts.
Stela
Forum Commoner
Posts: 46
Joined: Tue Aug 12, 2008 12:38 pm

Re: PHP & Tumblr

Post by Stela »

I think I found a solution, not the Pro one, but it almost works entirelly

Code: Select all


<?php
$request_url = "http://wineinsights.tumblr.com/api/read?type=post&start=0&num=1";
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{'regular-title'};
$post = $xml->posts->post->{'regular-body'};
$small_post = substr($post,0,320);
echo '<strong>'.$title.'</strong>';
echo $small_post;
echo '<H5>' . "..." . '</H5>';
?>

	<?php
$request_url = "http://wineinsights.tumblr.com/api/read?type=post&start=1&num=2";
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{'regular-title'};
$post = $xml->posts->post->{'regular-body'};
$small_post = substr($post,0,320);
echo '<strong>'.$title.'</strong>';
echo ''.$small_post.'';
echo '<H5>' . "..." . '</H5>';
?>



	<?php
$request_url = "http://wineinsights.tumblr.com/api/read?type=post&start=2&num=3";
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{'regular-title'};
$post = $xml->posts->post->{'regular-body'};
$small_post = substr($post,0,320);
echo '<strong>'.$title.'</strong>';
echo ''.$small_post.'';
?>

Know, I only wanted to add "..." to the end of the cut of the text, but the "..." always go two lines under the post. How can I put the "..." after the post? Is it possible?

You can see it here
http://www.win-winemarketing.com/Working/index.php
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: PHP & Tumblr

Post by twinedev »

The issue is that you need to loop over the posts... you were very close:

Code: Select all

<?php
$request_url = "http://wineinsights.tumblr.com/api/read?type=post&start=0&num=3";
$xml = simplexml_load_file($request_url);

// In the object $xml, [b]$xml->posts->post[/b] is actually an array of posts, which you need to loop over

foreach($xml->posts->post as $oPost) {
	$title = $oPost->{'regular-title'};
	$post = $oPost->{'regular-body'};
	$small_post = substr($post,0,320);
	echo '<h1>'.$title.'</h1>';
	echo '<p>'.$small_post.'</p>';
	echo "…\n\n";
}
Whenever you are working with XML object, and you are not sure what is what, after you load it into $xml do a var_dump($xml) (you will want to do View->Source in your browsers, as it will most likely not be too readable since the xml contains html and the browser will try to parse it.

On another note, the XML already contains <P> ... </P> tags around the post, so you do not need to add them when echoing out. However, you are limiting the size of the post, so you will want to check to see if need to add the ending one back.

Code: Select all

foreach($xml->posts->post as $oPost) {
	$title = $oPost->{'regular-title'};
	$post = $oPost->{'regular-body'};
	$small_post = substr($post,0,320);
	echo '<h1>'.$title.'</h1>';
	echo $small_post;
	if (substr($small_post,-4)!='</p>') { echo '</p>'; } 
	echo "…\n\n";
}
Stela
Forum Commoner
Posts: 46
Joined: Tue Aug 12, 2008 12:38 pm

Re: PHP & Tumblr

Post by Stela »

Code: Select all

if (substr($small_post,-4)!='</p>') { echo '</p>'; } 
        echo "…\n\n";
I would never thought about this 8O

Thank you very much!
Post Reply