Hi,
I have been working on this code for over 3 days, and all is working well. however the following is not doing what it should
I need it to serialize the category/s in an RSS feed. However I can not get it to work.
<?php
$feed = file_get_contents("http://www.techau.tv/blog/?feed=rss2");
$xml = new SimpleXmlElement($feed);
foreach ($xml->channel->item as $entry){
echo $entry->title.'<br/>';
echo $entry->description.'<br/><br/>';
foreach ($feed->channel->category as $kwordsarray) {
$keywordsArray[] = $kwordsarray;
}
//serialize keyword array ready for posting in link
$keywords = str_replace( array('O:16:"SimpleXMLElement":0:{}', 'O:16:"SimpleXMLElement":'), array('s:0:"";', 'O:8:"stdClass":'), serialize(simplexml_load_string($keywordsArray)));
?>Am I over thinking this?


Hi Russell, A couple of
Hi Russell,
A couple of points..
Firstly, I'm not sure whether you meant the foreach... category loop to be within the main foreach... item loop; as the code would just repeat for each item, potentially adding the same categories over and over again (as categories are not part of each item).
Secondly, i'm not sure what purpose simplexml_load_string serves on $keywordsArray - as the array won't be containing XML at this point. Have a look at something like this:
<?php$feed = file_get_contents("http://www.techau.tv/blog/?feed=rss2");
$xml = new SimpleXmlElement($feed);
foreach ($xml->channel->item as $entry)
{
echo $entry->title.'<br/>';
echo $entry->description.'<br/><br/>';
}
foreach ($feed->channel->category as $kwordsarray)
{
$keywordsArray[] = $kwordsarray;
}
//serialize keyword array ready for posting in link
$keywords = str_replace(
array('O:16:"SimpleXMLElement":0:{}', 'O:16:"SimpleXMLElement":'),
array('s:0:"";', 'O:8:"stdClass":'),
serialize($keywordsArray)
);
?>
Hope this helps!
Cheers,
David.
I had to change your script
I had to change your script a little, as it would not show each one after the description.
<?php
$feed = file_get_contents("http://www.techau.tv/blog/?feed=rss2");
$xml = new SimpleXmlElement($feed);
foreach ($xml->channel->item as $entry)
{
echo $entry->title.'<br/>';
echo $entry->description.'<br/><br/>';
foreach ($entry->category as $a => $kwordsarray)
{
$keywordsArray[] = $kwordsarray;
//serialize keyword array ready for posting in link
$keywords = str_replace(
array('O:16:"SimpleXMLElement":0:{}', 'O:16:"SimpleXMLElement":'),
array('s:0:"";', 'O:8:"stdClass":'),
serialize($keywordsArray)
);
print $keywords;
print "<br/><br/>";
}
}
?>
However it is still not working see {link saved}
In short it is printing
a:1:{i:0;s:0:"";}
a:2:{i:0;s:0:"";i:1;s:0:"";}
thanks
Hi Russell, How does that
Hi Russell,
How does that compare to the output that you want?
Thanks,
David.
Well it is not showing the
Well it is not showing the category inside there it should show something like this
a:2:{i:0;s:0:"iTouch";i:1;s:0:"Apple";}
Hi Russell, Could you add
Hi Russell,
Could you add some debug code to show the contents of the
<?php$feed = file_get_contents("http://www.techau.tv/blog/?feed=rss2");
$xml = new SimpleXmlElement($feed);
foreach ($xml->channel->item as $entry)
{
echo $entry->title.'<br/>';
echo $entry->description.'<br/><br/>';
$keywordsArray = array();
foreach ($entry->category as $a => $kwordsarray)
{
$keywordsArray[] = $kwordsarray;
}
print_r($keywordsArray);
//serialize keyword array ready for posting in link
$keywords = str_replace(
array('O:16:"SimpleXMLElement":0:{}', 'O:16:"SimpleXMLElement":'),
array('s:0:"";', 'O:8:"stdClass":'),
serialize($keywordsArray)
);
print $keywords;
print "<br/><br/>";
}
?>
I modified the loop slightly so that $keywordsArray is composed of all keywords before the serialization (I assume that what the code is intending to do); and also reset the array prior to this so that it begins from empty each time...
Hope this helps!
Cheers,
David.
Hey david, this is what it
Hey david,
this is what it is outputting
Array ( [0] => SimpleXMLElement Object ( ) ) a:1:{i:0;s:0:"";}I hope that helps you.
Hi Russell, What's the
Hi Russell,
What's the indicating is that where you have this code:
$keywordsArray[] = $kwordsarray;...$kwordsarray is still an XML element rather than an actual string; so try using asXML, for example:
$keywordsArray[] = $kwordsarray->asXML();Cheers,
David.
Did what you said but I
Did what you said but I still only getting
a:1:{i:0;s:39:"";}Hi Russell, Try using:
Hi Russell,
Try using:
print_r($kwordsarray);...and that should reveal the sub-element containing the code that you are intending to add to $keywordsArray... If you're not sure how to structure the variable based on the output of print_r post an example of the output generated and I'll take a look...
Cheers,
David.
Got it working I had to
Got it working I had to remove the following.
$keywords = str_replace('<category><![CDATA[','', $keywords);$keywords = str_replace(']]></category>','', $keywords);
Hey David, Hope you don't
Hey David, Hope you don't mind me asking the following in the forum.
But the following RSS has a small error, which is stopping our RSS pinger.
I need away to recognize the following
<dc:creator />I am using the following to get the information what i need is if it see the above to put the $writtenby as
$writtenby = "unknown";<?php
$dc = $item->children('http://purl.org/dc/elements/1.1/');
$writtenby = $dc->creator;
$writtenby = str_replace(',',' and', $writtenby);
?>
Hi Russell, That should just
Hi Russell,
That should just be a case of checking $writtenby for an empty string, and setting it to "unknown" if that's the case, for example:
$writtenby = $dc->creator;if ($writtenby == "") $writtenby = "Unknown";
Cheers,
David.
What if a feed does not have
What if a feed does not have $dc->creator;
Like CNN news
http://rss.cnn.com/rss/edition_world.rss
Hi Russell, It should still
Hi Russell,
It should still work in the same way; however depending on your PHP error level setting you may get an unassigned variable warning. If that's what's happening, use isset() to check the variable before using it; for example:
$writtenby = "";if (isset($dc->creator))
{
$writtenby = $dc->creator;
}
if (!$writtenby)
{
$writtenby = "unknown";
}
This will handle all scenarios without any PHP warnings...
Cheers,
David.
thanks will try that out in
thanks will try that out in a second.
I don't know much about Preg_replace and just reading up on it.
How do i make sure it deletes any thing like the following
<category http"Domain="http://www.nytimes.com/namespaces/keywords/mdes"><category>
</category>
Basiclly if it has anything inside the
<category ...I want to make sure it deletes it but keeps what is in between the
<category></category>Sorry about all the questions.