I have a file that is ftp'd to my root and it's in .gz format. I need to uncompress and read/parse xml string. I run and it bombs on fopen. Is there a different way of doing it?
zdemo.php
rss.phpPHP Code:<?php
$feed_max = 2;
$topheader = "News";
$feed_path = dirname(__FILE__) . "\Catalog_1.xml.gz";
include('rss.php');
?>
Result of running zdemo.phpPHP Code:<div class="rss">
<h5><?php echo $topheader; ?></h5>
<?php
//set a max value of 3 listings if variable is empty
if (empty($feed_max)){
$feed_max=3;
}
$ip=$_SERVER['REMOTE_ADDR'];
$file = $feed_path;
$map_array = array(
"BOLD" => "B",
"EMPHASIS" => "I",
"LITERAL" => "TT"
);
$current = "";
$sLink = "";
$sSiteHost = "";
$sDescription = "";
$sTitle = "";
$GLOBALS['iStart'] = 0;
$GLOBALS['iRowCount'] = 0;
$GLOBALS['ifeed_max'] = $feed_max;
function startElement_mioutdoor($parser, $name, $attrs)
{
$name = strtoupper($name);
global $current;
$current = $name;
if ($name == "ITEM") { echo "<p>"; }
if ($name == "LISTING") { echo ""; }
if ($name == "TITLE") { echo ""; }
if ($name == "DESCRIPTION") { echo ""; }
if ($name == "PUBDATE") { echo ""; $GLOBALS['iStart'] = 1; }
}
function endElement_mioutdoor($parser, $name)
{
$name = strtoupper($name);
if ($name == "DESCRIPTION") { echo ""; }
if ($name == "LISTING") { echo ""; }
if ($name == "TITLE") { echo ""; }
if ($GLOBALS['iStart'] == 1) {
if ($GLOBALS['iRowCount'] < $GLOBALS['ifeed_max']) {
if ($name == "ITEM") {echo "<p><a href=\"" . $GLOBALS['sLink'] . "\" target=\"_blank\">" . $GLOBALS['sTitle'] . "</a><br />" . $GLOBALS['sDescription'] . "</p>"; $GLOBALS['sTitle'] = ""; $GLOBALS['sDescription'] = ""; $GLOBALS['iRowCount'] ++;
}
}
}
if ($name == "ITEM") { echo "</p>"; }
}
function characterData_mioutdoor($parser, $data)
{
global $current;
if (!(ord($data) == 10)){
if ($GLOBALS['iStart'] == 1) {
if ($current == "TITLE") { $GLOBALS['sTitle'] .= $data; }
if ($current == "DESCRIPTION") { $GLOBALS['sDescription'] .= $data; }
if ($current == "LINK") { $GLOBALS['sLink'] = $data; }
if ($current == "URL") { $GLOBALS['sSiteHost'] = $data; }
}
}
}
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement_mioutdoor", "endElement_mioutdoor");
xml_set_character_data_handler($xml_parser, "characterData_mioutdoor");
//NEW LINE TO TAKE .gz FILE AND UNPACK
$file = gzfile_get_contents($file);
//THIS IS WHERE IT'S ERRORING
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
$data = convToUtf8($data);
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
function gzfile_get_contents($filename, $use_include_path = 0)
{
//File does not exist
if( !@file_exists($filename) )
{ return false; }
//Read and imploding the array to produce a one line string
$data = gzfile($filename, $use_include_path);
$data = implode($data);
return $data;
}
?>
</div>
Catalog_1.xml.gz (partial)HTML Code:Warning: fopen(<?xml version="1.0" e........ in D:\www\domain.us\httpdocs\rss.php could not open XML input
HTML Code:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE product_catalog SYSTEM "http://www.cj.com/downloads/tech/dtd/product_catalog_1_1.dtd"> <catalog> <product> <programname>U.S. Cavalry Affiliate Program</programname> <programurl>http://www.uscav.com</programurl> <catalogname>Product Catalog 1</catalogname> <lastupdated>02/04/2009</lastupdated> <name>FOX&reg; Response Vest</name> <keywords>Response Vests AH1004 AE1004 AA1004 AH AE AA 1004 aj aj1004 Law Enforcement Protective Gear Black HKC1004 JBC1004 JBC 1004 JFC1004 JFC 1004 HKC1004 HKC 1004 JFC1004 JFC 1004 JLC1004 JLC 1004 lbv LBV LBV kdm1004 kdm 1004 kdl1004 kdl 1004 KDF1004 KDF 1004 LGC LGC1004 LKG1004 LKC1004 fox military gear</keywords> <description>4 mag pockets, 2 utility pockets, and snap loops for hook-up to a pistol belt.</description> <sku>1004</sku> <manufacturer>FOX OUTDOOR PRODUCTS</manufacturer> <manufacturerid>1</manufacturerid> <currency>USD</currency> <price>30.99</price> <buyurl>http://www.dpbolvw.net/click-3594573-10273726?url=http%3A%2F%2Fwww.uscav.com%2Fproductinfo.aspx%3FTabID%3D548%26productid%3D5287%26cm_mmc%3DCJ-_-Affiliate-_-ProductCatalog-_-1004</buyurl> <impressionurl>http://www.ftjcfx.com/image-3594573-10273726</impressionurl> <imageurl>http://www.uscav.com/prodinfo/images/1004.jpg</imageurl> <instock>YES</instock> </product> </catalog>




Reply With Quote