有时候我们访问远程URL文件,需要指定User-Agent,这种情况不常见,但是如果你做的多了,你就会发现,有的文件只有使用浏览器才能打开,使用Curl或其他库却只能获取到403.
这时候我们需要指定一个User-Agent.
安装
composer require campo/random-user-agent使用
使用方式非常简单,只要最简单的调用即可:
echo \Campo\UserAgent::random(), "\n";指定一些User-Agent类型,比如操作系统或者设备类型:
echo \Campo\UserAgent::random([
    'os_type' => 'Windows',
    'device_type' => 'Mobile'
]), "\n";也可以使用同时穿多个类型:
echo \Campo\UserAgent::random([
    'os_type' => ['Android', 'iOS'],
    'device_type' => ['Mobile', 'Tablet']
]), "\n";支持的全部筛选如下:
- agent_name
- agent_type
- device_type
- os_name
- os_type
可以查看支持的所有的参数:
- UserAgent::getDeviceTypes()
- UserAgent::getAgentTypes()
- UserAgent::getAgentNames()
- UserAgent::getOSTypes()
- UserAgent::getOSNames()
原文标题: [campo/random-user-agent]随机伪造你的User-Agent
原文地址: https://phpreturn.com/index/a623b0546e5d7f.html
原文平台: PHP武器库
版权声明: 本文由phpreturn.com(PHP武器库官网)原创和首发,所有权利归phpreturn(PHP武器库)所有,本站允许任何形式的转载/引用文章,但必须同时注明出处。
