从REST API返回的未定义值

问题描述 投票:0回答:1

我正在尝试从此API获取数据但不幸的是我遇到了一些错误。但是我确实设法通过不同的API获得了一些成功。

这是我的PHP代码:

<?php
$url = "https://api.cryptonator.com/api/ticker/btc-usd";

$fgc = file_get_contents($url);
$json = json_decode($fgc, true);
$price = $json["price"];

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title> Bitcoin Widget</title>
 </head>
       <body>
       <div id="container">
        <table width ="100%">
            <tr>
                <td rowspan="3" id="blyat"><?php echo $price; ?></td>
            </tr>
        </table>
        </div>
       </body>
       </html>

当我包含这个API:https://www.bitstamp.net/api/v2/ticker/ltcusd/它工作正常。

但是当我添加这个API时:https://api.cryptonator.com/api/ticker/btc-usd

XAMPP给了我这个错误:

注意:未定义的索引:第7行的C:\ xampp \ htdocs \ BTCWIDGET \ indexx.php中的价格

什么可能导致此错误消息?

php json api
1个回答
-2
投票

你的索引文件真的拼写为indexx.php吗?您可能在api完成获取数据之前设置变量价格。这是竞争条件。

© www.soinside.com 2019 - 2024. All rights reserved.