2

I have a custom phantomJS procedure which I am calling with the PhantomJS PHP Library. Witht his procedure I want to load the content and extract some information of the rendered page.

The exectration works fine when calling the procedure from command line and logging the "found information" to the console. But now I want to get this information for further processing in my php script.

How to return information from the procedure to the calling php-script?

PHP

$location = '/var/www/vhosts/boos/procs';

$serviceContainer = ServiceContainer::getInstance();

$procedureLoader = $serviceContainer->get('procedure_loader_factory')
    ->createProcedureLoader($location);

$client = Client::getInstance();
$client->getEngine()->setPath('/usr/local/share/phantomjs/bin/phantomjs');
$client->getEngine()->addOption('--web-security=false');
$client->setProcedure('extractor');
$client->getProcedureLoader()->addLoader($procedureLoader);

$request  = $client->getMessageFactory()->createRequest();
$request->setUrl('http://website.de');

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

$client->send($request, $response);
// GIVE ME THE RESPONSE HERE

Procedure

var page = require('webpage').create();

// settings
page.settings.loadImages = false;
page.settings.localToRemoteUrlAccessEnabled = true;
page.settings.webSecurityEnabled = false;
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 FirePHP/4Chrome';
page.viewportSize = {
  width: 1440,
  height: 900
};

t = Date.now();

page.open('{{ input.getUrl() }}', function(status) {
  if (status == 'success') {
        // Evaluate page
        var links = page.evaluate(function() {
            // GET ALL LINKS FROM DOM 
        }

        // RETURN THE LINKS TO THE PHP SCRIPT

        phantom.exit();

  }
});
rakete
  • 2,953
  • 11
  • 54
  • 108

2 Answers2

0

I'd say it is quite extreme use of the library, which aims just

to load pages through the PhantomJS headless browser and return the page response.

Templates were designed to set parameters, whilst you are trying to implement some logic there.

If you really need to do it this way, you can play with undocumented response object. Properties you assign to the response (js procedure) will be available in $response (php) after $client->send($request, $response);:

// procedure js:
response.test = 18;

// php:
echo $response->get('test'); // prints 18

See how it is used in the default templates, e.g. here.

Alex Blex
  • 34,704
  • 7
  • 48
  • 75
  • There is no get() method in the Response-Class – rakete Mar 08 '16 at 17:16
  • I am pretty sure [it is](https://github.com/jonnnnyw/php-phantomjs/blob/8c0c2d72ed3d93f69aeb71e705619f0248b1425f/src/JonnyW/PhantomJs/Procedure/Output.php#L80), but I see your point. Somehow [\JonnyW\PhantomJs\Procedure\OutputInterface](https://github.com/jonnnnyw/php-phantomjs/blob/8c0c2d72ed3d93f69aeb71e705619f0248b1425f/src/JonnyW/PhantomJs/Procedure/Procedure.php#L92) turns into [\JonnyW\PhantomJs\Http\ResponseInterface](https://github.com/jonnnnyw/php-phantomjs/blob/8c0c2d72ed3d93f69aeb71e705619f0248b1425f/src/JonnyW/PhantomJs/Client.php#L156). – Alex Blex Mar 08 '16 at 17:32
0

I recently published a project that gives PHP access to a browser. Get it here: https://github.com/merlinthemagic/MTS

Just like the library you currently use, this also relies on PhantomJS.

After downloading and setup you would simply use the following code:

$myUrl          = "http://website.de";
$windowObj      = \MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);

//set the size of the window
$windowObj->setSize(1440, 900);

//now you can either retrive the DOM and parse it, like this:
$domData    = $windowObj->getDom();

//or you could write a function in javascript and then trigger it, like this:
$script = "function getLinks() {
                var array = [];
                var links = document.links;
                for(var i=0; i<links.length; i++) {
                    array.push(links[i].href);
                }
                return JSON.stringify(array);
            }";
//load the script on the page
$windowObj->loadJS($script);
//call the function
$scriptReturn   = $windowObj->callJSFunction("getLinks");
//convert the Json to an array of links
$linkArr        = json_decode($scriptReturn, true);
MerlinTheMagic
  • 575
  • 5
  • 16