Jump to content

simpleXML Attribute won't be read in foreach loop


candybrie

Recommended Posts

Ok so this is the xml I'm trying to read basically goes:

 

<tumblr>
            <posts>
                       <post url="http://stuff">
                                other stuff
                       </post>
                       <post url="http://stuff">
                                other stuff
                       </post>
                       <post url="http://stuff">
                                other stuff
                       </post>
              </posts>
</tumblr>

 

If it helps to actually see it: http://candybrie.tumblr.com/api/read?type=post&start=0&num=4&type=text

 

My problem is getting the url's from each post.

 

The relevant php looks like this:

 

foreach ($xml->posts->post AS $post) 
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$link = $post->attributes()->url;
$small_post = substr($post,0,320);

echo '<h1>'.$title.'</h1>';
echo '<p>'.$small_post;
echo "?";
echo '<a target=frame2 href="'.$link.'">Read More</a></p>';
}

 

The problem is with getting the url attribute, it gives me nothing. If I do this

 

$link = $xml->posts->post->attributes()->url;

 

I can get the first post's.

 

This is the first thing I've actually done in php so sorry if there is a really obvious answer or flaw.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.