php-phantomjs icon indicating copy to clipboard operation
php-phantomjs copied to clipboard

Not stable , getting status 408 and null content

Open AymanEG opened this issue 7 years ago • 7 comments

Hello there,

Thanks for your great effort to make this project real.However, I am getting random errors which I can not figure out why and I hope someone can help.

The script is workng fine for google.com , but when I try to get with other websites it start through errors and retruning status 408 .Some time I get the status 200 but the content to NULL if I try to get an xml file in this url , any advices ?! One more thing when I uncomment the debug line the script hangout , how I can echo the debug the right way ?!

Here is my code

<?php
require_once __DIR__ . '/vendor/autoload.php';
use JonnyW\PhantomJs\Client;

if ($_GET) {
    $link = $_GET['link'];
} else {
    $link = $argv[1];
}

if (! empty($link) ) {
	$client = Client::getInstance();
	$client->getProcedureCompiler()->disableCache();
	$client->getEngine()->setPath('C:/scripts/bin/phantomjs.exe');
	//$client->getEngine()->debug(true);
	$client->getEngine()->addOption('--load-images=true');
	$client->isLazy(); // Tells the client to wait for all resources before rendering

	$request = $client->getMessageFactory()->createRequest($link, 'GET');
	//$request->setTimeout(5000);	

	$response = $client->getMessageFactory()->createResponse();

	// Send the request
	$client->send($request, $response);

	$return['Status'] = $response->getStatus();
	$return['Link'] = $link;
	$return['Content'] = $response->getContent();
	//$return['Log'] = $client->getLog();

	echo json_encode($return, JSON_PRETTY_PRINT);
}

AymanEG avatar Jan 04 '17 14:01 AymanEG

Hi,

I've got the same error when trying to generate a PDF from a local URL. Maybe a phantom.js bug? http://stackoverflow.com/questions/35739174/phantomjs-408-error-socket-operation-timed-out

pepoteloko avatar Mar 24 '17 11:03 pepoteloko

I am facing same issue, it always returns status code 408 for any site, if you guys had fixed, please help me

mrcodecloud avatar Mar 06 '18 04:03 mrcodecloud

This works for me in the same situation: $client->getEngine()->addOption('--load-images=false');

zaivst avatar May 07 '19 01:05 zaivst

$request->addHeader("User-Agent", "Your Application Name");

I have tried it and it working form me!

chiragsenjaliya avatar Oct 23 '19 19:10 chiragsenjaliya

Just stumbled upon the same issue. What worked for me was simply setting a longer timeout. Especially if you are iterating over multiple pages or your web server is under some strain due to db calls or similar.

$request->setTimeout(20000); // 20 seconds

rolandihms avatar Jul 21 '22 10:07 rolandihms

It is amazing that a post from 2017 still get replies!

AymanEG avatar Jul 21 '22 10:07 AymanEG

It is amazing that a post from 2017 still get replies!

Yeah true that. Been using PhantomJS forever and its never let me down :)

rolandihms avatar Jul 21 '22 10:07 rolandihms