1
私のコードは、この
<?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();
?>
私abc.txtファイルのように見える
は、上記のコードイムで、ここでこの
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
のようなものを取るしようとして見えます最初のURLを処理し、DBにデータを挿入し、2番目の3番目と同様に処理します。
が、ここに私のコードは、それが唯一の最後のURLのために正常に動作abc.txt私は、URLの中の順序を変更した場合にのみ、最後のURLのために正常に動作し、他のURLの
Warning: simplexml_load_file(): http://example.com/RssNAV.aspx?swise=y&mf=43%0D%0A:4: parser error : Extra content at the end of the document in C:\xampp\htdocs\v\xml\fetch_xml_data.php on line 21
Warning: simplexml_load_file(): <body> in C:\xampp\htdocs\v\xml\fetch_xml_data.php on line 21
に次のエラーを与えています
これを解決するための修正は何ですか?
をあなたの答えはほとんど働いていたアンダーソン@scottありがとうそれはそう代わりに私が試した最後に空の行を取るために使用されます$ url = str_replace(PHP_EOL、 ''、fgets($ fp));それはうまくいった。 – aries12