PHP:重试查询一定次数或直到成功 [英] PHP: Retrying a query a set number of times or until success

查看:238
本文介绍了PHP:重试查询一定次数或直到成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查询Alexa API,由于某种未知原因,它有时会失败.当它确实失败时,我想自动重试查询,最多10次.

I'm doing a query to the Alexa API which, for some unknown reason, will occasionally fail. When it does fail, I want to automatically retry the query, up to 10 times.

失败时,API返回的响应包含子字符串AuthFailure.

When it fails, the response returned by the API contains the substring AuthFailure.

我可以执行哪种循环来重试查询,直到返回的响应不包含子字符串AuthFailure或尝试了10次重试?

What kind of loop can I do that will retry the query until either the response returned does not contain the substring AuthFailure or 10 retries have been attempted?

推荐答案

您可以使用for循环进行此操作.

You could do this with a for loop.

for($i=0; $i < 10; $i++) {
    $return = (Alexa call here) 
    if(!strstr($return,"AuthFailure"))
        break;
}

将10调整为所需的任何数字.更好的是,在其他地方使用常量define() ed.这将一直运行到尝试次数用完或返回值不包含"AuthFailure"为止.

Adjust 10 to whatever number you wish. Better yet, use a constant define()'ed elsewhere. This will run until the number of tries is exhausted or until the return value does not contain "AuthFailure".

这篇关于PHP:重试查询一定次数或直到成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆