PHP 重试脚本直到成功或出错

发布于 2024-09-10 02:10:55 字数 729 浏览 3 评论 0原文

我正在尝试创建一个 php 脚本,该脚本会重试另一个 php 脚本最多 3 次,直到显示错误。我想也许这可以使用 php 循环来完成?如果代码第一次成功,则无需重试 3 次,但是,如果第一次失败,则应重试 php 脚本最多 3 次,直到显示错误消息。

使用 php 编码,我设法制作了一个脚本,该脚本使用“file_get_contents”从另一个位置获取/获取内容,然后为每个单词/数据提供一个 php 变量。所有这一切都是通过得到 stackoverflow 上其他成员的帮助来完成的(我非常感谢)。下面的代码就是这一切的作用:

$searchdata = file_get_contents('http://www.example.com');

list($no1, $no2, $no3, $no4, $no5, 
     $no6, $no7, $no8, $no9) = explode(" ", $searchdata);

所以,我想添加某种循环,最多重试该脚本 3 次;如果第一次不起作用。要确定脚本第一次/第二次/第三次是否有效,在使用“file_get_contents”时应找到文本“#endofscript”或“failure”。如果显示除“#endofscript”或“失败”之外的任何其他内容,则应将其视为错误并应循环直至找到。如果第三次仍然没有找到,是否会显示错误消息?例如“错误 - 请重试”。

感谢您的所有帮助,我将感谢您的每一个回复。如果您需要更多详细信息,请随时询问。再次,我真的很感激这一点。 :)

I'm trying to create a php script which retries another php script up to 3 times until an error is displayed. I'm thinking perhaps this could be done using a php loop? If the code works successfully the first time, then there is no need for it to retry 3 times, however, if it doesn't work the first time, then it should retry the php script up to 3 times till an error message is displayed.

Using php coding, I've managed to make a script which grabs/fetches content from another location using "file_get_contents" and thereafter gives each word/data a php variable. All this was done by getting help from other members on stackoverflow (which I extremely appreciate). The code below is what does it all:

$searchdata = file_get_contents('http://www.example.com');

list($no1, $no2, $no3, $no4, $no5, 
     $no6, $no7, $no8, $no9) = explode(" ", $searchdata);

So, I'd like to add some sort of loop which retries this script up to 3 times; if it doesn't work the first time. To determine whether the script works the first/second/third time or not, the text "#endofscript" or "failure" should be found when using "file_get_contents". If anything else is displayed other than "#endofscript" or "failure" that should be counted as an error and should be looped till found. If it still isn't found after the third try, could an error message be displayed? Such as "Error - Please try again".

Thank you for all your assistance and I will appreciate each and every single reply. If you need more details, please feel free to ask. And again, I'm really grateful for this. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

━╋う一瞬間旳綻放 2024-09-17 02:10:55
$maxTries = 3;
for ($try=1; $try<=$maxTries; $try++) {
    // your code
    if ($success) {
        break;
    }
}

// if $try > 3, script failed
$maxTries = 3;
for ($try=1; $try<=$maxTries; $try++) {
    // your code
    if ($success) {
        break;
    }
}

// if $try > 3, script failed
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文