Laravel User Agent 輕松識(shí)別客戶端信息 —— jenssegers/agent,客戶端會(huì)通過(guò)這個(gè)請(qǐng)求向訪問網(wǎng)站提供瀏覽器類型、操作系統(tǒng)及版本,瀏覽器渲染引擎,瀏覽器語(yǔ)言等標(biāo)識(shí)的信息。
項(xiàng)目地址:https://github.com/jenssegers/agent
安裝命令:
composer require jenssegers/agent
安裝成功之后我們使用tinker進(jìn)行測(cè)試一下。
php artisan tinker
1、實(shí)例化一下Agent對(duì)象
use Jenssegers\Agent\Agent;
$agent = new Agent();
2、設(shè)置相關(guān)的頭信息
$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');
測(cè)試信息完整的代碼:
$ 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"