douwen5546 2015-07-22 03:39
浏览 44

使用php和simplexml_load_file从xml中提取外汇数据

<?php
$url = "http://rates.fxcm.com/RatesXML";
$xml = simplexml_load_file($url);
print_r($xml);
?>

This script really works and the output is here:

http://sitoxte.com/test%20mercato/array.php

But what I want to do is put this data to MySQL tabs, I need some help because I want to store the data each minute.

So i Try:

json-to-mysql

but I think that array in xml variable is not adeguate.

I want create a for cicle

and create 69 table like this:

  table 1 eur/usd 
id -  Bid - Ask - High - Low - Direction - Last - timestamp
1
2
3
4...
and so on 

refresh mode is simple and I do in this way whit javascript:

    <script>
setInterval(function () {}, 3000);
var myVar=setInterval(function(){myTimer()},10000);
function myTimer() 
{
    var d = new Date();
    document.getElementById("demo").innerHTML = d.toLocaleTimeString();
    location.reload();
}
    </script>

connetion is simple too and is like this:

//Host: 
$localhost="******";
//database
 $mioblog=*******;
//Nome utente: 
$username=********;
//Password: 
$password=*******;
// connessione a MySQL con l'estensione MySQLi
$mysqli = new mysqli("$localhost", "$username", "$password", $mioblog);
// verifica dell'avvenuta connessione
if (mysqli_connect_errno()) {
// notifica in caso di errore
echo "Errore in connessione al DBMS: ".mysqli_connect_error();
// interruzione delle esecuzioni i caso di errore
exit();
}
else 
{
// notifica in caso di connessione attiva
echo "Connessione avvenuta con successo";
}
  • 写回答

3条回答 默认 最新

  • drix47193 2015-07-22 04:19
    关注

    This depends on what you're using for your database, but assuming PDO, something like the below. If you need more details then look up a MySQL tutorial, but hopefully this gets you started and shows you how to traverse the XML.

    foreach($xml->Rate as $rate) {
        $query = "INSERT INTO tblrate (time, symbol, bid) VALUES (NOW(), :symbol, :bid)";
        $query = $pdo->prepare($query);
        $query->execute(array(
            ':symbol' => $rate->@attributes['Symbol'],
            ':bid'    => $rate->Bid;
        ));
    }
    

    edit: If you only need one currency, something like this should work

    foreach($xml->Rate as $rate) {
        if($rate->@attributes['Symbol'] == 'EURUSD') {
            $query = "INSERT INTO tblrate (time, bid) VALUES (NOW(), :bid)";
            $query = $pdo->prepare($query);
            $query->execute(array(
                ':bid'    => $rate->Bid;
            ));
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line