0

我有一个简单的脚本,如果它发现用户正在手机上浏览,它会重定向到网站的移动版本。它使用 Tera-WURFL 网络服务来实现这一点,并将放置在 Tera-WURFL 本身以外的其他主机上。我想保护它,以防 Tera-WURFL 托管停机。换句话说,如果我的脚本运行时间超过一秒钟,则停止执行它并重定向到常规网站。如何有效地做到这一点(这样CPU不会被脚本过度负担)?

编辑:看起来 TeraWurflRemoteClient 类具有超时属性。参见下文。现在我需要找到如何将它包含在我的脚本中,以便在超时的情况下重定向到常规网站。

这是脚本:

// Instantiate a new TeraWurflRemoteClient object
$wurflObj = new TeraWurflRemoteClient('http://my-Tera-WURFL-install.pl/webservicep.php');

// Define which capabilities you want to test for. Full list: http://wurfl.sourceforge.net/help_doc.php#product_info
$capabilities = array("product_info");

// Define the response format (XML or JSON)
$data_format = TeraWurflRemoteClient::$FORMAT_JSON;

// Call the remote service (the first parameter is the User Agent - leave it as null to let TeraWurflRemoteClient find the user agent from the server global variable)
$wurflObj->getCapabilitiesFromAgent(null, $capabilities, $data_format);

// Use the results to serve the appropriate interface
if ($wurflObj->getDeviceCapability("is_tablet") || !$wurflObj->getDeviceCapability("is_wireless_device") || $_GET["ver"]=="desktop") {
    header('Location: http://website.pl/'); //default index file
} else {
header('Location: http://m.website.pl/'); //where to go
}
?>

这里是包含的 TeraWurflRemoteClient.php 的来源如文档中所述,它具有可选的超时参数:

// The timeout in seconds to wait for the server to respond before giving up
$timeout = 1;
4

2 回答 2

3

TeraWurflRemoteClient 类具有超时属性。正如我在文档中看到的那样,默认情况下它是 1 秒。因此,此脚本的执行时间不会超过一秒。

于 2012-04-28T09:19:13.030 回答
0

尝试通过在他们的类中设置对 TeraWurfl 的 HTTP 请求的非常短的超时来实现这一点,这样如果响应没有在 2-3 秒内返回,则认为检查为假并显示完整的网站。

寻找设置更短超时的位置可能会有所不同,具体取决于您用于发出 HTTP 请求的传输方式。就像在 Curl 中一样,您可以设置 HTTP 请求的超时时间。

在此之后,将您的 HTTP 请求超时重置回原来的状态,这样您就不会影响任何其他代码。

我也在研究它时发现了一点,你可能想读一读,但我会说远离分叉,除非你非常清楚事情是如何运作的。

刚才 Adelf 发布了 TeraWurflRemoteClient 类的默认超时时间为 1 秒,这样可以解决您的问题,但无论如何我都会发布我的答案。

于 2012-04-28T09:21:39.330 回答