单位做了个团购网站,现在需要在其他的网站上把这些团购信息显示出来,网上找了一段代码,不过有问题。几经修改,终于改好了,而且还能读出多个团购信息来,代码如下:
<?php
$file = "http://www.yourwebsite.com/api/baidu.php";
$g_urlset = array();
$g_elem = null;
$m=0;
$n=0;
function startElement( $parser, $name, $attrs ){
global $g_urlset, $g_elem;
if ( $name == 'ITEM' ) $g_urlset []= array();
$g_elem = $name; }
function endElement( $parser, $name ){
global $g_elem;
$g_elem = null; }
function textData( $parser, $text ){
global $g_urlset, $g_elem,$content,$m,$n;
if ( $g_elem == 'WEBSITE' || $g_elem == 'SITEURL' || $g_elem == 'CITY' || $g_elem == 'TITLE' || $g_elem == 'IMAGE' || $g_elem == 'STARTTIME' || $g_elem == 'ENDTIME' || $g_elem == 'VALUE' || $g_elem == 'PRICE' || $g_elem == 'REBATE' || $g_elem == 'BOUGHT')
{
$g_urlset[ count( $g_urlset ) - 1 ][ $g_elem ] = $text;
//echo $b=count( $g_urlset ) - 1;
//echo " ".$g_elem."<br>";
$content[$m][]=$text;
$n=$n+1;
if ($n>10){$m=$m+1;$n=0;}
}}
$parser = xml_parser_create();
xml_set_element_handler( $parser, "startElement", "endElement" );
xml_set_character_data_handler( $parser, "textData" );
if (!($f = fopen($file, "r ")))
{ die( "could not open XML ");}
while( $data = fread( $f, 4096 ) )
{
xml_parse( $parser, $data );
}
xml_parser_free( $parser );
for ($i=0; $i<$m; $i++){
echo "<table border='1'>";
echo "<tr><td>网站</td>";echo "<td>".$content[$i][0]."</td></tr>";
echo "<tr><td>网址</td>";echo "<td>".$content[$i][1]."</td></tr>";
echo "<tr><td>城市</td>";echo "<td>".$content[$i][2]."</td></tr>";
echo "<tr><td>题目</td>";echo "<td>".$content[$i][3]."</td></tr>";
echo "<tr><td>图片地址</td>";echo "<td>".$content[$i][4]."</td></tr>";
echo "<tr><td>开始时间</td>";echo "<td>".$content[$i][5]."</td></tr>";
echo "<tr><td>结束时间</td>";echo "<td>".$content[$i][6]."</td></tr>";
echo "<tr><td>原价</td>";echo "<td>".$content[$i][7]."</td></tr>";
echo "<tr><td>团购价</td>";echo "<td>".$content[$i][8]."</td></tr>";
echo "<tr><td>折扣</td>";echo "<td>".$content[$i][9]."</td></tr>";
echo "<tr><td>已购买人数</td>";echo "<td>".$content[$i][10]."</td></tr>";
echo "</table>";
}
?>