Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:致命错误:无法重新声明startElement()_Php_Parsing_Xml Parsing - Fatal编程技术网

PHP:致命错误:无法重新声明startElement()

PHP:致命错误:无法重新声明startElement(),php,parsing,xml-parsing,Php,Parsing,Xml Parsing,由于某些原因,当我在PHP中尝试使用多个require()函数时,下面会出现此错误。基本上,我使用几个require()函数来访问几个xml解析器页面。 有人知道如何解决这个问题吗?如果这不是很好的描述,请在下面说,我会尝试解决它。非常感谢。我感谢任何积极的反馈。另外,我只是在学习PHP,所以请不要对我太苛刻。我将在下面提供以下代码 以下是错误: 致命错误:无法在第34行的/Applications/XAMPP/xamppfiles/htdocs/yournewsflow/news/sports

由于某些原因,当我在PHP中尝试使用多个require()函数时,下面会出现此错误。基本上,我使用几个require()函数来访问几个xml解析器页面。 有人知道如何解决这个问题吗?如果这不是很好的描述,请在下面说,我会尝试解决它。非常感谢。我感谢任何积极的反馈。另外,我只是在学习PHP,所以请不要对我太苛刻。我将在下面提供以下代码

以下是错误: 致命错误:无法在第34行的/Applications/XAMPP/xamppfiles/htdocs/yournewsflow/news/sports.php:27中重新声明startElement()

Here are the require functions: 

        <?php

        require("news/sports.php");
        require("news/political.php");
        ?>




Here is the xml parser used for a couple pages:
<?php
$tag = "";
$title = "";
$description = "";
$link = "";
$pubDate = "";
$show= 50;
$feedzero = "http://feeds.finance.yahoo.com/rss/2.0/category-stocks?region=US&lang=en-US"; $feedone = "http://feeds.finance.yahoo.com/rss/2.0/category-ideas-and-strategies?region=US&lang=en-US"; 
$feedtwo  = "http://feeds.finance.yahoo.com/rss/2.0/category-earnings?region=US&lang=en-US"; $feedthree = "http://feeds.finance.yahoo.com/rss/2.0/category-bonds?region=US&lang=en-US";
$feedfour  = "http://feeds.finance.yahoo.com/rss/2.0/category-economy-govt-and-policy?region=US&lang=en-US";

$insideitem = false;
$counter = 0;
$outerData;

function startElement($parser, $name, $attrs) {
    global $insideitem, $tag, $title, $description, $link, $pubDate;
    if ($insideitem) {
        $tag = $name;
    } elseif ($name == "ITEM") {
        $insideitem = true;
    } }
function endElement($parser, $name) {
    global $insideitem, $tag, $counter, $show, $showHTML, $outerData;
    global $title, $description, $link, $pubDate;
    if ($name == "ITEM" && $counter < $show) {
        echo "<table>
                <tr>
                  <td>

        <a href=\"".htmlspecialchars($description)."\">".htmlspecialchars($description)."</a>
                  </td>
                </tr>";


        // if you chose to show the HTML
        if ($showHTML) {
            $title = htmlspecialchars($title);
            $description = htmlspecialchars($description);
            $link = htmlspecialchars($link);
            $pubDate = htmlspecialchars($pubDate);

        // if you chose not to show the HTML
        } else {
            $title = strip_tags($title);
            $description = strip_tags($description);
            $link = strip_tags($link);
            $pubDate = strip_tags($pubDate);
        }

        // fill the innerData array
        $innerData["title"] = $title;
        $innerData["description"] = $description;
        $innerData["link"] = $link;
        $innerData["pubDate"] = $pubDate;

        // fill one index of the outerData array
        $outerData["data".$counter] = $innerData;

        // make all the variables blank for the next iteration of the loop
        $title = "";
        $description = "";
        $link = "";
        $pubDate = "";
        $insideitem = false;

        // add one to the counter
        $counter++;
    }
}

function characterData($parser, $data) {
    global $insideitem, $tag, $title, $description, $link, $pubDate;
    if ($insideitem) {
    switch ($tag) {
        case "TITLE":
        $title .= $data;
        break;
        case "DESCRIPTION":
        $description .= $data;
        break;
        case "LINK":
        $link .= $data;
        break;
        case "PUBDATE":
        $pubDate .= $data;
        break;
    }
    }
}


// Create an XML parser 
$xml_parser = xml_parser_create(); 

// Set the functions to handle opening and closing tags 
xml_set_element_handler($xml_parser, "startElement", "endElement"); 

// Set the function to handle blocks of character data 
xml_set_character_data_handler($xml_parser, "characterData"); 

// if you started with feed:// fix it to html://



// Open the XML file for reading
$feedzeroFp = fopen($feedzero, 'r') or die("Error reading RSS data."); 
$feedoneFp = fopen($feedone, 'r') or die("Error reading RSS data."); 
$feedtwoFp = fopen($feedtwo, 'r') or die("Error reading RSS data."); 
$feedthreeFp = fopen($feedthree, 'r') or die("Error reading RSS data."); 
$feedfourFp = fopen($feedfour, 'r') or die("Error reading RSS data."); 
// Read the XML file 4KB at a time 
while ($data = fread($feedoneFp, 4096)) 
//Parse each 4KB chunk with the XML parser created above 
    xml_parse($xml_parser,$data,feof($feedoneFp)) 
        //Handle errors in parsing 
        or die(sprintf("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($xml_parser)),
            xml_get_current_line_number($xml_parser))); 



// Close the XML file 
fclose($feedoneFp); 

while ($data = fread($feedtwoFp, 4096)) 
//Parse each 4KB chunk with the XML parser created above 
    xml_parse($xml_parser,$data,feof($feedtwoFp)) 
        //Handle errors in parsing 
        or die(sprintf("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($xml_parser)),
            xml_get_current_line_number($xml_parser))); 



// Close the XML file 
fclose($feedtwoFp); 
while ($data = fread($feedthreeFp, 4096)) 
//Parse each 4KB chunk with the XML parser created above 
    xml_parse($xml_parser,$data,feof($feedthreeFp)) 
        //Handle errors in parsing 
        or die(sprintfs("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($xml_parser)),
            xml_get_current_line_number($xml_parser))); 



// Close the XML file 
fclose($feedthreeFp); 
while ($data = fread($feedfourFp, 4096)) 
//Parse each 4KB chunk with the XML parser created above 
    xml_parse($xml_parser,$data,feof($feedfourFp)) 
        //Handle errors in parsing 
        or die(sprintf("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($xml_parser)),
            xml_get_current_line_number($xml_parser))); 



// Close the XML file 
fclose($feedfourFp); 



// Free up memory used by the XML parser 
xml_parser_free($xml_parser);

?>
以下是所需的功能:
以下是用于几页的xml解析器:
您不能多次需要相同的“解析器”,因为您已经在该文件中定义了函数。您需要重新构造代码:

在parser.functions.php中:

function startElement($parser, $name, $attrs) {
    global $insideitem, $tag, $title, $description, $link, $pubDate;
    if ($insideitem) {
        $tag = $name;
    } elseif ($name == "ITEM") {
        $insideitem = true;
    } }
function endElement($parser, $name) {
    global $insideitem, $tag, $counter, $show, $showHTML, $outerData;
    global $title, $description, $link, $pubDate;
    if ($name == "ITEM" && $counter < $show) {
        echo "<table>
                <tr>
                  <td>

        <a href=\"".htmlspecialchars($description)."\">".htmlspecialchars($description)."</a>
                  </td>
                </tr>";


        // if you chose to show the HTML
        if ($showHTML) {
            $title = htmlspecialchars($title);
            $description = htmlspecialchars($description);
            $link = htmlspecialchars($link);
            $pubDate = htmlspecialchars($pubDate);

        // if you chose not to show the HTML
        } else {
            $title = strip_tags($title);
            $description = strip_tags($description);
            $link = strip_tags($link);
            $pubDate = strip_tags($pubDate);
        }

        // fill the innerData array
        $innerData["title"] = $title;
        $innerData["description"] = $description;
        $innerData["link"] = $link;
        $innerData["pubDate"] = $pubDate;

        // fill one index of the outerData array
        $outerData["data".$counter] = $innerData;

        // make all the variables blank for the next iteration of the loop
        $title = "";
        $description = "";
        $link = "";
        $pubDate = "";
        $insideitem = false;

        // add one to the counter
        $counter++;
    }
}

function characterData($parser, $data) {
    global $insideitem, $tag, $title, $description, $link, $pubDate;
    if ($insideitem) {
    switch ($tag) {
        case "TITLE":
        $title .= $data;
        break;
        case "DESCRIPTION":
        $description .= $data;
        break;
        case "LINK":
        $link .= $data;
        break;
        case "PUBDATE":
        $pubDate .= $data;
        break;
    }
    }
}

这意味着已定义了功能
startElement
。不能有多个同名函数

$tag = "";
$title = "";
$description = "";
$link = "";
$pubDate = "";
$show= 50;
$feedzero = "http://feeds.finance.yahoo.com/rss/2.0/category-stocks?region=US&lang=en-US"; $feedone = "http://feeds.finance.yahoo.com/rss/2.0/category-ideas-and-strategies?region=US&lang=en-US"; 
$feedtwo  = "http://feeds.finance.yahoo.com/rss/2.0/category-earnings?region=US&lang=en-US"; $feedthree = "http://feeds.finance.yahoo.com/rss/2.0/category-bonds?region=US&lang=en-US";
$feedfour  = "http://feeds.finance.yahoo.com/rss/2.0/category-economy-govt-and-policy?region=US&lang=en-US";

$insideitem = false;
$counter = 0;
$outerData;

require_once('path/to/parser.functions.php');

// Create an XML parser 
$xml_parser = xml_parser_create(); 

// Set the functions to handle opening and closing tags 
xml_set_element_handler($xml_parser, "startElement", "endElement"); 

// Set the function to handle blocks of character data 
xml_set_character_data_handler($xml_parser, "characterData"); 

// if you started with feed:// fix it to html://



// Open the XML file for reading
$feedzeroFp = fopen($feedzero, 'r') or die("Error reading RSS data."); 
$feedoneFp = fopen($feedone, 'r') or die("Error reading RSS data."); 
$feedtwoFp = fopen($feedtwo, 'r') or die("Error reading RSS data."); 
$feedthreeFp = fopen($feedthree, 'r') or die("Error reading RSS data."); 
$feedfourFp = fopen($feedfour, 'r') or die("Error reading RSS data."); 
// Read the XML file 4KB at a time 
while ($data = fread($feedoneFp, 4096)) 
//Parse each 4KB chunk with the XML parser created above 
    xml_parse($xml_parser,$data,feof($feedoneFp)) 
        //Handle errors in parsing 
        or die(sprintf("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($xml_parser)),
            xml_get_current_line_number($xml_parser))); 



// Close the XML file 
fclose($feedoneFp); 

while ($data = fread($feedtwoFp, 4096)) 
//Parse each 4KB chunk with the XML parser created above 
    xml_parse($xml_parser,$data,feof($feedtwoFp)) 
        //Handle errors in parsing 
        or die(sprintf("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($xml_parser)),
            xml_get_current_line_number($xml_parser))); 



// Close the XML file 
fclose($feedtwoFp); 
while ($data = fread($feedthreeFp, 4096)) 
//Parse each 4KB chunk with the XML parser created above 
    xml_parse($xml_parser,$data,feof($feedthreeFp)) 
        //Handle errors in parsing 
        or die(sprintfs("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($xml_parser)),
            xml_get_current_line_number($xml_parser))); 



// Close the XML file 
fclose($feedthreeFp); 
while ($data = fread($feedfourFp, 4096)) 
//Parse each 4KB chunk with the XML parser created above 
    xml_parse($xml_parser,$data,feof($feedfourFp)) 
        //Handle errors in parsing 
        or die(sprintf("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($xml_parser)),
            xml_get_current_line_number($xml_parser))); 



// Close the XML file 
fclose($feedfourFp); 



// Free up memory used by the XML parser 
xml_parser_free($xml_parser);