如何将php文件导入控制器并收集数据

如何将php文件导入控制器并收集数据,php,laravel,uptime-monitoring,Php,Laravel,Uptime Monitoring,我的控制器 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

我的控制器

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参考
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);
}

?>
目前正在构建laravel web应用程序


我只是想知道如何才能从uptimerobot收集数据。我将使用我的控制器,这样我就可以将它传递给我的视图,但我不知道如何传递。我有下面的代码和上面的curl-php类型。真的很困惑我在这里做什么新的程序员。有人能解释一下我的路径是否正确,或者是否可以在控制器中执行。提前感谢。

我可以建议稍微不同的解决方案:

  • 以单独的方式提取curl代码,并每分钟运行一次该命令(例如,作为一个示例)

  • 命令save to database/file/memory的结果

  • showMonthlyReport()中,参考现有结果

  • 好处:

    • 这样,您就不必等待每个
      showMonthlyReport()
      的curl结果。所有代码都将异步运行
    • 所有错误处理都将在一个位置
    • 命令是可测试的

    您可以将curl代码作为控制器类中的私有函数编写,并像调用任何其他函数一样调用它functions@RinsadAhmed如果我这样做,那么上面的函数$uptime=???应该是$uptime=uptimefunction();类似的东西?好吧,因为你在一个类中,你应该使用$uptime=$this->uptimefunction()@Prisacridimitrii我已经尝试过并成功运行了,但我不知道在哪里可以找到结果:/@ChristianGallarmin您可以尝试使用
    Storage
    facade将其保存到文件中,并在
    showMonthlyReport()上读取该文件。有关详细信息,请参阅。@prisacridimtrii您能看看我的命令文件吗?我将编辑上面的内容。首先,我建议使用Guzzle客户端而不是bare curl(请参阅相关问题:),其次,我将避免在命令中使用任何
    include
    s。所有代码都应作为服务写入您的laravel应用程序中。之后,应该使用依赖项注入在命令中注入服务。如果有任何问题,请在讨论超出范围时创建单独的问题。
    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);
    
    }