Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change user-agent for Symfony Panther Chromeclient

How do I change the user-agent in a headless Chrome created by Symfony's Panther createChromeClient()?

When I create a Chrome client with

$client = \Symfony\Component\Panther\Client::createChromeClient();

I see in the access_log a user-agent of

"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/77.0.3865.90 Safari/537.36"

I searched for solutions, and think I have to change the user-agent string via the arguments of the chrome, but can't find the right way, because the answers on the web aren't for PHP or Panther.

Cheers!

like image 429
Oliver Hörold Avatar asked Oct 28 '25 05:10

Oliver Hörold


1 Answers

I found it:

$client = \Symfony\Component\Panther\Client::createChromeClient(null, [
            '--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
            '--window-size=1200,1100',
            '--headless',
            '--disable-gpu',
]);

This question gave me the idea.

like image 83
Oliver Hörold Avatar answered Oct 29 '25 22:10

Oliver Hörold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!