KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Use of API in the Dashboard Framework #2537

Closed Andre Van Der Walt opened this topic on on Jan 14 - 11 comments

Andre Van Der Walt commented on Jan 14

Good day

Can you please assist with some guidance or report structure of using an API within the Dashboard framework?

Can you set it up as a data source like one would with the Automaker?

KoolReport commented on Jan 14

Hi Andre,

First off, thank you very much for your renewal. Very appreciated!

Could you please describe more about API you would like to use. As I understand, you would like to have a route which can return json as result, right.

Andre Van Der Walt commented on Jan 15

Hi

No problem, thank you for the great product.

Correct, under normal circumstances, I would create a class (report) that extends my widget type and includes fetching data from a separate class using Automaker. We then create another class that extends Dashboard and create the above "report" in said class.

If possible, we would like to use the same process by replacing Automaker with an API data source that provides the data in JSON format.

Happy to provide more info if needed.

Andre Van Der Walt commented on Jan 15

This is what I was thinking of:

TestAPIDataSource.php

<?php

namespace App\TestAPIFolder;

use koolreport\datasources\APIDataSource;

class TestAPIDataSource extends APIDataSource

{
    protected function settings(){

        return array(
            "dataSources"=>array(
                "apiarray"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                    "dataFormat"=>"table",
                )
            )
        );
    }
    protected function setup(){

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.publicapis.org/entries',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        echo $response;

        $json = $response;
        $arr = json_decode($json, true);
        $output = [];
        foreach($arr["entries"] as $entries)
        {
            array_push($output,[
                "API"=>$arr["entries"]["API"],
                "Description"=>$arr["entries"]["Description"],
                "Auth"=>$arr["entries"]["Auth"],
                "HTTPS"=>$arr["entries"]["HTTPS"],
                "Cors"=>$arr["entries"]["Cors"],
                "Link"=>$arr["entries"]["Link"],
                "Category"=>$arr["entries"]["Category"],
            ]);
        }

        return([
            "entries"=>$output
        ]);
    }

}
?>

TestAPIReport.php

<?php

namespace App\TestAPIFolder\TestAPI;

use App\TestAPIFolder\TestAPIDataSource;
use koolreport\dashboard\fields\Text;
use koolreport\dashboard\widgets\Table;

class TestAPIReport extends Table
{
    protected function dataSource()
    {
        return array(TestAPIDataSource::create());
    }

    protected function fields()
    {
        return [
            Text::create("API"),
            Text::create("Description"),
            Text::create("Auth"),
            Text::create("HTTPS"),
            Text::create("Cors"),
            Text::create("Link"),
            Text::create("Category"),
        ];
    }
}
?>

TestAPIReportDashboard.php

<?php

namespace App\TestAPIFolder\TestAPI;

use \koolreport\dashboard\Dashboard;
use \koolreport\dashboard\containers\Row;
use \koolreport\dashboard\containers\Panel;
use \koolreport\dashboard\widgets\Text;


class TestAPIReportBoard extends Dashboard
{
    protected function widgets()
    {
        return [
            Text::create()->text("<h3>Test API Reports</h3>")->asHtml(true),


            Panel::create()->header("<b>Test API Data</b>")->sub([
                Row::create()->sub([
                    TestAPIReport::create(),
                ]),
            ]),
        ];
    }
}
?>
Andre Van Der Walt commented on Jan 15

Andre Van Der Walt commented on Jan 17

It seems like we only need assistance with returning the data. Where are we going wrong with returning the array?

Andre Van Der Walt commented on Jan 18

Any feedback would be helpful.

KoolReport commented on Jan 18

Actually I have forwarded your case to dev.team to get suggestion from them. I will update you ASAP.

Andre Van Der Walt commented on Jan 18

Appreciate it!

KoolReport commented on Jan 18

You do this:

<?php

namespace App\TestAPIFolder\TestAPI;

use koolreport\dashboard\fields\Text;
use koolreport\dashboard\widgets\Table;

class TestAPIReport extends Table
{
    protected function dataSource()
    {
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.publicapis.org/entries',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        echo $response;

        $json = $response;
        $arr = json_decode($json, true);
        $output = [];
        foreach($arr["entries"] as $entries)
        {
            array_push($output,[
                "API"=>$arr["entries"]["API"],
                "Description"=>$arr["entries"]["Description"],
                "Auth"=>$arr["entries"]["Auth"],
                "HTTPS"=>$arr["entries"]["HTTPS"],
                "Cors"=>$arr["entries"]["Cors"],
                "Link"=>$arr["entries"]["Link"],
                "Category"=>$arr["entries"]["Category"],
            ]);
        }
        return $output;
    }

    protected function fields()
    {
        return [
            Text::create("API"),
            Text::create("Description"),
            Text::create("Auth"),
            Text::create("HTTPS"),
            Text::create("Cors"),
            Text::create("Link"),
            Text::create("Category"),
        ];
    }
}
?>

Let me know if it works.

Andre Van Der Walt commented on Jan 18

Thank you!

I just had to change the foreach to the following, and it's working perfectly.

    foreach($arr["entries"] as $entries)
    {
        array_push($output,[
            "API"=>$entries["API"],
            "Description"=>$entries["Description"],
            "Auth"=>$entries["Auth"],
            "HTTPS"=>$entries["HTTPS"],
            "Cors"=>$entries["Cors"],
            "Link"=>$entries["Link"],
            "Category"=>$entries["Category"],
        ]);
    }
    return $output;
KoolReport commented on Jan 19

That's great!

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
solved

Dashboard