[英]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.