0

我的控制器。

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'));

}

而我的 UptimeRobot.php 参考https://uptimerobot.com/api getMonitors()方法

<?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);

}

目前正在构建一个 laravel Web 应用程序。

我只是想知道如何从 uptimerobot 收集数据。我将使用我的控制器,所以我可以将它传递给我的视图,但我不知道如何。我在下面的代码中有上面的 curl php 类型。真的很困惑我在这里做什么新程序员。有人可以解释我是在正确的道路上还是可以在控制器中做。提前致谢。

4

1 回答 1

1

我可以建议略有不同的解决方案:

  1. 在单独的控制台命令中提取您的 curl 代码并每分钟运行此命令(例如,作为cron 作业)。

  2. 命令的结果保存到数据库/文件/内存。

  3. 在您showMonthlyReport()参考现有结果时。

好处:

  • 这样,您就不必在每个showMonthlyReport(). 所有代码将异步运行
  • 所有错误处理将集中在一个地方
  • 命令是可测试的
于 2018-09-12T07:31:35.390 回答