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 !

How to take screenshot of url with PHP

How to take screenshot of url with PHP

Here is an example and source code of how to take screenshot of url with php. I have taken this code from one of my projects. It is also able to handle https and optionally, follow through with redirects.

Requirements :
-PhantomJS standalone executable(.exe)
-PHP library for phantomjs

Setup :

  1. Follow the installation for PHP PhantomJS here
  2. Set the path to your phantomjs executable file in the code

    $client->getEngine()->setPath(__DIR__.’/bin/phantomjs.exe’);

    Note : You may have to rename /bin/phantomjs to /bin/phantomjs.exe

Additional features :
– customize width/height of screenshot
– support http and https
– follow redirects

 

require 'vendor/autoload.php';

use JonnyW\PhantomJs\Client;


function get_redirect_url($url)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Set to true to make curl follow the url
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$a = curl_exec($ch); 
	$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This gets the last location/url after all the redirection has been followed through

	return $url;
}
function get_http_response_code($url)
{
	$ch = curl_init($url);
	curl_exec($ch);
	$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	return $code;
}
function is_redirect($url) 
{
	$code = get_http_response_code($url);
	if (($code == 301) || ($code == 302)) {
		return true;
	}
	return false;
}



function screenshot($width, $height, $url, $filePath="file.jpg", $followRedirect=false)
{
        $path = pathinfo($filePath);
        $directory = $path['dirname'];

	if( $followRedirect && is_redirect($url) ) 
	{
		$redirectedUrl = get_redirect_url($url);
		echo "{$url} redirects to {$redirectedUrl} <br/>";
		$url = $redirectedUrl;
	}		

	// Ensure the path is writable to PhantomJS/PHP
	if( !is_writable($filePath) )
	{
		//echo "$directory of $filePath is not writable. Chmod-ing <br />";
		//return false;
	}

	$client = Client::getInstance();
	$client->getEngine()->setPath(__DIR__.'/bin/phantomjs.exe');
	// HTTPS support
	$client->getEngine()->addOption('--ssl-protocol=any');
	$client->getEngine()->addOption('--ignore-ssl-errors=true');
	$client->getEngine()->addOption('--web-security=false');

	$width  = $width ? $width : 1280;  // Note : Specifying a 800x600 may be too small and result in @media css determining
	$height = $height ? $height : 800; // it to be a mobile instead of desktop. Generally a 1280x800 should be good.
	$top    = 0;
	$left   = 0;


	//@see JonnyW\PhantomJs\Http\CaptureRequest
	$request = $client->getMessageFactory()->createCaptureRequest($url, "GET"); //('http://jonnyw.me', 'GET');
	$request->setOutputFile($filePath);
	$request->setViewportSize($width, $height);
	$request->setCaptureDimensions($width, $height, $top, $left);
	$request->setDelay(3); // Depends on how long it takes for the webpage to fully load. this vs setTimeout(x) ?
	$request->setTimeout(1000); // Not sure the diff between setDelay and setTimeout though...

	//@see JonnyW\PhantomJs\Http\Response 
	$response = $client->getMessageFactory()->createResponse();

	// Send the request
	$client->send($request, $response);
	
	return file_exists($filePath); // Check if screenshot exists
}

 

Enjoyed the content ? Share it with your friends !