2012-02-02 91 views
0

我需要一些帮助获得一个银行RSS饲料到我的数据库。我尝试了一些东西,但我似乎无法让它工作。获取一个银行RSS饲料到我的MySQL数据库与PHP?

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源。

的变种转储给我此:

对象(的SimpleXMLElement)#63(1){[ “统计”] =>对象(的SimpleXMLElement)#65(2){[ “国”] => string(2)“CA”[“exchangeRate”] => object(SimpleXMLElement)#66(5){[“value”] => string(6)“1.0029”[“baseCurrency”] => string(3)“ CAD“[”targetCurrency“] =>字符串(3)”USD“[”rateType“] =>字符串(24)”加拿大银行中午利率“[”observationPeriod“] =>字符串(25) 01T12:15:00-05:00“}}}

现在我是PHP的初学者,所以它可能都是错误的。我试图让'targetCurrency'和'value'进入我的数据库,但我得到的是50多行空行。它必须意味着数据库正在生成,但没有任何进展。如果任何人都可以更改代码以使其正常工作,我将不胜感激,因为我试图让它正常工作,但无济于事。

+0

**警告**您的代码易受sql注入攻击。 – 2012-02-02 14:04:38

回答

0

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

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

另外,请使用类似mysql_real_escape_string($rss_field_value)逃脱你把值SQL查询,以防止SQL注入。

+0

我将旧代码更改为“code = $ cb ['targetCurrency']; + $ curr = $ cb ['value'];”。然后,我做了一个变量转储以查看发生了什么。它们都返回NULL。我的空行问题仍然存在。无论如何,你所说的话会留下来,因为它似乎是一个合乎逻辑的答案。谢谢。 – Zyble 2012-02-02 14:56:43