douqianrou9079 2015-10-25 15:37
浏览 12
已采纳

如何在API中获取单个值

How will i be able to get a single value out of BTC-e API using PHP?. This is the current API that i can use.

https://btc-e.com/api/3/ticker/btc_usd

As you can see, it shows lots of information, but i dont need many information that is displayed there. What can i do so that i will only get

"last":284.323

this number?

Thank you for your help regarding this subject.

  • 写回答

1条回答 默认 最新

  • douhuanbai6729 2015-10-25 15:50
    关注

    As you can see, the received data is a JSON value.

    If you simply want to receive data from a website, you can use the file_get_contents function.

    With the json_decode function, you can create an object from this data.

    If you're looking for a specific value, you maybe can use print_r to get a clarifying list of the given data.

    When you use the print_r function on the given data, it will return:

    stdClass Object ( [btc_usd] => stdClass Object ( [high] => 288.957 [low] => 272.00201 [avg] => 280.479505 [vol] => 2178508.65397 [vol_cur] => 7771.83325 [last] => 286.322 [buy] => 286.328 [sell] => 286.322 [updated] => 1445787588 ) )

    As you can see, you are looking for a variable stored in ->btc_usd->last

    So when it all comes together, you can use the following:

    <?php
    # Get String
    $_ApiString = '{"btc_usd":{"high":288.957,"low":272.00201,"avg":280.479505,"vol":2178508.65397,"vol_cur":7771.83325,"last":286.322,"buy":286.328,"sell":286.322,"updated":1445787588}}';
    
    # Or maybe you simply want to download it from a website
    $_ApiString = file_get_contents('https://btc-e.com/api/3/ticker/btc_usd');
    
    # Create object from ApiString
    $_Object    = json_decode($_ApiString);
    
    # Get variable
    $_Last      = $_Object->btc_usd->last;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效