PHP如何获取货币汇率?

 时间:2026-02-15 15:43:01

1、编写输入页面;

//demo.html

<html><head>    <meta charset="utf-8">    <title>获取汇率</title></head><body style="margin-bottom: 10px;">   基本货币:<input  type="text" id="basicCoin" name="basicCoin"/>==>   <input  type="text" id="changeCoin" name="changeCoin"/>   <input  type="button" id="btn" name="btn" value="提交" onclick="req_server()"/>    <P id="result">    <script>    function req_server(){          //获取基本货币        var basicCoin=document.getElementById("basicCoin").value;        //获取兑换货币        var changeCoin=document.getElementById("changeCoin").value;                var xmlhttp;         //创建XMLHttpRequest对象          if (window.XMLHttpRequest){             xmlhttp=new XMLHttpRequest();          }else{              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");          }                    //设置请求类型,请求地址        xmlhttp.open("GET","deal.php?basicCoin="+basicCoin+"&changeCoin="+changeCoin,true);          //发送请求至服务器          xmlhttp.send();          //处理onreadystatechange事件          xmlhttp.onreadystatechange=function()          {              //结果处理            if (xmlhttp.readyState==4 && xmlhttp.status==200){                  var data= eval("("+xmlhttp.responseText+")");                alert(data.status);                if(data.status=="ok"){                    document.getElementById("result").innerHTML="兑换汇率为:"+data.ret;                }else{                    document.getElementById("result").innerHTML="处理错误!请检查!";                }            }          }                }    </script></body></html>    

PHP如何获取货币汇率?

PHP如何获取货币汇率?

2、运行效果:

PHP如何获取货币汇率?

3、php后台处理:

<?php    //获取参数    $basicCoin = $_GET['basicCoin'];    $changeCoin = $_GET['changeCoin'];    $result = json_encode(array("status"=>"err","ret"=>0));    if($basicCoin && $changeCoin){        $str=$basicCoin.$changeCoin;        $url="http://download.finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s={$str}=x";        $file = fopen($url,'r');        $arr=array();        while ($data = fgetcsv($file))        {            $arr[] = $data;        }        fclose($file);        if($arr){            //输出汇率            $result = json_encode(array("status"=>"ok","ret"=>$arr[0][1]));            exit($result);        }else{            exit($result);        }    }    exit($result);?>

PHP如何获取货币汇率?

4、点击进行测试,效果如下:

PHP如何获取货币汇率?

  • 辣椒炒南瓜怎么做
  • 健身小工具,药球使用实用动作教学
  • 酱香酒的功效与作用?
  • 夫妻性生活会不会传播湿疣
  • 美味桥底炒辣蟹
  • 热门搜索
    民族民风手抄报 手抄报标题艺术字 饮食安全手抄报内容 尊敬老人手抄报 家庭教育手抄报 生活中的小窍门手抄报 呵护心灵手抄报 心里手抄报 竞选大队委手抄报 戒烟手抄报图片