Skip to content

PHP PhantomJS Change User Agent String

PHP PhantomJS Change User Agent String

Noticed that google isn’t really useful in this case (there’s no copy and paste answer). Google for me only shows a bunch of people having issues and no easy solution when in fact the solution is quite simple

First things first, you have to check to see if your PHP-PhantomJs library version is 3.x or 4.x

If it is 3.x, to change the user-agent string, you just have to do this

$request->addSetting('userAgent', 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0');

If it is 4.x, to change the user-agent string, you just have to do this

$request->addHeader('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0');

Of course, feel free to change the new value of your user-agent to whatever you want it to be.

To verify that it is working, if your testing locally, then you can use tools such as Fiddler to check the headers. Alternatively, you can dump out the $request and $response variables, like so,

var_dump($request); // $request is the return value of either createRequest or createCaptureRequest function
var_dump($response); // $response is the return value of createResponse function

If you have any questions feel free to let me know in the comments below.

 

 

Enjoyed the content ? Share it with your friends !
Published inDevelopmentProgramming

Be First to Comment

Leave a Reply

Your email address will not be published.