如何将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类型。真的很困惑我在这里做什么新的程序员。有人能解释一下我的路径是否正确,或者是否可以在控制器中执行。提前感谢。我可以建议稍微不同的解决方案:
showMonthlyReport()中,参考现有结果
- 这样,您就不必等待每个
的curl结果。所有代码都将异步运行showMonthlyReport()
- 所有错误处理都将在一个位置
- 命令是可测试的
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);
}