Hey... I really need some help here!
Ok, below is part of the code I am running. This works fine on my development server using PHP 5.0.4. But on the live server ( PHP 5.0.0 ) it gets stuck in some type of loop or something. It pretty much boggs down the system and the hosting place keeps shutting my site down because its on a shared plan.
I really need to find out what is causing this. If I comment out the (!xml_parse) part it doesnt get stuck, so the problem isnt with the while loop.. it has something to do with the xml parsing.
Anyways heres the code...
$found_mapped = false; // used to selectively print text of declared elements
$current = ''; // current element being processed
$currentJob = ''; // current job being processed
$cdataholder = ''; // holder variable for multiple calls to characterData()
$total_results = 0;
function startElement($parser, $name, $attrs)
{
global $text_array, $found_mapped, $current, $currentJob;
if (isset($text_array[$name]) || $currentJob != '') {
$found_mapped = true;
$current = $name;
}
if($name == "RESULT") {
$currentJob = new JobResult();
}
}
function endElement($parser, $name)
{
global $text_array, $current, $found_mapped, $cdataholder, $total_results, $currentJob, $jobResults, $end_display;
if (isset($text_array[$name])) {
if($current == "TOTALRESULTS") {
$total_results = $cdataholder;
settype($total_results, "integer");
$end_display = min($end_display, $total_results);
}
}
else if($currentJob != '' && $cdataholder != '') {
$currentJob->$current = $cdataholder;
}
if($name == "RESULT") {
array_push($jobResults, $currentJob);
$currentJob = '';
}
$found_mapped = false;
$cdataholder = '';
}
function characterData($parser, $data)
{
global $cdataholder, $found_mapped, $total_results;
if($found_mapped) $cdataholder .= $data;
}
$xml_parser = xml_parser_create(''); // empty string to autodetect input format
// 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", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($url, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
$data = str_replace('<?xml version=\"1.0\" encoding=\"UTF-8\" ?>',"",$data);
// Escape ampersands that aren't part of entities.
$data = preg_replace('/&(?!\w{2,6};)/', '&', $data);
// Remove all non-visible characters except SP, TAB, LF and CR.
$data = preg_replace('/[^\x20-\x7E\x09\x0A\x0D]/', "\n", $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);
}
Thanks alot!!