PHP gives you a simple way to read XML data. Look into the xml_... functions! Let me get you started! This is a genirec XML paser, that will build an array of the data from the XML feed so you can use it in your script or program! Now I would would write this as a class, but then again a simple function include file will work for this example! I will try to explain each process, but message board is really no place to explain the vast knowledge you need to learn to do this sort of stuff!
Lets begin!
I first need to create are XML include file! (2) of the functions in that file will be the core XML processing functions! The other will be a specific function to process the final result array returned by the XML functions! This thrid function should contain how you would like to have the feeds output displayed! Seeing I am so nice, , I will give you an example of that too! But you can change it to what you want to use!
the script...
// links.php
[php]
<?
define ( 'IS_INCLUDED', 1 );
include_once ( './xml_get.php' );
$auth = '987654321'; // only url query containing this ?id=$auth, can access this script!
if ( !empty ( $_GET ) && $_GET == $auth )
{
$out = file_get_contents ( $url ) or die (); // die or call a error function then exit();
$out = process_xml ( $out ); // process this request
echo show_xml ( $out ); // show the pretty results!
}
?>
[/php]
now the include file, the heart of the system!
// the include file
xml_get.php
[php]
<?
if ( !defined ( 'IS_INCLUDED' ) )
{
exit ();
}
// this will begin the XML paser
// it looks for a certain structure and prosess the feed based on what
// type of structure it has found!
// this is called by process_xml and it is a recursive function that processes
// each part of the XML feed, if the array is multi dimensional it recalls it's
// self so that it can merge each multi dimensional array into a single element
// type array! There are a few other ways you can do this, but keeping it generic
// will allow you to do (2) things, (1) use the least amount of server resources
// and (2), be able to use these 2 same functions to process any XML feed you want!
Sorry I did not want to write any fancy HTML, so the output does not look that great, but all you have to do is change the show_xml() to make it display the way you like!
Remember what I told you, the first 2 functions ( process_xml, get_extended) will process any XML data file, so you can use them anywhere you need to build a output array on some XML data!
Comments
Seems easy enough....
PHP gives you a simple way to read XML data. Look into the xml_... functions! Let me get you started! This is a genirec XML paser, that will build an array of the data from the XML feed so you can use it in your script or program! Now I would would write this as a class, but then again a simple function include file will work for this example! I will try to explain each process, but message board is really no place to explain the vast knowledge you need to learn to do this sort of stuff!
Lets begin!
I first need to create are XML include file! (2) of the functions in that file will be the core XML processing functions! The other will be a specific function to process the final result array returned by the XML functions! This thrid function should contain how you would like to have the feeds output displayed! Seeing I am so nice, , I will give you an example of that too! But you can change it to what you want to use!
the script...
// links.php
[php]
<?
define ( 'IS_INCLUDED', 1 );
include_once ( './xml_get.php' );
$auth = '987654321'; // only url query containing this ?id=$auth, can access this script!
$url = 'http://www.xtremecoders.com/index.php?rss';
if ( !empty ( $_GET ) && $_GET == $auth )
{
$out = file_get_contents ( $url ) or die (); // die or call a error function then exit();
$out = process_xml ( $out ); // process this request
echo show_xml ( $out ); // show the pretty results!
}
?>
[/php]
now the include file, the heart of the system!
// the include file
xml_get.php
[php]
<?
if ( !defined ( 'IS_INCLUDED' ) )
{
exit ();
}
// this will begin the XML paser
// it looks for a certain structure and prosess the feed based on what
// type of structure it has found!
function process_xml ( $in )
{
$xml = xml_parser_create ( 'ISO-8859-1' );
xml_parser_set_option ( $xml, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct ( $xml, $in, $value, $name );
xml_parser_free ( $xml );
$x = 0;
$part = array ();
if ( !empty ( $value[$x] ) )
{
$part[$value[$x]][] = $value[$x];
$name = ( count ( $part[$value[$x]] ) - 1 );
$part[$value[$x]][$name] = array_merge ( $part[$value[$x]][$name], get_extended ( $value, $x ) );
}
else
{
$part[$value[$x]][] = get_extended ( $value, $x );
}
return ( $part );
}
// this is called by process_xml and it is a recursive function that processes
// each part of the XML feed, if the array is multi dimensional it recalls it's
// self so that it can merge each multi dimensional array into a single element
// type array! There are a few other ways you can do this, but keeping it generic
// will allow you to do (2) things, (1) use the least amount of server resources
// and (2), be able to use these 2 same functions to process any XML feed you want!
function get_extended ( $value, &$x )
{
$node = array ();
if ( !empty ( $value[$x] ) )
{
$node = $value[$x];
}
while ( ++$x < count ( $value ) )
{
switch ( $value[$x] )
{
case 'cdata':
if ( !empty ( $node ) )
{
$node .= $value[$x];
}
else
{
$node = $value[$x];
}
break;
case 'complete':
if ( !empty ( $value[$x] ) )
{
$node[$value[$x]][] = $value[$x];
$name = ( count ( $node[$value[$x]] ) - 1 );
$node[$value[$x]][$name] = !empty ( $value[$x] ) ? $value[$x] : '';
}
else
{
$node[$value[$x]][] = !empty ( $value[$x] ) ? $value[$x] : '';
}
break;
case 'open':
if ( !empty ( $value[$x] ) )
{
$node[$value[$x]][] = $value[$x];
$name = ( count ( $node[$value[$x]] ) - 1 );
$node[$value[$x]][$name] = array_merge ( $node[$value[$x]][$name], get_extended ( $value, $x ) );
}
else
{
$node[$value[$x]][] = get_extended ( $value, $x );
}
break;
case 'close':
return ( $node );
}
}
}
// the function to display the output!
// you would change the HTML in this function!
function show_xml ( $in )
{
$out = $in[0][0];
$data = "<html>
<head>
<title>XML OUTPUT EXAMPLE</title>
<style>
body ( background-color : #ffffff; font-size : 11px; color : #000000; margin : 10px; padding : 4px }
.border { background-color : #ffffff; border : 2px solid #182842; padding : 4px }
.header { background-color : #182842; color : #ffffff; padding : 4px; }
.info { background-color : #c0c0c0; color : #000000; padding : 4px; font-size : 12px }
.link { text-decoration : none; color : #ffff00 }
</style>
</head>
<body>
<table width='100%' cellpadding='2' cellspacing='1'>
<tr>
<td width='50%'></td>
<td class='border'>
<table width='460' cellpadding='0' cellspacing='1' bgcolor='#ffffff'>
<tr>
<td width='100%' class='header'><span style='float : right'><a class='link' href='" . $out[0] . "' target='_SELF'>" . $out[0] . "</a></span>" . $out[0] . "</td>
</tr>";
for ( $i = 0; $i < sizeof ( $out ); $i++ )
{
$data .= "\r\n <tr>
<td width='100%' class='info'>
<span style='float : right'><a class='link' href='" . $out[$i][0] . "' target='_SELF'>[Read]</a></span><strong>" . $out[$i][0] . "</strong>
<br />
" . $out[$i][0] . "...
<br />
<br />
</td>
</tr>";
}
$data .= " </table>
<td width='50%'></td>
</td>
</tr>
</table>
</body>
</html>";
return ( $data );
}
?>
[/php]
Sorry I did not want to write any fancy HTML, so the output does not look that great, but all you have to do is change the show_xml() to make it display the way you like!
Then to run the script....
hXXp://www.your_site.com/links.php?id=987654321
Example....
http://www.ya-right.com/links.php?id=987654321
If you have questions just ask...
Remember what I told you, the first 2 functions ( process_xml, get_extended) will process any XML data file, so you can use them anywhere you need to build a output array on some XML data!
Sonia!