简体   繁体   English

Laravel Http 根据要求设置自定义用户代理

[英]Laravel Http set custom user agent on request

I've got a cron set up in my Laravel project that makes a GET request to an endpoint, I'd like to set my own customer user agent that this request uses so that it can easily be identified amongst other potential requests to the same server.我在我的 Laravel 项目中设置了一个 cron,它向端点发出 GET 请求,我想设置我自己的客户用户代理,该请求使用,以便可以在其他潜在请求中轻松识别它服务器。

My current code within my function is:我的 function 中的当前代码是:

$response = Http::timeout($timeout)->get($url);
$code = $response->getStatusCode();

I'd like to set a custom user agent for this request like so:我想为此请求设置一个自定义用户代理,如下所示:

Mozilla/5.0+(compatible; EXAMPLE/2.0; http://www.EXAMPLE.com/ ) Mozilla/5.0+(兼容;EXAMPLE/2.0; http://www.EXAMPLE.com/

Can't seem to find anything in the docs to suggest how to do this似乎无法在文档中找到任何建议如何执行此操作的内容

Since Laravel v8.8 ( this pull request ) you can use the withUserAgent method to set your own user agent.由于 Laravel v8.8(此拉取请求),您可以使用withUserAgent方法设置您自己的用户代理。

Http::withUserAgent($userAgent)->get($url);

Before that you could set your own header like this:在此之前,您可以像这样设置自己的 header:

Http::withHeaders(['User-Agent' => $userAgent])->get($url);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以设置$ _SERVER ['HTTP_USER_AGENT']? - Is it possible for $_SERVER['HTTP_USER_AGENT'] to not be set? 未定义索引:少数用户的 Laravel 中的 HTTP_USER_AGENT - Undefined index: HTTP_USER_AGENT in Laravel for few users 如何在Amazon API Gateway上检测GET请求的HTTP用户代理? - How to detect HTTP user agent of GET request on Amazon API Gateway? 为get_browser()设置$ _SERVER ['HTTP_USER_AGENT'] - Set $_SERVER['HTTP_USER_AGENT'] for get_browser() 在何处以及如何设置Vary:User-Agent HTTP标头 - Where and How to set Vary: User-Agent HTTP Header 在Laravel中,在自定义http请求中传递文件 - In Laravel, passing a file in a custom http request 当自定义http标头设置为发布请求时,Laravel Input :: get('params.id')不起作用 - Laravel Input::get('params.id') is not working when custom http header is set to the post request 我应该为Simplepie设置自定义用户代理吗 - Should I set up a custom user-agent for Simplepie Zend_Soap_Client可以设置自定义用户代理吗? - Can Zend_Soap_Client set custom user-agent? 如何使用修改的User-Agent标头javascript或php进行跨源Http发布请求? - How to make a cross origin Http post request with modified User-Agent header javascript or php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM