dongmu3269
dongmu3269
2017-07-22 16:49

知道如何连接到这个API吗?

已采纳

I tried to find any widget to show Tinkoof's bank currency rate, because it changes every 60sec., but nothing. Finally I found this API, but there is no any documentation for it.

I tried to find any articles about parsing, but I guess there's no use in that because of absence of any tag.

I need to show the currency rate on my website via this API. Any idea? Big thanks!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • drjk87189 drjk87189 4年前

    You just need to fetch the content You can use cURL or file_get_contents()

    cURL version:

    <?php
    
    $url = "https://www.tinkoff.ru/api/v1/currency_rates";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $r = curl_exec($curl);
    curl_close($curl);
    $array = json_decode($r, true);
    
    echo "<pre>";
    print_r($array);
    echo "</pre>";
    
    ?>
    

    file_get_contents version:

    <?php
    $r = file_get_contents('https://www.tinkoff.ru/api/v1/currency_rates');
    
    echo "<pre>";
    echo print_r(json_decode($r, true));
    echo "</pre>";
    ?>
    

    Both of them will work unless the remote website requires you to be human (has extra verifications to stop robot requests). cURL would be a better way if that were the case because you can fake a user agent using a header array.

    Once you have the array build it's just a matter of accessing the required data. using $r as an array result of the remote json structure.

    点赞 评论 复制链接分享
  • doujiang2641 doujiang2641 4年前

    It looks pretty straightforward to me. For someone with a decent knowledge of PHP will do this, provided the output is:

    enter image description here

    Now with the above information, I would:

    1. Get the result to PHP using file_get_contents().
    2. Parse the result as an array using json_decode($contents, $true).
    3. Using the above result, I would get display the value using: $output["payload"]["rates"][0]["buy"] or something similar.

    At this time of writing, the above will get me 58:

    enter image description here

    点赞 评论 复制链接分享

为你推荐