Bonjour,
Basé sur le tutorial suivant:
http://www.kirupa.com/web/xml_php_parse_beginner.htm
Je cherche à écrire un script PHP me permettant de récupérer les données d'un fichier XML généré par l'API de ma.gnolia pour les écrire dans une bdd MySQL.
--------------------
Voici un exemple de XML généré par ma.gnolia:
<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>
--------------------
Et voici mon script PHP:
<?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);
?>
--------------------
Tout fonctionne plus ou moins correctement et mes 'arrays' sont correctement générées:
Array ( [1] => Array ( [TITLE] => Writing Secure PHP Scripts => http://www.dagondesign.com/articles/writing-secure-php-scripts-part-1/] =>
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 )
--------------------
Sauf la valeur SCREENSHOT qui est tronquée. J'obtiens juste:
[SCREENSHOT] => s=6727eb4bc3bac6a2
A la place de:
[SCREENSHOT] => http://scst.srv.girafa.com/srv/i?i=sc010159&r=dagondesign.com/articles/writing-secure-php-scripts-part-1&s=6727eb4bc3bac6a2
--------------------
La valeur est coupée au niveau du '&'. Pourtant, dans le XML, l'ampersand '&' est bien transcrit sous la forme '&'. Donc, ça devrait fonctionner.
Une idée?
Merci,
Cary
Basé sur le tutorial suivant:
http://www.kirupa.com/web/xml_php_parse_beginner.htm
Je cherche à écrire un script PHP me permettant de récupérer les données d'un fichier XML généré par l'API de ma.gnolia pour les écrire dans une bdd MySQL.
--------------------
Voici un exemple de XML généré par ma.gnolia:
<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>
--------------------
Et voici mon script PHP:
<?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);
?>
--------------------
Tout fonctionne plus ou moins correctement et mes 'arrays' sont correctement générées:
Array ( [1] => Array ( [TITLE] => Writing Secure PHP Scripts => http://www.dagondesign.com/articles/writing-secure-php-scripts-part-1/] =>

--------------------
Sauf la valeur SCREENSHOT qui est tronquée. J'obtiens juste:
[SCREENSHOT] => s=6727eb4bc3bac6a2
A la place de:
[SCREENSHOT] => http://scst.srv.girafa.com/srv/i?i=sc010159&r=dagondesign.com/articles/writing-secure-php-scripts-part-1&s=6727eb4bc3bac6a2
--------------------
La valeur est coupée au niveau du '&'. Pourtant, dans le XML, l'ampersand '&' est bien transcrit sous la forme '&'. Donc, ça devrait fonctionner.
Une idée?
Merci,
Cary