Reputation: 45
In localhost (using Wamp on windows). I'm trying to use this:
<?
$xml = simplexml_load_file(‘http://stocklamp.tumblr.com/api/read/xml’);
$posts = $xml->xpath(“/tumblr/posts/post[@type=’regular’]”);
foreach($posts as $post) {?>
<?echo $post[‘id’];?>
<?echo $post[‘url-with-slug’];?>”>
<?echo $post->{‘regular-title’};?>
<?echo $post->{‘regular-body’};?>
<?echo date(“jS D M, H:i”,strtotime($post[‘date’]));?>
<?}?>
When trying, all I see is this on my site:
xpath(“/tumblr/posts/post[@type=’regular’]”); foreach($posts as $post) { ?> ”> {‘regular-title’};?> {‘regular-body’};?>
I found the snippet here:
http://stocklamp.tumblr.com/post/274675902/putting-your-tumblr-posts-on-your-websites-the-easy-way
Edit: fixed. Now I'm getting
Parse error: syntax error, unexpected ':' in C:\wamp\www..\index.php on line 52
and it is this line:
$xml = simplexml_load_file(‘http://stocklamp.tumblr.com/api/read/xml’);
I keep getting this error: http://codepad.org/7f1IejIG
Okay. Now I go that fixed, but how do I get the posts by tag?
changing 'type=...' doesn't work.
$posts = $xml->xpath("/tumblr/posts/post[@type='file']");
Upvotes: 0
Views: 239
Reputation: 77996
You are using curly quotes. Try this:
<?
$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post[@type='regular']");
foreach($posts as $post) {
echo $post['id'];
echo $post['url-with-slug'];
echo $post->{'regular-title'};
echo $post->{'regular-body'};
echo date("jS D M, H:i",strtotime($post['date']));
}
?>
Upvotes: 2
Reputation: 49208
The last problem is to resolve is that the API XML url provided on the blog is not right. The following works:
<?php
$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read/'); // No /xml
$posts = $xml->xpath('/tumblr/posts/post[@type="regular"]');
foreach($posts as $post) {?>
<?echo $post['id'];?>
<?echo $post['url-with-slug'];?>”>
<?echo $post->{'regular-title'};?>
<?echo $post->{'regular-body'};?>
<?echo date("jS D M, H:i",strtotime($post['date']));?>
<?}?>
http://jfcoder.com/test/tumblrtest.php
EDIT
Try running this code without the tags interspersed:
<?php
$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read/'); // No /xml
$posts = $xml->xpath('/tumblr/posts/post[@type="regular"]');
foreach($posts as $post) {
echo $post['id'];
echo $post['url-with-slug'];
echo $post->{'regular-title'};
echo $post->{'regular-body'};
echo date("jS D M, H:i",strtotime($post['date']));
}
?>
Upvotes: 0
Reputation: 18295
Your page isn't running as PHP. If you view source, you'll see the entire PHP code visible on your page as plain HTML.
The bit that displays is after the $xml-> bit, because it thinks the opening php tag and the -> are one big html tag.
Is the file extension .php? What's the file name? Where did you put this code? Try replacing <?
with <?php
, sometimes web servers don't have short tags enabled.
Upvotes: 3