Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines
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.
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(),
]),
]),
];
}
}
?>
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.
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;
Let KoolReport help you to make great reports. It's free & open-source released under MIT license.
Download KoolReport View demo