简体   繁体   English

处理 foreach 循环中的警告和通知

[英]Handle the warnings and notices in foreach loop

I have an array我有一个数组

   $array = [$element1,$element2,$element3...];

I have a php script which does particular tasks(Scraping) on each element of the array我有一个 php 脚本,它在数组的每个元素上执行特定的任务(抓取)

foreach($array as $element){
 //scrape data for each element
  }

This script throws many errors, I want to know for which particular element of the array it is throwing those errors If I run the script in the browser I can simply echo the element name, Something like this这个脚本抛出了很多错误,我想知道它为数组的哪个特定元素抛出这些错误如果我在浏览器中运行脚本,我可以简单地回显元素名称,像这样

foreach($array as $element){
  echo $element;
  //scrape data for element
  }

So by the above method, I can know which element is throwing the error, Because the element name will print out on the screen before the errors.所以通过上面的方法,我可以知道是哪个元素抛出了错误,因为元素名称会在错误之前在屏幕上打印出来。

But I want to run the script via cron, So i think the only possible way to track the errors is to return an array of errors in the end of the script (and send that in JSON form via email)但我想通过 cron 运行脚本,所以我认为跟踪错误的唯一可能方法是在脚本末尾返回一组错误(并通过电子邮件以 JSON 形式发送)

For example: if the script throws errors for element3 and element5.. it should return an array in the end of the script例如:如果脚本为 element3 和 element5 抛出错误......它应该在脚本的末尾返回一个数组

$errors_array = [element3=> ['warning:...','notice:...'], element5=>['warning:...']]

I think I can use this array to get notified about the errors via email (or dump these errors in some file)我想我可以使用这个数组通过电子邮件获得有关错误的通知(或将这些错误转储到某个文件中)

Thanks谢谢

If I run the script in the browser I can simply echo the element name ... But I want to run the script via cron, So i think the only possible way to track the errors is to return an array of errors in the end of the script如果我在浏览器中运行脚本,我可以简单地回显元素名称......但我想通过 cron 运行脚本,所以我认为跟踪错误的唯一可能方法是在最后返回一个错误数组剧本

You can very easily capture exactly the same output you would be sending to the browser into a file when you run it under cron.当您在 cron 下运行时,您可以非常轻松地将发送到浏览器的输出完全相同地捕获到文件中。 The trick is that you need to redirect both "Standard Error" and "Standard Output" to the same file.诀窍是您需要将“标准错误”和“标准输出”重定向到同一个文件。 The command (which you can test by running manually on the command-line) will look something like this:命令(您可以通过在命令行上手动运行来测试)将如下所示:

php /path/to/my/script.php some args 2>&1 >/path/to/my/output.log

The >file part is redirecting standard output; >file部分正在重定向标准输出; the 2>&1 adds standard error into the same output file. 2>&1将标准错误添加到同一个输出文件中。

You can enable the error reporting in PHP您可以在 PHP 中启用错误报告

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

And send the error logs, you can use them.并发送错误日志,您可以使用它们。

else if you want to customized error log否则如果你想自定义错误日志

You can use error_get_last() this gives you the last error.您可以使用error_get_last()这会给您最后一个错误。

foreach($array as $element){
  $error_arr = error_get_last();
 //log error in the file and flush on next iterator
  error_clear_last();
}

Or use the error reporting tool like sentry which will send you error + current var values to your email.或者使用像sentry这样的错误报告工具,它会将错误 + 当前 var 值发送到您的电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM