KoolReport's Forum

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

API in Koolreport for data #2336

Closed Javier Gallardo opened this topic on on Sep 15, 2021 - 9 comments

Javier Gallardo commented on Sep 15, 2021

I need to connect with API and pass that data in JSON to create a reports

Javier Gallardo commented on Sep 15, 2021

<?php

namespace space\report;

class MyReport extends \koolreport\KoolReport {

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

function setup()
{

//START CODE API

$curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => 'URL',
      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',
      CURLOPT_HTTPHEADER => array(
        'Authorization: TOKEN'
      ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);

//END CODE API

    $this->src("apiarray")->load($data)
    ->pipe(...)

      pipe($this->dataStore("result"));
}

}

can you explain me in more detail

KoolReport commented on Sep 15, 2021

If you are able to receive the $response in json, you do:

    ...
    $response = curl_exec($curl);
    curl_close($curl);
    $data = json_decode($response,true);
     $this->src("apiarray")->load($data)
    ->pipe(...)
    ...

Of course, I do not know your structure of your json which you need to work out, it may need some transformation before it can be used. the $data in the load function need to be in this format (array of associate array),

[
    ["name"=>"Peter","age"=>39],
    ["name"=>"John","age"=>20],
]
Javier Gallardo commented on Sep 15, 2021

I dont understand this

->pipe(...)

...
KoolReport commented on Sep 15, 2021

It is not a code, just mean, that's part is not important like "..." (bla bla bla) you can put any pipe there

Javier Gallardo commented on Sep 20, 2021

it does not work

Sebastian Morales commented on Sep 20, 2021

Saying "it does not work" is the most meaningless thing one can say and provides us with no information whatsoever to help solving the problem for you. Provide your code, screenshots of errors or your problem, and describe what the problem is exactly and specifically.

Javier Gallardo commented on Sep 20, 2021

I answered everything in the other post, thank you

Javier Gallardo commented on Sep 20, 2021

..

KoolReport commented on Sep 22, 2021

I will close this post and will answer you in your new post #2345. We try to keep a topic on single issue for single person and also eliminate duplicate topics. Please continue in your new topic.

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
None yet

None