【问题标题】:Problem with reading api giving json data读取提供 json 数据的 api 的问题
【发布时间】:2019-03-01 21:09:39
【问题描述】:

我想为自己编写一个网页,显示来自公司的一些数据。 我已经找到了一个显示不同值的 API,人们可以选择。

https://financialmodelingprep.com/developer/docs#Companies-Financial-Statements

所以我开始整理我的网页并添加了一个输入。这样用户就可以选择他正在搜索的公司。

<input type="text" name="Ticker" placeholder="Ticker Symbol">

然后在文件的末尾我添加了一个 php 部分,它从 URL 中选择数据(JSON):

<?php
            if(isset($_GET['Ticker']))
            {
                $Ticker_Name = $_GET['Ticker'];
                echo '<p>Your choosen company is '. $Ticker_Name . '</p>';
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, 'https://financialmodelingprep.com   /api/financials/income-statement/' . $Ticker_Name);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_exec($ch);
                curl_close($ch);
            }           
        ?>

这会显示从 api 提供的原始信息。 但我遇到的问题是格式化/访问 API 给出的 json 数据。

我最后尝试的是:

<?php
            if(isset($_GET['Ticker']))
            {
                $Ticker_Name = $_GET['Ticker'];
                echo '<p>Your choosen company is '. $Ticker_Name . '</p>';
                $url = file_get_contents('https://financialmodelingprep.com/api/financials/income-statement/' . $Ticker_Name);
                //$daten = file_get_contents($url);
                $json = json_decode($url, true);

                echo $json->Revenue;
            }   
?>  

收到此消息:

注意:试图获取非对象的属性“收入”

不显示任何数据。

如果有人可以帮助我,我将不胜感激,在此先感谢。

【问题讨论】:

  • file_get_contents 是否从 URL 返回任何数据?
  • 您是否将allow_url_fopen 设置为true? stackoverflow.com/a/15617547/571723
  • $Ticker_Name 是实际的股票代码吗?
  • 去掉truejson_decode 的参数。这使它返回一个关联数组,而不是一个对象。
  • 或使用$json['Revenue']

标签: php html json api


【解决方案1】:

发生这种情况是因为您在 json_decode 中传递 true 这意味着它返回关联数组而不是对象,您可以像 echo $json[$Ticker_Name 一样访问它]['Revenue']['2013-09']print_r($json[$Ticker_Name]['Revenue']) 像这样。

试试这个代码

片段

<?php
            if(isset($_GET['Ticker']))
            {
                $Ticker_Name = $_GET['Ticker'];
                echo '<p>Your choosen company is '. $Ticker_Name . '</p>';
                $url = file_get_contents('https://financialmodelingprep.com/api/financials/income-statement/' . $Ticker_Name);
                $url = str_replace('<pre>','',$url); //Removing <pre> html tag
                $json = json_decode($url, true);
                print_r($json[$Ticker_Name]['Revenue'];
            }   
?>  

注意:此 API 返回一个无效的 json,它在响应的开头添加 &lt;pre&gt; 标签。因此,json_decode 总是无法解码,您需要使用 str_replace 删除该标签。我已添加 $url = str_replace('&lt;pre&gt;','',$url); 这一行以使其正常工作。

【讨论】:

    【解决方案2】:

    感谢您的回答,它正在工作。

    但是,如果我尝试在该输入框附近放置另一个输入框,以提供用户正在搜索网页的值不再更新所选输入的可能性。 它不再更新了。

            <label>Enter Ticker Symbol</label>
            <input type="text" name="Ticker" placeholder="Ticker Symbol">
            <label>Enter your Value</label>
            <input type="text" name="Value" placeholder="Value">
    
    <?php
            if(isset($_GET['Ticker']))
            {
                    $Ticker_Name = $_GET['Ticker'];
                    $Value = $_GET['Value'];
                    echo '<p>Your choosen company is '. $Ticker_Name . '</p>';
                    echo '<p>Your choosen value is '. $Value . '</p>';
                    $url = file_get_contents('https://financialmodelingprep.com/api/financials/income-statement/' . $Ticker_Name);
                    $url = str_replace('<pre>','',$url); //Removing <pre> html tag
                    $json = json_decode($url, true);
                    print_r($json[$Ticker_Name][$Value]);
            }   
    ?>  
    

    非常感谢。

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 2016-06-03
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多