Jump to content

simplexml_load_file and tumblr api


Liberty53k
Go to solution Solved by us2rn4m2,

Recommended Posts

I'm working through some code to display a tumblr blog on a website.

 

Each of Tumblr's post types are easy enough to get to...

 


<ul id="reblog">
<? foreach ($xml->posts->post as $post) { ?>

<li>

<?
// REGULAR POSTS
if ($post['type'] == "regular" ){

echo "This is a regular post";
}

// PHOTO POSTS
if ($post['type'] == "photo" ){

echo "This is a photo post";
}

// LINK POSTS
if ($post['type'] == "link" ){

echo "This is a link post";
}

// AUDIO POSTS
if ($post['type'] == "audio" ){

echo "This is an audio post";
}



?>

</li>
<? } ?>
</ul>

 

What I'm trying to do is to check and see if certain tags are used on a post, and the based on that I'll be formatting the entry a little different.

 

I can list all of the tags, but I'm stumped on how to check the existence of a certain tag in $post->{'tag'}

 

$xml = simplexml_load_file('http://applesold.com/assets/cache/tumblrposts.txt');


<ul id="reblog">
<? foreach ($xml->posts->post as $post) { ?>

<li>

<?
// REGULAR POSTS
if ($post['type'] == "regular" ){
echo "type of post: regular<br>";

// THIS DOESN'T WORK --
if ($post->{'tag'} == "infographic") {
echo "INFOGRAPHIC";
} else {
echo "non-INFOGRAPHIC";
}
}


?>


</li>
<? } ?>
</ul>

Link to comment
Share on other sites

Thanks for the response…

Not exactly what I'm trying to get at...

 

Rather than printing all of the tags as being "infographic" or "non-infographic" --

 

what I'm trying to do is simply test to see if a certain tag is present --- and do something based on that or NOT

 

if the infographic tag exists for this post..  
{do this } 
     else 
{do this} 

Link to comment
Share on other sites

Your questions associate to your code are not very clear...

So I show you the method that I use to check the presence of "certain tag".

 

 

// check the presence of 'post'
if(property_exists($xml, 'posts')) {
echo 'post';
} else {
// ...
}

// check the presence of 'regular-title'
if(property_exists($xml->posts->post[0], 'regular-title')) {
echo 'regular-title';
} else {
// ...
}

// check the presence of 'tag'
foreach($xml->posts->post as $post) {
if(property_exists($post, 'tag')) {
echo 'tag';
} else {
// ...
}
}

Link to comment
Share on other sites

I'm apologize for the lack of clarity. I'm not sure what would make it more understandable.

 

// check the presence of 'tag'
foreach($xml->posts->post as $post) {
if(property_exists($post, 'tag')) {
echo 'tag';
} else {
// ...
}
}

 

The effect of that code is that it's echoing the word "tag" for every occurrence of a tag on each post OR doing something else if it doesn't exist.

 

What I'm trying to do is more narrow:

  • Each blog post has a set of tags-- (ie "infographic", "news", "real estate", "buying a house", "foreclosure" ...etc)
  • I want to be able to check for the the existence of a particular tag... (ie "infographic")
  • and then if that particular tag exists.. do something.

Edited by Liberty53k
Link to comment
Share on other sites


foreach($feed->posts->post as $post) {
if(property_exists($post, 'tag')) {
$count = count($post->tag);
$i = 0;
while($i <= $count) {
if(isset($post->tag[$i]) && $post->tag[$i] == 'real estate') {
// print_r($post->tag);
echo '<b>YES</b>';
} else {
echo 'no';
}
$i++;
}
} else {
// ...
}
}
 
Link to comment
Share on other sites

// ! Different output with PHP 5.3 and 5.4
 
// PHP 5.4
if(isset($post->tag[$i]) && $post->tag[$i] == 'real estate') {
echo '<pre>';
print_r($post->tag);
/*
SimpleXMLElement Object
(
    [0] => real estate
    [1] => vets
    [2] => Veterans Affairs
    [3] => VA
    [4] => home loans
    [5] => mortgage info
    [6] => video
    [7] => Frank Garay
    [8] => Brian Stevens
    [9] => buying a house
)
SimpleXMLElement Object
(
    [0] => real estate
    [1] => dream home
    [2] => buying a hosue
    [3] => neighbours
    [4] => compatibility
    [5] => lifestyle
    [6] => Frank Garay
    [7] => Brian Stevens
)
SimpleXMLElement Object
(
    [0] => real estate
    [1] => news
    [2] => hoopla
    [3] => mortgage rates
    [4] => video
    [5] => Frank Garay
    [6] => Brian Stevens
    [7] => hype
)
SimpleXMLElement Object
(
    [0] => green
    [1] => lifestyle
    [2] => energy conservation
    [3] => NEST
    [4] => personal finance
    [5] => real estate
    [6] => home systems
    [7] => Energy Star
)
SimpleXMLElement Object
(
    [0] => real estate
    [1] => buying a house
    [2] => internet
    [3] => www
    [4] => infographic
    [5] => home search
)
*/


// PHP 5.3
echo '<pre>';
print_r($post->tag);
/*
SimpleXMLElement Object
(
[0] => real estate
)
SimpleXMLElement Object
(
[0] => real estate
)
SimpleXMLElement Object
(
[0] => real estate
)
SimpleXMLElement Object
(
[0] => green
)
SimpleXMLElement Object
(
[0] => real estate
)
*/
 

 

Edited by us2rn4m2
Link to comment
Share on other sites

thank you!!!... I've been able to adapt this. 

 

 

I spoke too soon....

 

Here's what I have based off of your post...

		<? // if the post is tagged "infographic"		
		if(property_exists($post, 'tag')) {
		$count = count($post->tag);
		$i = 0;
		while($i <= $count) {
		if(isset($post->tag[$i]) && $post->tag[$i] == 'infographic') {
		echo 'infographic';
		} else {
		echo 'non-infographic';
		}
		$i++; 
		} 
		}

 
		?>

 

 

 

 

What I'm getting is something like this (each bullet represents a single post ):

  • non-infographicnon-infographicnon-infographicnon-infographicnon-infographicnon-infographicnon-infographicnon-infographic
  • non-infographicnon-infographicnon-infographicnon-infographicinfographicnon-infographicnon-infographic
  • non-infographicnon-infographicnon-infographicnon-infographicnon-infographicnon-infographic

 

What I'm trying to get is output that looks like this...  

  • non-infographic
  • infographic
  • non-infgraphic

I do not NEED to print something for every single tag -- I only need to check the whole bunch of tags to see if something is indeed an "infographic"

Link to comment
Share on other sites

You could take a slightly different approach, like getting the list of tags and looking to see if any have an "infographic" tag.

foreach ($xml->posts->post as $post) {
$tags = array_map('strval', $post->xpath('tag'));
if (in_array('infographic', $tags)) {
// yay, has an infographic tag
} else {
// aww, not infographic
}
}
Link to comment
Share on other sites


$xml   = simplexml_load_file('http://applesold.com/assets/cache/tumblrposts.txt');
$tag   = null;
$bunch = array();

echo '<ul>';
foreach($xml->posts->post as $post) {
if(property_exists($post, 'tag')) {
$count = count($post->tag);
$i = 0;
while($i <= $count) {
if(isset($post->tag[$i]) && $post->tag[$i] == 'infographic') {
$bunch[$i] = '<li><b>infographic</b></li>';
$tag = true;
} else {
$bunch[$i] = '<li>non-infographic</li>';
}
$i++; 

// check the whole bunch of tags and print "a list" if something is indeed an "infographic"
if(in_array('<li><b>infographic</b></li>', $bunch)) {
echo implode('', $bunch) . '</ul>';
}
// check the whole bunch of tags and print "YES, has an infographic tag" if something is indeed an "infographic"
if($tag) {
echo 'YES, has an infographic tag';
$tag = null;
}

}

/**
Result:
non-infographic
non-infographic
non-infographic
non-infographic
infographic
non-infographic
non-infographic
non-infographic
non-infographic
non-infographic
non-infographic
YES, has an infographic tag
*/
 
Link to comment
Share on other sites

  • Solution

update

// check the whole bunch of tags and print "a list" if something is indeed an "infographic"
if(in_array('<li><b>infographic</b></li>', $bunch)) {
echo implode('', $bunch) . '</ul>'; 
}
$bunch = array(); // add this line
// check the whole bunch of tags and print "YES, has an infographic tag" if something is indeed an "infographic"
if($tag) {
echo 'YES, has an infographic tag';
$tag = null;
}
 
Link to comment
Share on other sites

 

You could take a slightly different approach, like getting the list of tags and looking to see if any have an "infographic" tag.

foreach ($xml->posts->post as $post) {
    $tags = array_map('strval', $post->xpath('tag'));
    if (in_array('infographic', $tags)) {
        // yay, has an infographic tag
    } else {
        // aww, not infographic
    }
}

 

In my own research, I had come across the in_array function and couldn't figure out to use it since the info was an object???

 

I tried putting your example in, and couldn't get it working... This seems like the right path though. 

 

I've now got something working, & I intend on tinkering some more with this idea, and see if I can work through it.

 

I appreciate your input!!

 

 

 

 

update

// check the whole bunch of tags and print "a list" if something is indeed an "infographic"
if(in_array('<li><b>infographic</b></li>', $bunch)) {
echo implode('', $bunch) . '</ul>'; 
}
$bunch = array(); // add this line
// check the whole bunch of tags and print "YES, has an infographic tag" if something is indeed an "infographic"
if($tag) {
echo 'YES, has an infographic tag';
$tag = null;
}
 

 

 

So now using this (and a slight modification) I've finally gotten something working with the desired output.

 

Here's what I've got, in case anyone else want to see it...

 

 
<ul id="reblog">
<? foreach ($xml->posts->post as $post) { ?>
    
<li>
 
<? 
// REGULAR POSTS
if ($post['type'] == "regular" ){?> 
    
    
    <? // if this is a status post with no title... 
if ($post->{'regular-title'} == "") {?>
        
Regular Post: This is a status update
 
<? ;}  ?>
        
        
<? // regular post with check for the "infographic" tag 
 
if(property_exists($post, 'tag')) {
$count = count($post->tag);
$i = 0;
while($i <= $count) {
if(isset($post->tag[$i]) && $post->tag[$i] == 'infographic') {
$bunch[$i] = 'infographic';
$tag = true;
} else {
$bunch[$i] = 'non-infographic';
}
$i++; 
} 
 
 
if($tag) {?>
         
Regular Post: This is an infographic
 
<? 
$tag = null;}
else {?> 
        
        Regular Post 
 
<? ;} 
}  
?>
        
  
<? 
} 
 
// PHOTO POSTS
if ($post['type'] == "photo" ){ ?>
Photo Post
 
<?
    }
// LINK POSTS
if ($post['type'] == "link" ){ ?>
    Link Post 
 
<?
    }
 
// Quote Posts 
if ($post['type'] == "quote" ){ ?>
Quote POST
 
<?
    }
 
// CHAT POSTS
if ($post['type'] == "conversation" ){ ?>
Chat Post
<?
    }
 
// AUDIO POSTS
if ($post['type'] == "audio" ){ ?>
Audio Post
<?
    }
// VIDEO POSTS
if ($post['type'] == "video" ){ ?>
Video Post
    <?
    }
?>
</li>
<? } ?>
</ul>
 
 
 
/**
 
 
<ul>
<li>Video Post</li>
<li>Video Post</li>
<li>Photo Post
<li>Regular Post</li>
<li>Video Post</li>
<li>Link Post</li>
<li>Regular Post: This is an infographic</li>
<li>Video Post</li>
<li>Video Post</li>
<li>Regular Post</li>
<ul>
 
*/
 
 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.