无法一次从多个URL获取XML数据

时间:2016-07-14 10:38:16

标签: php mysql xml url

我的代码看起来像这样

channel_xpath = "//select[@id='80211Mode_5']/option[@value="+"\'"+channel+"\'"+"]" 

time.sleep(1) 

self.browser_driver.find_element_by_xpath(channel_xpath).click() 

我的abc.txt文件看起来像这样

<?php
include 'dbconnection.php';
include 'date.php';

function fetch_url_content()
{
    $conn = get_dbConnection();

    $fp = fopen("abc.txt", "r") or die("Unable to open file!");
    $count = 0;
    while (!feof($fp))
    {

    $url = fgets($fp);

    //read feed into SimpleXML object
    $sxml = simplexml_load_file($url);


    foreach($sxml->channel->item as $type){
    $description = $type->description;
    $dom = new DOMDocument;
    $dom->loadHTML($description);
    $a=array();

    foreach ($dom->getElementsByTagName('td') as $node){
    array_push($a,$node->nodeValue);
    }
    $a1 = $type->title;
    $a2 = addslashes($a[3]);
    $a3 = $a[5];
    $a4 = date_correction('30-'.$a[1]); 

    $sql = "INSERT IGNORE INTO   my_table(`c1`,`c2`,`c3`,`c4`) 
    VALUES ($a1,'$a2',$a3,'$a4')";
    $sth = $conn->prepare($sql);
    $sth->execute();

    $count++;
        }
    }
    echo "successfully inserted ".$count." entries to the DB";  
}
fetch_url_content();
?>

这里在上面的代码我试图采取第一个url进程,将数据插入到数据库中并对第二个三分之一执行相同操作。

但是这里我的代码只对最后一个url工作正常如果我改变abc.txt中url的顺序它只对最后一个url工作正常,并将以下错误提供给其他url

http://example.com/RssNAV.aspx?swise=y&mf=43
http://example.com/RssNAV.aspx?swise=y&mf=36
http://example.com/RssNAV.aspx?swise=y&mf=4
http://example.com/RssNAV.aspx?swise=y&mf=62

解决这个问题的修改应该是什么?

1 个答案:

答案 0 :(得分:0)

这可能是您的abc.txt文件中的行结尾的结果。 如果文件包含Windows换行符,请尝试使用Unix换行符保存。 如果不可能,您可以使用以下内容替换您的fgets()解决方案:

$file = fopen("abc.txt,"r");
$text = fread($file, filesize("abc.txt"));
$lines = explode(PHP_EOL, $text);
foreach($lines as $line)
{
   // Do something
}