Run Php Code in Linux - php

I would like to run some php code in my linux scripts to do some automation.I have searched on Google but I can't find any resources on it.Can anyone suggest a way to do this?
#!/bin/bash
<?php echo "this is a test "?>
Error Message:syntax error near unexpected token 'newline'

You can run your script using command in bash on linux:
php /path/to/file.php
also you can put it into some bash script.

#!/usr/bin/php
<?php
echo 'foo';
use which php to find out where your php interpreter is hidden and make sure you can execute your file chmod u+x foo.php

Related

Running PhantomJS from PHP with exec()

I've got the following script:
#!/bin/sh
export DISPLAY=:0
phantomjs --version
It try to run it from the following PHP script:
<?php
$result = shell_exec('sh test.sh');
echo $result;
?>
This script return the following error:
[Thu Jun 19 10:31:31 2014] [error] [client] test.sh: line 3: phantomjs: command not found
I tried to run phantomjs -v by hand in a console, and it runs fine. I checked the PATH, and phantomjs is correctly defined and found.
The execution environment is a virtual Server with LiveConfig.
Can someone help me understand what I'm doing wrong ?
It could be an issue with shell_exec() and line breaks,
try adding "2>&1" to the string you are passing:
$result = shell_exec('sh test.sh 2>&1');
this worked for me, found it in the top comment here, naturally ;)
Your PATH probably lacks the location for the phantomjs executable. PhantomJS is probably installed in /usr/local/bin so you need to add this to your PATH variable:
#!/bin/sh
export DISPLAY=:0
PATH=$PATH:/usr/local/bin
phantomjs --version
To check what the current PATH is, you could begin the shell script with:
#!/bin/sh
echo $PATH
<?php
exec('/usr/local/bin/phantomjs path/somescript.js');
?>
Yes. Sometimes phantomjs don't need full path in some environment without generate any error. However, sometimes it does.
Always use the full path for all argument in the php command.
Did you use the fullpath for hello.js?
Do not use exec(). Never. It's a bad way.
Use the php-phantomjs and PhantomJS Runner instead.

Execute sh script on linux with php

I'm stuck with the following problem:
I try to execute a sh script using php.
My attempts:
I can run other commands. As an example I can run the 'cat' programm to display the script I'd like to execute: <?php echo exec("cat /srv/web/scripts/start_server.sh"); ?>
The following snippets don't work:
<?php echo `/srv/web/scripts/start_server.sh`; ?>
<?php echo `sh /srv/web/scripts/start_server.sh`; ?>
<?php exec("sh /srv/web/scripts/start_server.sh"); ?>
<?php exec("/srv/web/scripts/start_server.sh"); ?>
The same with: system(), popen(), proc_open(), passthru(), shell_exec()
I tried various file permissions on the script. Even rwxrwxrwx and rwsrwxrwx.
PHP continues execution after the execution as if it worked. No PHP error is generated. When I start the script on the console (like: sh /srv/web/scripts/start_server.sh) it works.
I tested with a simple script, which works on the console but not when I try to execute it with php:
#!/bin/sh
echo hallo > /srv/web/scripts/file.txt
There is no safe-mode enabled. The exec function is nowhere disabled.
As an environment I use ipfire core 71 on an alix board. Apache as httpd and php5 as php interpreter.
I hope someone can help me.

How can I run curl as shell command line in PHP

If I try to run this inside a script:
<?php exec("curl http://ww.google.com") ?>
I get:
-bash-3.2$ php test.php
sh: /curl: No such file or directory
using shell_exec:
PHP Warning: shell_exec(): Cannot execute using backquotes in Safe Mode...
How can I run curl as shell command line?
Those errors are happening on Linux, on my mac works.
The issue is that PHP safe mode is on and it is better to use the full path to run cURL (thanks ghostJago and amosrivera). Running the script with the following command fixed the issue:
php -dsafe_mode=Off test.php
I do not want to change the php.ini but it could be a solution too.
shell_exec tells the safe mode problem, but exec just tell you an wrong message, hopefully I tried both exec and shell_exec.
Disable safe mode in your php.ini file. Also check if you do have curl installed.
safe_mode = Off
To convert from an bash command (like you can copy from chrome-dev-tools) to php take a look at this: https://incarnate.github.io/curl-to-php/
at the commandline, do this:
which curl
This will give you the absolute path to the curl program.
Then double check that safe_mode = Off is in your php.ini.
When you've done this, change your code to:
<?php exec("path/you/got/from/which/curl http://www.google.com") ?>

Running php script (php function) in linux bash

How we run php script using Linux bash?
php file test.php
test.php contains:
<?php echo "hello\n" ?>
From the command line, enter this:
php -f filename.php
Make sure that filename.php both includes and executes the function you want to test. Anything you echo out will appear in the console, including errors.
Be wary that often the php.ini for Apache PHP is different from CLI PHP (command line interface).
Reference: https://secure.php.net/manual/en/features.commandline.usage.php
First of all check to see if your PHP installation supports CLI. Type: php -v. You can execute PHP from the command line in 2 ways:
php yourfile.php
php -r 'print("Hello world");'
There are two ways you can do this. One is the one already mentioned, i.e.:
php -f filename.php
The second option is making the script executable (chmod +x filename.php) and adding the following line to the top of your .php file:
#!/path/to/php
I'm not sure though if a webserver likes this, so if you also want to use the .php file in a website, that might not be the best idea. Still, if you're just writing some kind of script, it is easier to type ./path/to/phpfile.php than having to type php -f /path/to/phpfile.php every time.
Simply this should do:
php test.php
just run in linux terminal to get phpinfo .
php -r 'phpinfo();'
and to run file like index.php
php -f index.php
php -f test.php
See the manual for full details of running PHP from the command line
php test.php
should do it, or
php -f test.php
to be explicit.
I was in need to decode URL in a Bash script. So I decide to use PHP in this way:
$ cat url-decode.sh
#!/bin/bash
URL='url=https%3a%2f%2f1%2fecp%2f'
/usr/bin/php -r '$arg1 = $argv[1];echo rawurldecode($arg1);' "$URL"
Sample output:
$ ./url-decode.sh
url=https://1/ecp/

PHP shell_exec() - having problems running command

Im new to php shell commands so please bear with me. I am trying to run the shell_exec() command on my server. I am trying the below php code:
$output = shell_exec('tesseract picture.tif text_file -l eng');
echo "done";
I have the picture.tif in the same directory as the php file. In my shell I can run this without a problem.
It takes a while to run the code, then it doesnt make the text_file like it does when I run it in command prompt.
Per your comment:
Should I write a loop in shell
instead?
You can write a very simple shell script to run the command in a loop. Create the script file first:
touch myscript.sh
Make the script executable:
chmod 700 myscript.sh
Then open it with a text editor such as vim and add this:
for (( i = 0 ; i <= 5; i++ ))
do
tesseract picture.tif text_file -l eng
done
Thats the very basics of it (not sure what else you need), but that syntax should help get you started. To run the script, do this if you're in the same directory as the script:
./myscript.sh
Or specify the full path to run it from anywhere:
/path/to/mydir/myscript.sh
Could this be a permissions issue? My guess is that PHP isn't running with the same permissions that you do when you execute the command directly from the command prompt. What OS are you running on?

Categories