程序go 2020-01-28 14:29 采纳率: 100%
浏览 30

在PHP中查询AlphaVantage API

I'm trying to work with the alphavantage API to fetch the currency exchange data.

This is the query URI I'm using:

https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=USD&to_currency=JPY&apikey=demo

Below is the data the API returns:

{
    "Realtime Currency Exchange Rate": {
        "1. From_Currency Code": "USD",
        "2. From_Currency Name": "United States Dollar",
        "3. To_Currency Code": "JPY",
        "4. To_Currency Name": "Japanese Yen",
        "5. Exchange Rate": "108.99000000",
        "6. Last Refreshed": "2020-01-28 14:35:01",
        "7. Time Zone": "UTC",
        "8. Bid Price": "108.99000000",
        "9. Ask Price": "108.99000000"
    }
}

How would I query for this data in PHP?

  • 写回答

1条回答 默认 最新

  • weixin_33736048 2020-01-28 14:40
    关注

    Firstly JSON is a standard data format. PHP is a backend programming language. Basically the URL is returning JSON Format. You have to call the endpoint with an apikey. Because if you call now the api by a client call f.e with the jquery's AJAX request:

    $.get("<url>", function(data){
    console.log(data)
    })
    

    You will get this information:

    error: 
    responseText: "{↵    "Information": "The **demo** API key is for demo purposes only. Please claim your free API key at (https://www.alphavantage.co/support/#api-key) to explore our full API offerings. It takes fewer than 20 seconds, and we are committed to making it free forever."↵}"
    

    After adding you apikey you are able to receive your data.

    Please read this: for asynchronous calls to an endpoint: https://api.jquery.com/jquery.get/

    how to work with json: https://www.w3schools.com/js/js_json_intro.asp

    you will get a stringified json, to work with it you have to parse it, to work with it as an object. https://www.w3schools.com/js/js_json_parse.asp

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度