我有一个简单的脚本,如果它发现用户正在手机上浏览,它会重定向到网站的移动版本。它使用 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;