0

My Controller.

public function showMonthlyReport($site_id, $report_id){

$reports = Report::where('report_id', $report_id)->firstOrFail();

$uptime = ???

return view('records', compact('site_id', 'report_id', 'reports', 'uptime'));

}

And my UptimeRobot.php reference https://uptimerobot.com/api getMonitors()method

<?php 

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.uptimerobot.com/v2/getMonitors",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "Your Api Key",
CURLOPT_HTTPHEADER => array(
  "cache-control: no-cache",
  "content-type: application/x-www-form-urlencoded"
 ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

 if ($err) {
   echo "cURL Error #:" . $err;
} else {
$data = json_decode($response);
$custom_uptime = ($data->monitors[0]->custom_uptime_ratio);
$uptime = explode("-",$custom_uptime);
}

?>

ApiCommand.php

public function handle()
    {
    include(app_path() . '/Includes/DeepCrawl.php');  
    include(app_path() . '/Includes/Uptime.php'); 
    include(app_path() . '/Includes/HelloAnalytics.php');

$stringData = ApiCommand::drawGraph($uptime, $dates, $users, $otherResultsRows, $array_issues, $array_pages_breakdown, $array_uncrawled_url, $array_non_200_pages, $array_orphaned_pages, $array_non_indexable_pages, $array_crawl_source_gap, $array_https_http);

Storage::disk('local')->put('hello.txt', $stringData);

}

Currently building a laravel web application.

I am just wondering how can i able to gather data from uptimerobot. I'm going to use my controller so I can pass it to my view but I don't know how. I have code below with the curl php type above. Really confused what am I doing new programmer here. Can someone explain if I'm at the right path or is it possible to do in controller. Thanks in advance.

Christian Gallarmin
  • 660
  • 4
  • 15
  • 34

1 Answers1

1

I can suggest slightly different solution:

  1. Extract your curl code in a separate console command and run this command each minute (for example, as a cron job).

  2. The result of the command save to database/file/memory.

  3. In your showMonthlyReport() refer to existing result.

Benefits:

  • In this way you would not have to wait for your curl result on each showMonthlyReport(). All code will run asynchronously
  • All errors processing will be in one place
  • Command is testable
Prisacari Dmitrii
  • 1,985
  • 1
  • 23
  • 33
  • @PrisacariDimitrii I already tried that and run successfully but I don't know where to find the result :/ – Christian Gallarmin Sep 12 '18 at 14:27
  • 1
    @ChristianGallarmin You could try to save it to file using `Storage` facade and read the file on `showMonthlyReport()` run. See https://laravel.com/docs/5.6/filesystem for details. – Prisacari Dmitrii Sep 12 '18 at 14:31
  • @PrisacariDimitrii can you have a look on my commands file? going to edit my content above. – Christian Gallarmin Sep 12 '18 at 15:21
  • 1
    Firstly I would recommend using Guzzle client instead of bare curl (see related question: https://stackoverflow.com/questions/22355828/doing-http-requests-from-laravel-to-an-external-api) Secondly, I would avoid using any `include`s in your command. All code should be written in your laravel app as services. After that you should inject services in your command using dependency injection. If there would be any questions, please create separate question as this discussion gets out of scope. – Prisacari Dmitrii Sep 12 '18 at 15:38