汇率查询接口如何用PHP实现调用?

一、什么是汇率查询接口?

提供实时汇率换算,外汇报价,货币汇率以及币种之间的汇率转换。分为四个子接口

  • 实时汇率查询换算

  • 单个货币列表

  • 货币列表

  • 银行汇率实时查询

二、如何用PHP进行对接?

下面我们用阿里云接口为例,具体的PHP代码示例如下:

接口地址:https://market.aliyun.com/apimarket/detail/cmapi00065831
<?php
    $host = "https://tsexchange.market.alicloudapi.com";
    $path = "/index";
    $method = "GET";
    $appcode = "你自己的AppCode";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "from=CNY&to=USD&money=1";
    $bodys = "";
    $url = $host . $path . "?" . $querys;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    var_dump(curl_exec($curl));
?>

返回代码示例:

{
  "code": 1,
  "msg": "操作成功",
  "data": {
    "from": "CNY",
    "from_name": "人民币",
    "to": "USD",
    "to_name": "美元",
    "exchange": "0.139265",
    "money": "0.139265",
    "updatetime": "2025-05-24 05:00:00"
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值