1

i would like to program myself a webpage that displays some data from companies. I already found a API that displays different values, which someone could choose.

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

So i started putting together my webpage and added an input. In that way the user could choose which company he is searching for.

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

Then at the end of the file i added a php part that picks that data(JSON) from the URL:

<?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);
            }           
        ?>

This displays the raw informations given from the api. But the problem i have is formatting/accessing the json data given from the API.

The last thing I tried is:

<?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;
            }   
?>  

Getting this message:

Notice: Trying to get property 'Revenue' of non-object

without showing anything on data.

I would really appreciate if somebody could help me, thanks in advance.

User123456
  • 11
  • 1

2 Answers2

0

This is happening because your are passing true in json_decode which mean it return associative array not object, you can access it like echo $json[$Ticker_Name]['Revenue']['2013-09'] or print_r($json[$Ticker_Name]['Revenue']) like this.

Try this code

Snippet

<?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'];
            }   
?>  

Note: This API is returning an invalid json, its add <pre> tag at the beginning of response. So, json_decode always failed to decode it, You need to remove that tag by using str_replace. I've added $url = str_replace('<pre>','',$url); this line to make it work.

Shahnawaz Kadari
  • 1,423
  • 1
  • 12
  • 20
0

Thank you for your answer, it is working.

But if i try putting another input box, near this one, to give the possibility which value the user is searching for the webpage is not updating the choosen input anymore. It does not update anymore.

        <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]);
        }   
?>  

Thank you very much.

User123456
  • 11
  • 1