Hello,
Based on this tutorial from KIRUPA, I’m currently trying to write an XML parser to parse an XML file created by the ma.gnolia API because I want to put the different values in a MySQL database.
Here is an example of the datas generated by the ma.gnolia API:
<bookmark private="false" created="2007-07-04T01:09:21-07:00" rating="0" updated="2007-07-04T01:08:46-07:00" id="yogusefu" owner="ozskry">
<title>Writing Secure PHP Scripts</title>
<url>http://www.dagondesign.com/articles/writing-secure-php-scripts-part-1/</url>
<description>The most important element of any PHP script is security. In this article I will be discussing several methods of securing variables in PHP, with special regard to user input.</description>
<screenshot>http://scst.srv.girafa.com/srv/i?i=sc010159&r=dagondesign.com/articles/writing-secure-php-scripts-part-1&s=6727eb4bc3bac6a2</screenshot>
<tags>
<tag name="tutorials"/>
<tag name="php"/>
<tag name="security"/>
</tags>
</bookmark>
And here is my PHP script:
<?php
function start_element($parser, $element_name, $element_attrs)
{
global $array_number, $current_element;
if($element_name == 'TITLE')
{
$array_number++;
}
$current_element = $element_name;
}
function character_data($parser, $data)
{
global $current_data;
$current_data = $data;
}
function end_element($parser, $element_name)
{
global $array_number, $current_element, $current_data, $array_key, $magnolia;
$magnolia[$array_number][$current_element]=$current_data;
print_r ($magnolia);
}
$parser = xml_parser_create();
xml_set_element_handler($parser, 'start_element', 'end_element');
xml_set_character_data_handler($parser, 'character_data');
$fp = fopen('http://ma.gnolia.com/api/rest/1/bookmarks_find?api_key=4d7401f2f813fe95d51f60b879ed38f14866ebaa&person=ozskry', 'r') or die();
while ($data = fread($fp, 4096))
{
xml_parse($parser, $data, feof($fp))
or die(sprintf('', xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
?>
All is working fine and my arrays are correctly created:
Array ( [1] => Array ( [TITLE] => Writing Secure PHP Scripts => http://www.dagondesign.com/articles/writing-secure-php-scripts-part-1/ [DESCRIPTION] => The most important element of any PHP script is security. In this article I will be discussing several methods of securing variables in PHP, with special regard to user input. [SCREENSHOT] => s=6727eb4bc3bac6a2 )
Except that the output I get from the SCREENSHOT value is truncated. I have just:
[SCREENSHOT] => s=6727eb4bc3bac6a2
In place of:
[SCREENSHOT] => http://scst.srv.girafa.com/srv/i?i=sc010159&r=dagondesign.com/articles/writing-secure-php-scripts-part-1&s=6727eb4bc3bac6a2
As you can see in the XML code above, the ampersand is correctly escaped with ‘&’.
So, what’s wrong?
Thanks,
Cary