【问题标题】:Parsing mobile phone user agent string , good solution or library?解析手机用户代理字符串,好的解决方案还是库?
【发布时间】:2012-03-19 22:35:59
【问题描述】:

要求: 通过从浏览器解析用户代理字符串来找出用户正在使用的电话。(在 PHP 中) 例如。诺基亚1100,三星D900。

是否有一个很好的库,其中包含足够的手机供应商和型号数据库?

我找到的一些解决方案,同样需要你的意见:

i) handsetdetectionhttps://www.handsetdetection.com - 付费订阅

ii) WURFL - http://wurfl.sourceforge.net/

iii) 自己的解决方案 - 我有一个小型手机制造商和型号数据库,但必须在我的代码中添加针对用户代理字符串的定制检查,以匹配 /fuzzy 与数据库的匹配不同制造商的用户代理字符串格式不一致。

更新:

我们创建了一个量身定制的解决方案,它混合了正则表达式来解析 iOS、Android、Blackberry 和 WURFL 等标准用户代理,作为 symbian、j2me、bada 等其他手机的后备机制。

WURFL 在添加数据库/缓存(MySql、memcached、mongodb 等)后效果很好,这些数据库/缓存已作为核心代码库中的设置存在。尽管您必须每隔几周使用最新版本的 WURFL 手机 xml 数据库更新/同步 wurfl 信息随时了解新发布的手机的规格。

【问题讨论】:

    标签: php user-agent mobile-devices


    【解决方案1】:

    最近尝试解决一个类似的问题,最好使用开源或现成的解决方案,因为您将努力使您的代码保持最新等。我个人会尝试免费版本,如果它做你需要的坚持下去。

    你可以尝试类似的东西 if(strstr($_SERVER['HTTP_USER_AGENT'],'Android') || strstr($_SERVER['HTTP_USER_AGENT'],'webOS') || strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod') ){ //移动用户 }

    如果您只需要了解操作系统,它会检测最流行的操作系统。然后根据需要添加其他人,例如诺基亚等。

    http://www.concrete5.org/community/forums/customizing_c5/mobile-version/似乎是一个更完整的列表

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      这看起来很有希望,您可以选择您想要检测代码的语言:PHP、JS 或 Apache、Nginx……但一如既往,您需要对其进行测试;)http://detectmobilebrowsers.com

      【讨论】:

        【解决方案3】:
        <?php
        // Include the browser php file that contains the class
        require_once 'Browser1.php';
        
        $browserData = new Browser();
        // Output for testing purposes browser info
        echo $browserData;
        /* My laptop:
        Browser Name:Firefox
        Browser Version:10.0.2
        Browser User Agent String:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
        Platform:Windows
        */
        
        $browserMajorVersion = intval($browserData->getVersion());
        $browserName = $browserData->getBrowser();
        $css = 3;
        if ($browserName == 'Internet Explorer' || $browserName == 'Opera') $css = 2;
        echo '<p>Css version supported:'.$css.'</p>';
        // PHP Class file
        <?php
        class Browser {
        private $_agent = '';
        private $_browser_name = '';
        private $_version = '';
        private $_platform = '';
        private $_os = '';
        private $_is_aol = false;
        private $_is_mobile = false;
        private $_is_robot = false;
        private $_aol_version = '';
        
        const BROWSER_UNKNOWN = 'unknown';
        const VERSION_UNKNOWN = 'unknown';
        
        const BROWSER_OPERA = 'Opera';                            // http://www.opera.com/
        const BROWSER_OPERA_MINI = 'Opera Mini';                  // http://www.opera.com/mini/
        const BROWSER_WEBTV = 'WebTV';                            // http://www.webtv.net/pc/
        const BROWSER_IE = 'Internet Explorer';                   // http://www.microsoft.com/ie/
        const BROWSER_POCKET_IE = 'Pocket Internet Explorer';     // http://en.wikipedia.org/wiki/Internet_Explorer_Mobile
        const BROWSER_KONQUEROR = 'Konqueror';                    // http://www.konqueror.org/
        const BROWSER_ICAB = 'iCab';                              // http://www.icab.de/
        const BROWSER_OMNIWEB = 'OmniWeb';                        // http://www.omnigroup.com/applications/omniweb/
        const BROWSER_FIREBIRD = 'Firebird';                      // http://www.ibphoenix.com/
        const BROWSER_FIREFOX = 'Firefox';                        // http://www.mozilla.com/en-US/firefox/firefox.html
        const BROWSER_ICEWEASEL = 'Iceweasel';                    // http://www.geticeweasel.org/
        const BROWSER_SHIRETOKO = 'Shiretoko';                    // http://wiki.mozilla.org/Projects/shiretoko
        const BROWSER_MOZILLA = 'Mozilla';                        // http://www.mozilla.com/en-US/
        const BROWSER_AMAYA = 'Amaya';                            // http://www.w3.org/Amaya/
        const BROWSER_LYNX = 'Lynx';                              // http://en.wikipedia.org/wiki/Lynx
        const BROWSER_SAFARI = 'Safari';                          // http://apple.com
        const BROWSER_IPHONE = 'iPhone';                          // http://apple.com
        const BROWSER_IPOD = 'iPod';                              // http://apple.com
        const BROWSER_IPAD = 'iPad';                              // http://apple.com
        const BROWSER_CHROME = 'Chrome';                          // http://www.google.com/chrome
        const BROWSER_ANDROID = 'Android';                        // http://www.android.com/
        const BROWSER_GOOGLEBOT = 'GoogleBot';                    // http://en.wikipedia.org/wiki/Googlebot
        const BROWSER_SLURP = 'Yahoo! Slurp';                     // http://en.wikipedia.org/wiki/Yahoo!_Slurp
        const BROWSER_W3CVALIDATOR = 'W3C Validator';             // http://validator.w3.org/
        const BROWSER_BLACKBERRY = 'BlackBerry';                  // http://www.blackberry.com/
        const BROWSER_ICECAT = 'IceCat';                          // http://en.wikipedia.org/wiki/GNU_IceCat
        const BROWSER_NOKIA_S60 = 'Nokia S60 OSS Browser';        // http://en.wikipedia.org/wiki/Web_Browser_for_S60
        const BROWSER_NOKIA = 'Nokia Browser';                    // * all other WAP-based browsers on the Nokia Platform
        const BROWSER_MSN = 'MSN Browser';                        // http://explorer.msn.com/
        const BROWSER_MSNBOT = 'MSN Bot';                         // http://search.msn.com/msnbot.htm
                                                                  // http://en.wikipedia.org/wiki/Msnbot  (used for Bing as well)
        
        const BROWSER_NETSCAPE_NAVIGATOR = 'Netscape Navigator';  // http://browser.netscape.com/ (DEPRECATED)
        const BROWSER_GALEON = 'Galeon';                          // http://galeon.sourceforge.net/ (DEPRECATED)
        const BROWSER_NETPOSITIVE = 'NetPositive';                // http://en.wikipedia.org/wiki/NetPositive (DEPRECATED)
        const BROWSER_PHOENIX = 'Phoenix';                        // http://en.wikipedia.org/wiki/History_of_Mozilla_Firefox (DEPRECATED)
        
        const PLATFORM_UNKNOWN = 'unknown';
        const PLATFORM_WINDOWS = 'Windows';
        const PLATFORM_WINDOWS_CE = 'Windows CE';
        const PLATFORM_APPLE = 'Apple';
        const PLATFORM_LINUX = 'Linux';
        const PLATFORM_OS2 = 'OS/2';
        const PLATFORM_BEOS = 'BeOS';
        const PLATFORM_IPHONE = 'iPhone';
        const PLATFORM_IPOD = 'iPod';
        const PLATFORM_IPAD = 'iPad';
        const PLATFORM_BLACKBERRY = 'BlackBerry';
        const PLATFORM_NOKIA = 'Nokia';
        const PLATFORM_FREEBSD = 'FreeBSD';
        const PLATFORM_OPENBSD = 'OpenBSD';
        const PLATFORM_NETBSD = 'NetBSD';
        const PLATFORM_SUNOS = 'SunOS';
        const PLATFORM_OPENSOLARIS = 'OpenSolaris';
        const PLATFORM_ANDROID = 'Android';
        
        const OPERATING_SYSTEM_UNKNOWN = 'unknown';
        
        public function Browser($useragent="") {
            $this->reset();
            if( $useragent != "" ) {
                $this->setUserAgent($useragent);
            }
            else {
                $this->determine();
            }
        }
        
        /**
        * Reset all properties
        */
        public function reset() {
            $this->_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "";
            $this->_browser_name = self::BROWSER_UNKNOWN;
            $this->_version = self::VERSION_UNKNOWN;
            $this->_platform = self::PLATFORM_UNKNOWN;
            $this->_os = self::OPERATING_SYSTEM_UNKNOWN;
            $this->_is_aol = false;
            $this->_is_mobile = false;
            $this->_is_robot = false;
            $this->_aol_version = self::VERSION_UNKNOWN;
        }
        
        function isBrowser($browserName) { return( 0 == strcasecmp($this->_browser_name, trim($browserName))); }
        
        public function getBrowser() { return $this->_browser_name; }
        public function setBrowser($browser) { return $this->_browser_name = $browser; }
        public function getPlatform() { return $this->_platform; }
        public function setPlatform($platform) { return $this->_platform = $platform; }
        public function getVersion() { return $this->_version; }
        public function setVersion($version) { $this->_version = preg_replace('/[^0-9,.,a-z,A-Z-]/','',$version); }
        public function getAolVersion() { return $this->_aol_version; }
        public function setAolVersion($version) { $this->_aol_version = preg_replace('/[^0-9,.,a-z,A-Z]/','',$version); }
        public function isAol() { return $this->_is_aol; }
        public function isMobile() { return $this->_is_mobile; }
        public function isRobot() { return $this->_is_robot; }
        public function setAol($isAol) { $this->_is_aol = $isAol; }
        protected function setMobile($value=true) { $this->_is_mobile = $value; }
        protected function setRobot($value=true) { $this->_is_robot = $value; }
        public function getUserAgent() { return $this->_agent; }
        public function setUserAgent($agent_string) {
            $this->reset();
            $this->_agent = $agent_string;
            $this->determine();
        }
        public function isChromeFrame() {
            return( strpos($this->_agent,"chromeframe") !== false );
        }
        public function __toString() {
            return "<strong>Browser Name:</strong>{$this->getBrowser()}<br/>\n" .
                   "<strong>Browser Version:</strong>{$this->getVersion()}<br/>\n" .
                   "<strong>Browser User Agent String:</strong>{$this->getUserAgent()}<br/>\n" .
                   "<strong>Platform:</strong>{$this->getPlatform()}<br/>";
        }
        protected function determine() {
            $this->checkPlatform();
            $this->checkBrowsers();
            $this->checkForAol();
        }
         protected function checkBrowsers() {
            return (
                $this->checkBrowserWebTv() ||
                $this->checkBrowserInternetExplorer() ||
                $this->checkBrowserOpera() ||
                $this->checkBrowserGaleon() ||
                $this->checkBrowserNetscapeNavigator9Plus() ||
                $this->checkBrowserFirefox() ||
                $this->checkBrowserChrome() ||
                $this->checkBrowserOmniWeb() ||
        
                // common mobile
                $this->checkBrowserAndroid() ||
                $this->checkBrowseriPad() ||
                $this->checkBrowseriPod() ||
                $this->checkBrowseriPhone() ||
                $this->checkBrowserBlackBerry() ||
                $this->checkBrowserNokia() ||
        
                // common bots
                $this->checkBrowserGoogleBot() ||
                $this->checkBrowserMSNBot() ||
                $this->checkBrowserSlurp() ||
        
                // WebKit base check (post mobile and others)
                $this->checkBrowserSafari() ||
        
                // everyone else
                $this->checkBrowserNetPositive() ||
                $this->checkBrowserFirebird() ||
                $this->checkBrowserKonqueror() ||
                $this->checkBrowserIcab() ||
                $this->checkBrowserPhoenix() ||
                $this->checkBrowserAmaya() ||
                $this->checkBrowserLynx() ||
                $this->checkBrowserShiretoko() ||
                $this->checkBrowserIceCat() ||
                $this->checkBrowserW3CValidator() ||
                $this->checkBrowserMozilla() /* Mozilla is such an open standard that you must check it last */
            );
        }
        protected function checkBrowserBlackBerry() {
            if( stripos($this->_agent,'blackberry') !== false ) {
                $aresult = explode("/",stristr($this->_agent,"BlackBerry"));
                $aversion = explode(' ',$aresult[1]);
                $this->setVersion($aversion[0]);
                $this->_browser_name = self::BROWSER_BLACKBERRY;
                $this->setMobile(true);
                return true;
            }
            return false;
        }
        protected function checkForAol() {
            $this->setAol(false);
            $this->setAolVersion(self::VERSION_UNKNOWN);
        
            if( stripos($this->_agent,'aol') !== false ) {
                $aversion = explode(' ',stristr($this->_agent, 'AOL'));
                $this->setAol(true);
                $this->setAolVersion(preg_replace('/[^0-9\.a-z]/i', '', $aversion[1]));
                return true;
            }
            return false;
        }
        protected function checkBrowserGoogleBot() {
            if( stripos($this->_agent,'googlebot') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'googlebot'));
                $aversion = explode(' ',$aresult[1]);
                $this->setVersion(str_replace(';','',$aversion[0]));
                $this->_browser_name = self::BROWSER_GOOGLEBOT;
                $this->setRobot(true);
                return true;
            }
            return false;
        }
        protected function checkBrowserMSNBot() {
            if( stripos($this->_agent,"msnbot") !== false ) {
                $aresult = explode("/",stristr($this->_agent,"msnbot"));
                $aversion = explode(" ",$aresult[1]);
                $this->setVersion(str_replace(";","",$aversion[0]));
                $this->_browser_name = self::BROWSER_MSNBOT;
                $this->setRobot(true);
                return true;
            }
            return false;
        }
        protected function checkBrowserW3CValidator() {
            if( stripos($this->_agent,'W3C-checklink') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'W3C-checklink'));
                $aversion = explode(' ',$aresult[1]);
                $this->setVersion($aversion[0]);
                $this->_browser_name = self::BROWSER_W3CVALIDATOR;
                return true;
            }
            else if( stripos($this->_agent,'W3C_Validator') !== false ) {
                // Some of the Validator versions do not delineate w/ a slash - add it back in
                $ua = str_replace("W3C_Validator ", "W3C_Validator/", $this->_agent);
                $aresult = explode('/',stristr($ua,'W3C_Validator'));
                $aversion = explode(' ',$aresult[1]);
                $this->setVersion($aversion[0]);
                $this->_browser_name = self::BROWSER_W3CVALIDATOR;
                return true;
            }
            return false;
        }
        protected function checkBrowserSlurp() {
            if( stripos($this->_agent,'slurp') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'Slurp'));
                $aversion = explode(' ',$aresult[1]);
                $this->setVersion($aversion[0]);
                $this->_browser_name = self::BROWSER_SLURP;
                $this->setRobot(true);
                $this->setMobile(false);
                return true;
            }
            return false;
        }
        protected function checkBrowserInternetExplorer() {
        
            if( stripos($this->_agent,'microsoft internet explorer') !== false ) {
                $this->setBrowser(self::BROWSER_IE);
                $this->setVersion('1.0');
                $aresult = stristr($this->_agent, '/');
                if( preg_match('/308|425|426|474|0b1/i', $aresult) ) {
                    $this->setVersion('1.5');
                }
                return true;
            }
            else if( stripos($this->_agent,'msie') !== false && stripos($this->_agent,'opera') === false ) {
                if( stripos($this->_agent,'msnb') !== false ) {
                    $aresult = explode(' ',stristr(str_replace(';','; ',$this->_agent),'MSN'));
                    $this->setBrowser( self::BROWSER_MSN );
                    $this->setVersion(str_replace(array('(',')',';'),'',$aresult[1]));
                    return true;
                }
                $aresult = explode(' ',stristr(str_replace(';','; ',$this->_agent),'msie'));
                $this->setBrowser( self::BROWSER_IE );
                $this->setVersion(str_replace(array('(',')',';'),'',$aresult[1]));
                return true;
            }
            else if( stripos($this->_agent,'mspie') !== false || stripos($this->_agent,'pocket') !== false ) {
                $aresult = explode(' ',stristr($this->_agent,'mspie'));
                $this->setPlatform( self::PLATFORM_WINDOWS_CE );
                $this->setBrowser( self::BROWSER_POCKET_IE );
                $this->setMobile(true);
        
                if( stripos($this->_agent,'mspie') !== false ) {
                    $this->setVersion($aresult[1]);
                }
                else {
                    $aversion = explode('/',$this->_agent);
                    $this->setVersion($aversion[1]);
                }
                return true;
            }
            return false;
        }
        protected function checkBrowserOpera() {
            if( stripos($this->_agent,'opera mini') !== false ) {
                $resultant = stristr($this->_agent, 'opera mini');
                if( preg_match('/\//',$resultant) ) {
                    $aresult = explode('/',$resultant);
                    $aversion = explode(' ',$aresult[1]);
                    $this->setVersion($aversion[0]);
                }
                else {
                    $aversion = explode(' ',stristr($resultant,'opera mini'));
                    $this->setVersion($aversion[1]);
                }
                $this->_browser_name = self::BROWSER_OPERA_MINI;
                $this->setMobile(true);
                return true;
            }
            else if( stripos($this->_agent,'opera') !== false ) {
                $resultant = stristr($this->_agent, 'opera');
                if( preg_match('/Version\/(11.*)$/',$resultant,$matches) ) {
                    $this->setVersion($matches[1]);
                }
                else if( preg_match('/Version\/(10.*)$/',$resultant,$matches) ) {
                    $this->setVersion($matches[1]);
                }
                else if( preg_match('/\//',$resultant) ) {
                    $aresult = explode('/',str_replace("("," ",$resultant));
                    $aversion = explode(' ',$aresult[1]);
                    $this->setVersion($aversion[0]);
                }
                else {
                    $aversion = explode(' ',stristr($resultant,'opera'));
                    $this->setVersion(isset($aversion[1])?$aversion[1]:"");
                }
                $this->_browser_name = self::BROWSER_OPERA;
                return true;
            }
            return false;
        }
        protected function checkBrowserChrome() {
            if( stripos($this->_agent,'Chrome') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'Chrome'));
                $aversion = explode(' ',$aresult[1]);
                $this->setVersion($aversion[0]);
                $this->setBrowser(self::BROWSER_CHROME);
                return true;
            }
            return false;
        }
        protected function checkBrowserWebTv() {
            if( stripos($this->_agent,'webtv') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'webtv'));
                $aversion = explode(' ',$aresult[1]);
                $this->setVersion($aversion[0]);
                $this->setBrowser(self::BROWSER_WEBTV);
                return true;
            }
            return false;
        }
        protected function checkBrowserNetPositive() {
            if( stripos($this->_agent,'NetPositive') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'NetPositive'));
                $aversion = explode(' ',$aresult[1]);
                $this->setVersion(str_replace(array('(',')',';'),'',$aversion[0]));
                $this->setBrowser(self::BROWSER_NETPOSITIVE);
                return true;
            }
            return false;
        }
        protected function checkBrowserGaleon() {
            if( stripos($this->_agent,'galeon') !== false ) {
                $aresult = explode(' ',stristr($this->_agent,'galeon'));
                $aversion = explode('/',$aresult[0]);
                $this->setVersion($aversion[1]);
                $this->setBrowser(self::BROWSER_GALEON);
                return true;
            }
            return false;
        }
        protected function checkBrowserKonqueror() {
            if( stripos($this->_agent,'Konqueror') !== false ) {
                $aresult = explode(' ',stristr($this->_agent,'Konqueror'));
                $aversion = explode('/',$aresult[0]);
                $this->setVersion($aversion[1]);
                $this->setBrowser(self::BROWSER_KONQUEROR);
                return true;
            }
            return false;
        }
        protected function checkBrowserIcab() {
            if( stripos($this->_agent,'icab') !== false ) {
                $aversion = explode(' ',stristr(str_replace('/',' ',$this->_agent),'icab'));
                $this->setVersion($aversion[1]);
                $this->setBrowser(self::BROWSER_ICAB);
                return true;
            }
            return false;
        }
        protected function checkBrowserOmniWeb() {
            if( stripos($this->_agent,'omniweb') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'omniweb'));
                $aversion = explode(' ',isset($aresult[1])?$aresult[1]:"");
                $this->setVersion($aversion[0]);
                $this->setBrowser(self::BROWSER_OMNIWEB);
                return true;
            }
            return false;
        }
        protected function checkBrowserPhoenix() {
            if( stripos($this->_agent,'Phoenix') !== false ) {
                $aversion = explode('/',stristr($this->_agent,'Phoenix'));
                $this->setVersion($aversion[1]);
                $this->setBrowser(self::BROWSER_PHOENIX);
                return true;
            }
            return false;
        }
        protected function checkBrowserFirebird() {
            if( stripos($this->_agent,'Firebird') !== false ) {
                $aversion = explode('/',stristr($this->_agent,'Firebird'));
                $this->setVersion($aversion[1]);
                $this->setBrowser(self::BROWSER_FIREBIRD);
                return true;
            }
            return false;
        }
        protected function checkBrowserNetscapeNavigator9Plus() {
            if( stripos($this->_agent,'Firefox') !== false && preg_match('/Navigator\/([^ ]*)/i',$this->_agent,$matches) ) {
                $this->setVersion($matches[1]);
                $this->setBrowser(self::BROWSER_NETSCAPE_NAVIGATOR);
                return true;
            }
            else if( stripos($this->_agent,'Firefox') === false && preg_match('/Netscape6?\/([^ ]*)/i',$this->_agent,$matches) ) {
                $this->setVersion($matches[1]);
                $this->setBrowser(self::BROWSER_NETSCAPE_NAVIGATOR);
                return true;
            }
            return false;
        }
        protected function checkBrowserShiretoko() {
            if( stripos($this->_agent,'Mozilla') !== false && preg_match('/Shiretoko\/([^ ]*)/i',$this->_agent,$matches) ) {
                $this->setVersion($matches[1]);
                $this->setBrowser(self::BROWSER_SHIRETOKO);
                return true;
            }
            return false;
        }
        protected function checkBrowserIceCat() {
            if( stripos($this->_agent,'Mozilla') !== false && preg_match('/IceCat\/([^ ]*)/i',$this->_agent,$matches) ) {
                $this->setVersion($matches[1]);
                $this->setBrowser(self::BROWSER_ICECAT);
                return true;
            }
            return false;
        }
        protected function checkBrowserNokia() {
            if( preg_match("/Nokia([^\/]+)\/([^ SP]+)/i",$this->_agent,$matches) ) {
                $this->setVersion($matches[2]);
                if( stripos($this->_agent,'Series60') !== false || strpos($this->_agent,'S60') !== false ) {
                    $this->setBrowser(self::BROWSER_NOKIA_S60);
                }
                else {
                    $this->setBrowser( self::BROWSER_NOKIA );
                }
                $this->setMobile(true);
                return true;
            }
            return false;
        }
        protected function checkBrowserFirefox() {
            if( stripos($this->_agent,'safari') === false ) {
                if( preg_match("/Firefox[\/ \(]([^ ;\)]+)/i",$this->_agent,$matches) ) {
                    $this->setVersion($matches[1]);
                    $this->setBrowser(self::BROWSER_FIREFOX);
                    return true;
                }
                else if( preg_match("/Firefox$/i",$this->_agent,$matches) ) {
                    $this->setVersion("");
                    $this->setBrowser(self::BROWSER_FIREFOX);
                    return true;
                }
            }
            return false;
        }
        protected function checkBrowserIceweasel() {
            if( stripos($this->_agent,'Iceweasel') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'Iceweasel'));
                $aversion = explode(' ',$aresult[1]);
                $this->setVersion($aversion[0]);
                $this->setBrowser(self::BROWSER_ICEWEASEL);
                return true;
            }
            return false;
        }
        protected function checkBrowserMozilla() {
            if( stripos($this->_agent,'mozilla') !== false  && preg_match('/rv:[0-9].[0-9][a-b]?/i',$this->_agent) && stripos($this->_agent,'netscape') === false) {
                $aversion = explode(' ',stristr($this->_agent,'rv:'));
                preg_match('/rv:[0-9].[0-9][a-b]?/i',$this->_agent,$aversion);
                $this->setVersion(str_replace('rv:','',$aversion[0]));
                $this->setBrowser(self::BROWSER_MOZILLA);
                return true;
            }
            else if( stripos($this->_agent,'mozilla') !== false && preg_match('/rv:[0-9]\.[0-9]/i',$this->_agent) && stripos($this->_agent,'netscape') === false ) {
                $aversion = explode('',stristr($this->_agent,'rv:'));
                $this->setVersion(str_replace('rv:','',$aversion[0]));
                $this->setBrowser(self::BROWSER_MOZILLA);
                return true;
            }
            else if( stripos($this->_agent,'mozilla') !== false  && preg_match('/mozilla\/([^ ]*)/i',$this->_agent,$matches) && stripos($this->_agent,'netscape') === false ) {
                $this->setVersion($matches[1]);
                $this->setBrowser(self::BROWSER_MOZILLA);
                return true;
            }
            return false;
        }
        protected function checkBrowserLynx() {
            if( stripos($this->_agent,'lynx') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'Lynx'));
                $aversion = explode(' ',(isset($aresult[1])?$aresult[1]:""));
                $this->setVersion($aversion[0]);
                $this->setBrowser(self::BROWSER_LYNX);
                return true;
            }
            return false;
        }
        protected function checkBrowserAmaya() {
            if( stripos($this->_agent,'amaya') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'Amaya'));
                $aversion = explode(' ',$aresult[1]);
                $this->setVersion($aversion[0]);
                $this->setBrowser(self::BROWSER_AMAYA);
                return true;
            }
            return false;
        }
        
        protected function checkBrowserSafari() {
            if( stripos($this->_agent,'Safari') !== false && stripos($this->_agent,'iPhone') === false && stripos($this->_agent,'iPod') === false ) {
                $aresult = explode('/',stristr($this->_agent,'Version'));
                if( isset($aresult[1]) ) {
                    $aversion = explode(' ',$aresult[1]);
                    $this->setVersion($aversion[0]);
                }
                else {
                    $this->setVersion(self::VERSION_UNKNOWN);
                }
                $this->setBrowser(self::BROWSER_SAFARI);
                return true;
            }
            return false;
        }
        
        protected function checkBrowseriPhone() {
            if( stripos($this->_agent,'iPhone') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'Version'));
                if( isset($aresult[1]) ) {
                    $aversion = explode(' ',$aresult[1]);
                    $this->setVersion($aversion[0]);
                }
                else {
                    $this->setVersion(self::VERSION_UNKNOWN);
                }
                $this->setMobile(true);
                $this->setBrowser(self::BROWSER_IPHONE);
                return true;
            }
            return false;
        }
        protected function checkBrowseriPad() {
            if( stripos($this->_agent,'iPad') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'Version'));
                if( isset($aresult[1]) ) {
                    $aversion = explode(' ',$aresult[1]);
                    $this->setVersion($aversion[0]);
                }
                else {
                    $this->setVersion(self::VERSION_UNKNOWN);
                }
                $this->setMobile(true);
                $this->setBrowser(self::BROWSER_IPAD);
                return true;
            }
            return false;
        }
        protected function checkBrowseriPod() {
            if( stripos($this->_agent,'iPod') !== false ) {
                $aresult = explode('/',stristr($this->_agent,'Version'));
                if( isset($aresult[1]) ) {
                    $aversion = explode(' ',$aresult[1]);
                    $this->setVersion($aversion[0]);
                }
                else {
                    $this->setVersion(self::VERSION_UNKNOWN);
                }
                $this->setMobile(true);
                $this->setBrowser(self::BROWSER_IPOD);
                return true;
            }
            return false;
        }
        
        protected function checkBrowserAndroid() {
            if( stripos($this->_agent,'Android') !== false ) {
                $aresult = explode(' ',stristr($this->_agent,'Android'));
                if( isset($aresult[1]) ) {
                    $aversion = explode(' ',$aresult[1]);
                    $this->setVersion($aversion[0]);
                }
                else {
                    $this->setVersion(self::VERSION_UNKNOWN);
                }
                $this->setMobile(true);
                $this->setBrowser(self::BROWSER_ANDROID);
                return true;
            }
            return false;
        }
        protected function checkPlatform() {
            if( stripos($this->_agent, 'windows') !== false ) {
                $this->_platform = self::PLATFORM_WINDOWS;
            }
            else if( stripos($this->_agent, 'iPad') !== false ) {
                $this->_platform = self::PLATFORM_IPAD;
            }
            else if( stripos($this->_agent, 'iPod') !== false ) {
                $this->_platform = self::PLATFORM_IPOD;
            }
            else if( stripos($this->_agent, 'iPhone') !== false ) {
                $this->_platform = self::PLATFORM_IPHONE;
            }
            elseif( stripos($this->_agent, 'mac') !== false ) {
                $this->_platform = self::PLATFORM_APPLE;
            }
            elseif( stripos($this->_agent, 'android') !== false ) {
                $this->_platform = self::PLATFORM_ANDROID;
            }
            elseif( stripos($this->_agent, 'linux') !== false ) {
                $this->_platform = self::PLATFORM_LINUX;
            }
            else if( stripos($this->_agent, 'Nokia') !== false ) {
                $this->_platform = self::PLATFORM_NOKIA;
            }
            else if( stripos($this->_agent, 'BlackBerry') !== false ) {
                $this->_platform = self::PLATFORM_BLACKBERRY;
            }
            elseif( stripos($this->_agent,'FreeBSD') !== false ) {
                $this->_platform = self::PLATFORM_FREEBSD;
            }
            elseif( stripos($this->_agent,'OpenBSD') !== false ) {
                $this->_platform = self::PLATFORM_OPENBSD;
            }
            elseif( stripos($this->_agent,'NetBSD') !== false ) {
                $this->_platform = self::PLATFORM_NETBSD;
            }
            elseif( stripos($this->_agent, 'OpenSolaris') !== false ) {
                $this->_platform = self::PLATFORM_OPENSOLARIS;
            }
            elseif( stripos($this->_agent, 'SunOS') !== false ) {
                $this->_platform = self::PLATFORM_SUNOS;
            }
            elseif( stripos($this->_agent, 'OS\/2') !== false ) {
                $this->_platform = self::PLATFORM_OS2;
            }
            elseif( stripos($this->_agent, 'BeOS') !== false ) {
                $this->_platform = self::PLATFORM_BEOS;
            }
            elseif( stripos($this->_agent, 'win') !== false ) {
                $this->_platform = self::PLATFORM_WINDOWS;
            }
        
        }
        }
        ?>
        

        【讨论】:

        • 请也包括使用技巧
        • 这不像 WURFL 那样提供制造商和型号信息。例如,这不会告诉我请求是否来自诺基亚 3100 设备。
        【解决方案4】:

        首先,我想说的是 KISS(保持简单,愚蠢),这是一个广泛使用的表达方式。我会首先仔细检查我的业务需求,看看我真正需要多少设备信息以及我将使用它来做什么。也许你只需要看看客户端使用的是哪个渲染引擎?

        其次,你应该考虑解析时间。如果您最终使用 WURFL 并在您的网站上解析该 XML,则在检查设备信息时需要解析 400,000 多行 XML。当然,您可以将 XML 放入本地索引数据库,但这也需要一些维护脚本来将更新后的 XML 中的数据与数据库同步。

        第三个(但也许应该是第一个?)是考虑解决方案的成本/收益。如果您在网站上赚钱,将一些责任留给合作伙伴可能是明智之举。像handsetdetection.com 这样的托管服务似乎能够以不那么可怕的成本提供高流量。另一个好处是他们负责维护自己的存储库,如果他们的服务不够好,可能会失去客户。理论上,开源社区可以休 4 个月的假期,并且在那段时间里不会维持解决方案(我真的不认为这应该是什么值得担心的 ;-)

        不知道你的确切需求,我会这样优先考虑:

        1. 使用尽可能简单的解决方案,即来自Detect Mobile Browsers 的解决方案
        2. 去开源,比如 WURFL。我只是喜欢开源解决方案 :-)
        3. 如果您的业务需要保证稳定性和数据质量,您应该让专业人员处理它;-)

        【讨论】:

        • 我无法竞争从如此出色的答案中夺走赏金!继续努力吧! :^)
        • > "其次,您应该考虑解析时间。如果您最终使用 ie WURFL 并在您的站点上解析该 XML,则在检查设备信息时需要解析 400 000+ 行 XML . 当然,您可以将 XML 放入本地索引数据库,但这也需要一些维护脚本来将更新后的 XML 中的数据与数据库同步。 - WURFL API 仅在初始加载时解析一次完整的 XML;随后的查找是针对一小组用户代理进行的,或者在数据库 API 的情况下通过索引 MySQL、MSSQL 或 Mongo DB 进行。
        • @SteveK 是的,它在第一个请求时被解析。所有后续请求都通过配置中定义的持久性和缓存配置提供服务。
        【解决方案5】:

        这个实现不难,查看http://detectmobilebrowsers.mobi/#download

        如果您想要更多数据,首先,检查它是否真的有必要,除非您打算创建像网站这样的移动分析,原因如下:

        手机可以是:

        • 智能手机,处理 HTML5/CSS3/Javascript,有时甚至 闪光

        • 平板电脑,支持同上,但屏幕更大,更多
          接口可能性

        • 中端手机,他们可以访问基本的互联网,一些应用程序 连接到 facebook、twitter 等,但不是完全有能力的 浏览器

        • 低端手机,有WAP浏览器,网速很慢 连接

        因此,如果您可以将它们分成这 4 个不同的组,那应该足以满足您的应用程序需求。

        这里有一些有趣的链接,可以帮助您保持简单,并做您想做的事:

        祝你好运!

        【讨论】:

        • 这并不能解决问题,因为它没有检测到手机制造商和型号。
        【解决方案6】:

        我过去曾参与实施 WURFL 的内容交付系统。对我来说似乎没问题,从来没有问题!

        【讨论】:

          【解决方案7】:

          我在我的项目中使用这个 PHP 类:

          http://code.google.com/p/mobileesp/source/browse/PHP/mdetect.php

          效果非常好。很简单,没有数据库,没有远程 API,没有大文件。

          【讨论】:

          • 这仅检测用户代理的操作系统。没有关于设备名称和型号的信息。
          【解决方案8】:

          这里已经多次提到 WURFL,但您应该看看新的 WURFL 云服务 - 它是 WURFL API 的托管版本,由 WURFL 团队维护,并包括一个可能满足您需求的免费计划.

          这是一个使用 PHP WURFL 云客户端的快速 PHP 示例:

          <?php
          require_once '../Client/Client.php';
          $config = new WurflCloud_Client_Config();  
          $config->api_key = 'xxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';   
          $client = new WurflCloud_Client_Client($config);  
          $client->detectDevice();
          $brand = $client->getDeviceCapability('brand_name');
          $model = $client->getDeviceCapability('model_name');
          if ($client->getDeviceCapability('ux_full_desktop')) {  
              echo "This is a desktop web browser";  
          } else {  
              echo "This is a $brand $model";
          }
          

          请务必查看getting started guideWURFL Cloud page

          【讨论】:

          • 这个api的性能如何?由于每个 api 调用一个网络请求似乎是一个瓶颈
          • 当然对于基于云的服务,存在网络瓶颈,但客户端使用cookie缓存,因此同一用户的后续页面视图被缓存,并且不需要查找云。网络延迟非常低 - 在美国和欧洲通常约为 100 毫秒,当然这取决于您的位置和提供商。如果您需要更高的性能,您可以随时尝试独立 API。 PHP 有两个 WURFL API - PHP API 和数据库 API(以前称为 Tera-WURFL)。
          猜你喜欢
          • 2023-03-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-29
          • 1970-01-01
          • 2011-01-08
          • 1970-01-01
          相关资源
          最近更新 更多