Search code examples
laravel-8laravel-package

How to Install & Configure the Finnhub.io Package in Laravel 8


<?php

    require_once(__DIR__ . '/vendor/autoload.php');

    $config = Finnhub\Configuration::getDefaultConfiguration()->setApiKey('token', '');
    $client = new Finnhub\Api\DefaultApi(
        new GuzzleHttp\Client(),
        $config
    );

    print_r($client->stockSymbols("US"));
?>

Am trying to use the Finnhub.io but after installation in laravel 8i do not know exactly what to do. kindly help me on to configure a package after installing it. Am new to laravel packages so please kindly help on the processes of configuration.

https://packagist.org/packages/finnhub/client


Solution

  • So unfortunately this was not in the documentation and I had to dig into the actual Model located in the {projectroot}/vendor/finnhub/client/lib/Model/CompanyNews in your case {projectroot}/vendor/finnhub/client/lib/Model/StockSymbol

    In this file you will see that they return an object and all of the properties are encapsulated by the "protected" structure. I cannot go into crazy detail about that but just know the only way to access these properties and methods is from the class itself. Meaning That they exposed methods for you to retrieve the data from the response. I.E Getters.

    For Example: I created an api, so in my api when a certain url is called

    $router ->get('/api/news',APIController::class,'GatherNews');
    
    

    My GatherNews Function is called in the APIController Class:

    public function GatherNews()
        {
            
            $dotenv = Dotenv::createImmutable(__DIR__ . "../../../../");
            $dotenv->load();
            $apiKey= $_ENV['FINNHUB_API'];
    
            $config = \Finnhub\Configuration::getDefaultConfiguration()->setApiKey('token', $apiKey);
            $client = new \Finnhub\Api\DefaultApi(
                new \GuzzleHttp\Client(),
                $config
            );
            $responseObject=[];
            foreach($client->companyNews("AAPL", '2024-03-01', '2023-03-05') as $article)
            {
                    $object = new \stdClass(); //Dynamically create an Object
                    $object->category = $article->getCategory(); //Add Property
                    $responseObject[]=$object; //appendObject to Array 
    
            }
    
            header('Content-type: application/json');
            echo json_encode(['articles'=>$responseObject]); //send response
    
        }
    
    

    Now in the case of your question. You will want to do the following.

    foreach($client->stockSymbols("US") as $symbol)
            {
                    $object = new \stdClass(); //Dynamically create an Object
                    $object->description = $article->getDescription();
                    $object->display_symbol=$article->getDisplaySymbol();
                    $object->symbol = $article->getSymbol(); 
                    //...etc you should get the point 
    
                    $responseObject[]=$object; //appendObject to Array 
    
            }
    
    

    It was not asked but for kicks and giggles in Javascript you can now.

    var newsData = JSON.parse( '<?php echo json_encode($articles) ?>' );
    console.log(newsData);
    
    

    Then Done! Now its a old question but hope it helps the next person.