使用php将银行rss提要获取到我的mysql数据库中


Getting a bank rss feed into my mysql database with php?

我需要一些帮助,将银行rss提要导入我的数据库。我试过几件事,但似乎就是做不到。

rss提要来自http://www.bankofcanada.ca/stats/assets/rates_rss/noon/en_all.xml

<?php
//Find and grab needed libraries and files.
require_once('proxy_bypass.php');
require_once ('config.php');
$url = $BOCRSS; // Bank of Canada RSS.
$rss = @simplexml_load_string(get_file($url)); // Get the rss feed data.
if($rss) {
 foreach($rss->item as $entry) { //For each RSS item in the rss xml file.
    $cb = $entry->children('http://www.cbwiki.net/wiki/index.php/Specification_1.1');
    //var_dump($cb);        
    //die();
    $code = $entry[targetCurrency];
    $curr = $entry[value];
    //echo $curr .' '. $code . '<br/>'; //Can be deleted - prints out data.
    $dbc = mysql_connect($db_host,$db_user,$db_password); //Connect to Shares.
    mysql_select_db($db_name, $dbc); //Select database.
    $qry = "INSERT INTO $db_table (currencycode,rate) VALUES ('$code', '$curr')"; //Creates the query.
    if (!$dbc){
    die('Could not connect: ' . mysql_error()); // Echo this is the connection to the database can't be made.
}
if (mysql_query($qry, $dbc)) {
    echo "Database created"; // Echo this if the RSS feed in placed in the database.
}
else {
    echo "Error creating database: " . mysql_error(); //Otherwise say this.
}
mysql_close($dbc); // Close the database connection.
}
} else echo "Error with RSS feed"; //Echo error if RSS is unreachable.
?>

我包含的两个文件让我通过了我所在大学的代理服务器,配置文件保存了我的mysql数据库详细信息和rss提要的url。

var转储给我的是:

object(SimpleXMLElement)#63(1){["statistics"]=>object(SimpleXML Element)#65(2 12:15:00-05:00"}

现在我是PHP的初学者,所以可能都错了。我正试图将"targetCurrency"answers"value"输入数据库,但我得到的只是50多个空行。这一定意味着数据库正在生成,但没有任何内容。如果有人能更改代码使其发挥作用,我将不胜感激,因为我曾试图让它发挥作用,但没有成功。

是否应该使用$cb(每个项的第一个子节点)而不是$element?

$code = $cb['targetCurrency'];
$curr = $cb['value'];

此外,请使用类似mysql_real_escape_string($rss_field_value)的方法来转义您在SQL查询中输入的值,以防止SQL注入。