Page 1 of 1

PHP & Tumblr

Posted: Mon Jan 09, 2012 3:28 am
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!

Re: PHP & Tumblr

Posted: Mon Jan 09, 2012 5:05 am
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.

Re: PHP & Tumblr

Posted: Mon Jan 09, 2012 8:48 am
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.

Re: PHP & Tumblr

Posted: Mon Jan 09, 2012 9:47 am
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

Re: PHP & Tumblr

Posted: Mon Jan 09, 2012 9:54 am
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";
}

Re: PHP & Tumblr

Posted: Mon Jan 09, 2012 10:17 am
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!