Laravel User Agent 輕松識別客戶端信息 —— jenssegers/agent,客戶端會通過這個請求向訪問網站提供瀏覽器類型、操作系統及版本,瀏覽器渲染引擎,瀏覽器語言等標識的信息。
項目地址:https://github.com/jenssegers/agent
安裝命令:
composer require jenssegers/agent
安裝成功之后我們使用tinker進行測試一下。
php artisan tinker
1、實例化一下Agent對象
use Jenssegers\Agent\Agent;
$agent = new Agent();
2、設置相關的頭信息
$agent->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2');
測試信息完整的代碼:
$ php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.1 — cli) by Justin Hileman
>>> use Jenssegers\Agent\Agent;
>>>
>>> $agent = new Agent();
=> Jenssegers\Agent\Agent {#2947}
>>> $agent->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2');
=> "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2"
>>> $agent->is('OS X');
=> true
>>> $agent->isSafari();
=> true
>>> $agent->browser();
=> "Safari"
>>> $browser = $agent->browser();
=> "Safari"
>>> $version = $agent->version($browser);
=> "5.1.7"