exec() phantomjs doesn't execute, no errors displayed - php

I'm on a Windows platform, I have the phantomjs executable on app/webroot/phantomjs and a js in the same folder.
When I do on php:
$response = exec($this->webroot . 'app/webroot/phantomjs/phantomjs getweb.js');
nothing happens. This is the content of the js:
console.log('Hello, world!');
phantom.exit();
I use absolute paths because this needs to be executed on a webpage online.
EDIT 1:
when using
$response = exec($this->webroot . 'app/webroot/phantomjs/phantomjs getweb.js 2>&1', $s, $o);
echo $response;
echo "<pre>";
print_r($s);
echo "</pre>";
echo "<pre>";
print_r($o);
echo "</pre>";
I get
Array
(
)
1
EDIT 2:
this works fine:
echo exec("whoami");
EDIT 3:
This does nothing:
$response = exec($this->webroot . 'app/webroot/phantomjs/phantomjs getweb.js 2>&1', $s, $o);

I was facing the same problem..
The thing is phantomjs requires complete path for all.
Here is the solution I came up with:
$getout = exec('D:\\xampp\\htdocs\\phantomjsdir\\phantomjs.exe D:\\xampp\\htdocs\\rasterisejsdir\\rasterize.js http://localhost/pagetobecaptured/test D:\\xampp\\htdocs\\outputfiledir\\test2.jpg "1800px*840px"',$o,$e);

Related

Cannot print, echo or var_dump in Codeignitor [PHP]

I am new to Codeignitor, but I have some years of experience with PHP.
Although my Environment in set to development in index.php file on the root of the project,
if($some_stament=="true") {
$relativePath = $w->getRelativePath();
$sep = strlen($relativePath)===0?'':'/';
$command = str_replace("'", "", $previewSyncCommand[$site].' '.escapeshellarg($relativePath).$sep.$fromName.' '.$fullToPath);
var_dump($command);
$return_var=-1;
exec($command, $outputArray, $return_var);
$output = implode("\n",$outputArray);
$returnMessage = RsyncWrapper::$rsyncStatus[$return_var];
}
or
print_r($bar_variable);
echo $foo_bar;
print('example to print to screen text');
do not work, in fact break further execution of the if() statement, even if I don't call exit() or die();
The strangest part is that, if I remove all the dumps, prints or echos, the code run perfectly!
Does anyone have a ideia?

strikethrough characters on php stream output

I am using php ssh2_exec to execute a ps aux command on a remote Linux server... The server initiating the connection is Ubuntu 14.04 and the server I am communicating with is Centos 6.6.
Both systems are fully updates and I am using the following versions of PHP and Apache on the Ubuntu system:
apache2 2.4.7-1ubuntu4.5
libapache2-mod-php5 5.5.9+dfsg-1ubuntu4.11
php5 5.5.9+dfsg-1ubuntu4.11
I am using the following code to send the command and capture the stream:
echo '<pre>';
$stream = ssh2_exec($connection, $command);
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
while($line = fgets($stream_out)) {
flush();
echo $line."<br />";
}
echo '</pre>';
echo '<br />';
unset($connection);
$command is defined as: $command = "ps aux |sed '/[.*]/d'";
The command runs, but the returned text, while showing the processes running on the remote system, is stricken through text... Below is a link to an image of what is happening.
https://www.joeman1.com/images/stikethoughtest.png
(I would have posted the image, but Im new around here and need some reputation ;)).
This does not happen when I use php -f on the command line, just in a browser - IE, Firefox, and Chrome was tested.
Any ideas on how to resolve this? If you need more information, please let me know.
Thanks!
Joe
Try checking your HTML Source. Or:
<?php
echo '<pre>';
$stream = ssh2_exec($connection, $command);
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
while($line = fgets($stream_out)) {
flush();
// convert the string to HTML Entities
// Since you're getting data from a stream, no telling what might come out.
echo htmlentities($line)."<br />";
}
echo '</pre>';
echo '<br />';
unset($connection);
?>

shell_exec in PHP returns empty string

shell_exec and exec are not returning any content. I can't figure out what's wrong.
Here's some code:
echo 'test: ';
$output = shell_exec('whoami');
var_export($output, TRUE);
echo PHP_EOL . '<br>' . PHP_EOL;
And here's the source of the output
test 2:
<br>
I do not have control over the host, but I believe they're running SuPHP. According to phpinfo, safe_mode is off. Running whoami from SSH outputs the expected value.
I'm at a loss. Any idea how to debug this?
You're never printing the $output variable. The var_export() call returns the content of the variable when you call it with a true second parameter, it does not print it directly.
If you want the output from a shell command read back into PHP, you're probably going to need popen(). For example:
if( ($fp = popen("some shell command", "r")) ) {
while( !feof($fp) ) {
echo fread($fp, 1024);
flush(); // input will be buffered
}
fclose($fp);
}

PHP exec() function windows 7 IIS 7.5

I am trying to run an a command line util using exec() but it doesnt return anything. I have read on php.net that the permissions on cmd.exe need to be set to allow the iis user to run it. I have not been able to do this using any method I can think of. cacls icacls and the standard security screen dont work. I am logging the output to a mysql database.
my code looks like this:
$Ret = array();
$err = "";
exec("dir", $Ret, $err);
in the db I get array for $Ret
Either something is wrong with the command, I doubt it, or I need to set the permissions somehow.
Please help.
Change the line:
exec("dir", $Ret, $err);
to:
$r = exec("dir", $Ret, $err);
echo $r;
You need to capture the return value of exec into a variable then render that.
Or try running this:
<?php
$a = array();
$e = "";
echo "exec'ing<br/>";
$r = exec("dir", $a, $e);
echo "var_dumping<br/>";
var_dump($a);
echo "=====================\n";
var_dump($e);
echo "=====================\n";
var_dump($r);
?>
If you're running this in a browser with xdebug turned off then you might need to view-source to see the results.

How to check PHP filecode syntax in PHP?

I am in a need of running the PHP parser for PHP code inside PHP. I am on Windows, and I have tried
system("C:\\Program Files (x86)\\PHP\\php.exe -l \"C:/Program Files (x86)/Apache/htdocs/a.php\"", $output);
var_dump($output);
without luck. The parameter -l should check for correct PHP syntax, and throw errors if some problems exist. Basically, I want to do something similar to this picture:
(source: mpsoftware.dk)
That is, to be able to detect errors in code.
This seems to work:
<?php
$file = dirname(__FILE__) . '/test.php';
//$output will be filled with output from the command
//$ret will be the return code
exec('php -l ' . escapeshellarg($file), $output, $ret);
//return code should be zero if it was ok
if ($ret != 0) {
echo 'error:';
var_dump($output);
} else {
echo 'ok';
}
The runkit extension provides a function to do that:
runkit_lint_file()
If you can not use runkit then you only other way is to do what you already tried:
echo passthru("php -l ".__FILE__);
Try fixing your path with realpath() if it does not work.

Categories