Neewbie : using phantomjs in a php script

6,874 views
Skip to first unread message

Joe Di Maggio

unread,
Aug 27, 2013, 1:46:34 PM8/27/13
to phan...@googlegroups.com

Hi I run this scrip in the console and it works fine.



//exemple pour PhantomJS 1.2
var page = new WebPage();
page
.open('http://parisjs.org/', function (status) {
   
if ('success' !== status) {
        console
.log("Error");
   
} else {
   
   
   
      console
.log(page.content);
     
        phantom
.exit();
   
}
});



But when I run it via php with
 echo exec

nothing came back I try to add a

return page.content

in the JS script but it doesn't works I am so stupid ???

Thanks for anyone who will help me on this issue

James Greene

unread,
Aug 27, 2013, 2:01:20 PM8/27/13
to phan...@googlegroups.com
No, you're not stupid... PHP is just "special".

As you can see on the Issue Tracker, there are a fair number of issues all related to executing PhantomJS from PHP:

:-\


Sincerely,
    James Greene



--
You received this message because you are subscribed to the Google Groups "phantomjs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to phantomjs+...@googlegroups.com.
Visit this group at http://groups.google.com/group/phantomjs.
For more options, visit https://groups.google.com/groups/opt_out.

Bryan Bishop

unread,
Aug 27, 2013, 2:17:00 PM8/27/13
to phantomjs, Bryan Bishop
On Tue, Aug 27, 2013 at 1:01 PM, James Greene <james.m...@gmail.com> wrote:
As you can see on the Issue Tracker, there are a fair number of issues all related to executing PhantomJS from PHP:

Darren Cook

unread,
Aug 27, 2013, 9:30:31 PM8/27/13
to phan...@googlegroups.com
> No, you're not stupid... PHP is just "special".
>
> As you can see on the Issue Tracker, there are a fair number of issues all
> related to executing PhantomJS from PHP:
> https://github.com/ariya/phantomjs/search?q=php&ref=cmdform&type=Issues

Are all these issues related to running PHP through a web server? And/or
in safe mode? (I'm not even sure it *should* work in safe mode.)

It works fine for me from PHP; but I think I've only ever tried it from
the commandline, and through PHPUnit. Here is the code I use (the
PhantomJS script has been built up in $s):


$temp_filename=tempnam(sys_get_temp_dir(),'UPJSB');
file_put_contents($temp_filename,$s);

$command="/usr/local/bin/phantomjs ".escapeshellarg($temp_filename);

$response=shell_exec($command);


Because my SSL tests access a server with a self-signed cert, the actual
command used is:
$command="/usr/local/bin/phantomjs --ignore-ssl-errors=true
".escapeshellarg($temp_filename);


Darren

Darren Cook

unread,
Aug 27, 2013, 9:37:11 PM8/27/13
to phan...@googlegroups.com
> As you can see on the Issue Tracker, there are a fair number of issues all
> related to executing PhantomJS from PHP:

Do you think there would be interest in a PhantomJS PHP module? It could
be something that ISPs could install and make available (perhaps even
when they insist on safe mode).

My company has the expertise to do this, but it would need to be
sponsored development; released as open-source at the end, of course.

Darren

Andrew Hamilton

unread,
Aug 28, 2013, 10:08:52 AM8/28/13
to phan...@googlegroups.com
Just another possibility, but echoing an exec only prints the last line, where running phantom directly would print all of the content. If you're running PHP from the command line, you can use:

passthru('/path/to/phantomjs /path/to/example.js');

to see all of the output. Not sure if this is what you really meant, but no other replies touched on this. 

news & news

unread,
Aug 28, 2013, 1:24:37 PM8/28/13
to phan...@googlegroups.com
Thanks for the help I finally succed by using a class that rip off the html from the output of phantomjs, and now I am more aware of issue that could come from it


2013/8/28 Darren Cook <dar...@dcook.org>

--
You received this message because you are subscribed to a topic in the Google Groups "phantomjs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/phantomjs/s8RS-Glf7fg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to phantomjs+...@googlegroups.com.

Hudson Atwell

unread,
May 28, 2015, 6:59:12 PM5/28/15
to phan...@googlegroups.com
Hello Joe,

I know it has been awhile but would you share an example of how you made it through?
Reply all
Reply to author
Forward
0 new messages