[擴展推薦]Laravel User Agent 輕鬆識別客戶端信息

說明

laravel-agent 是一個爲 Laravel 定製的客戶端識別擴展包, 基於 Mobile Detect , 提供了非常優雅的接口.

安裝

使用 composer 安裝:

composer require jenssegers/agent

修改  app/config/app.php 添加 ServiceProvider :

'Jenssegers\Agent\AgentServiceProvider',

修改 app/config/app.php 添加 alias:

'Agent' => 'Jenssegers\Agent\Facades\Agent'

基礎用法

// 操作系統Agent::is('Windows');Agent::is('Firefox');Agent::is('iPhone');Agent::is('OS X');// 廠商產品定位Agent::isAndroidOS();Agent::isNexus();Agent::isSafari();// 設備類型Agent::isMobile();Agent::isTablet();Agent::isDesktop();// 語言$languages = Agent::languages();// ['nl-nl', 'nl', 'en-us', 'en']// 是否是機器人Agent::isRobot();// 獲取設備信息 (iPhone, Nexus, AsusTablet, ...)Agent::device();// 系統信息  (Ubuntu, Windows, OS X, ...)Agent::platform();// 瀏覽器信息  (Chrome, IE, Safari, Firefox, ...)Agent::browser();// 獲取瀏覽器版本$browser = Agent::browser();$version = Agent::version($browser);// 獲取系統版本$platform = Agent::platform();$version = Agent::version($platform);

一個例子

下面是一段微信掃一掃下載 App 的鏈接的邏輯

$android_download_link = 'http://7xim5a.com2.z0.glb.qiniucdn.com/app.apk';$weichat_download_link = 'http://a.app.qq.com/o/simple.jsp?pkgname=com.app.indentifior';$ios_download_link     = 'https://itunes.apple.com/cn/app/kou-yu-jiao-er/app-id*?l=en&mt=8';$download_link         = '';// 如果是移動端訪問的話 (phones or tablets).if ( Agent::isMobile() ){
    if( Agent::isiOS() )
    {
        $download_link = $ios_download_link;
        // 微信訪問        if (strpos(Agent::getUserAgent(), 'MicroMessenger') !== false)
        {
            // echo "<h1>微信用戶若不能安裝, 請點擊右上角的跳轉按鈕, 選擇 \"在 Safari 中打開\" 即可正常跳轉哦 ^_^ .</h1>";
            // exit;            $download_link = $weichat_download_link;
        }
    }
    if( Agent::isAndroidOS() )
    {
        $download_link = $android_download_link;
        // 微信訪問        if (strpos(Agent::getUserAgent(), 'MicroMessenger') !== false)
        {
            // 使用應用寶可以訪問            $download_link = $weichat_download_link;
        }
    }}

View

<!doctype html><html lang="en"><head>
    <meta charset="UTF-8">
    <title></title></head><body>
    <h1>處理中... </h1>

    <script type="text/javascript">
        window.onload=function(){
          setTimeout("window.location.href='<?php echo $downloadlink; ?>'",1000);
        }
    </script></body></html>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章