【发布时间】:2012-05-08 21:04:47
【问题描述】:
我有一个简单的脚本,如果它发现用户正在手机上浏览,它会重定向到网站的移动版本。它使用 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
}
?>
还有here is source of TeraWurflRemoteClient.php 包含在内。它具有documentation 中提到的可选超时参数:
// The timeout in seconds to wait for the server to respond before giving up
$timeout = 1;
【问题讨论】:
标签: php performance time