Shell run/execute php script with parameters
test.php:
<?php
print_r($argv);
?>
Shell:
$ php -q test.php foo bar
Array
(
[0] => test.php
[1] => foo
[2] => bar
)
Run PHP script from CLI and pass parameters
You need to run PHP:
php /my/script.php arg1 arg2
Then in your PHP file:
<?php
// It's 0 based, but arg1 will be at index 1!
var_dump($argv);
Pass a variable to a PHP script running from the command line
The ?type=daily
argument (ending up in the $_GET
array) is only valid for web-accessed pages.
You'll need to call it like php myfile.php daily
and retrieve that argument from the $argv
array (which would be $argv[1]
, since $argv[0]
would be myfile.php
).
If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and Wget, and call that from cron:
#!/bin/sh
wget http://location.to/myfile.php?type=daily
Or check in the PHP file whether it's called from the command line or not:
if (defined('STDIN')) {
$type = $argv[1];
} else {
$type = $_GET['type'];
}
(Note: You'll probably need/want to check if $argv
actually contains enough variables and such)
How to pass parameters from bash to php script?
Call it as:
php /path/to/script/script.php -- 'id=19&url=http://bkjbezjnkelnkz.com'
Also, modify your PHP script to use parse_str():
parse_str($argv[1]);
If the index $_SERVER['REMOTE_ADDR']
isn't set.
More advanced handling may need getopt(), but parse_str() is a quick'n'dirty way to get it working.
How to call PHP file from a shell script file
In your php file named test.php, for example
<?php
//like programs in c language, use $argc, $argv access command line argument number and arguments, uncomment below two line to dump $argc and $argv
//var_dump($argc); //an integer
//var_dump($argv); //an array with arguments
//use args and do anything you want
echo "do my job\n";
exit(0);
then create a shell script named test.sh
#! `which bash`
php=`which php`
i=10
while [[ $i -ge 0 ]];
do
$php test.php 1 2
((i--))
done
put the two files into the same directory. Then run command in the terminal
bash test.sh
Execute a php script (with arguments) within another php script
You are not grabbing the output from that command. That's why you see nothing although the command was executed. There are several ways how to do that. This are the most common:
// test.php
<?php
$value = 123;
// will output redirect directly to stdout
passthru("php -f test1.php $value");
// these functions return the outpout
echo shell_exec("php -f test1.php $value");
echo `php -f test1.php $value`;
echo system("php -f test1.php $value");
// get output and return var into variables
exec("php -f test1.php $value", $output, $return);
echo $output;
?>
Related Topics
How to Get the Value from Object(Stdclass)
PHP Exec() Not Returning Error Message in Output
What Does the Percent Sign Mean in PHP
Get a PHP Datetime Difference in Days, Considering Midnight as a Day Change
Laravel Polymorphic Relations Has Many Through
Laravel Querybuilder How to Use Like in Wherein Function
How to Get the Length of Longest String in an Array
Is_Unique for Codeigniter Form Validation
Php: Whats the Total Length of a Post Global Variable
How to Execute and Get Content of a .PHP File in a Variable
Day of the Week to Day Number (Monday = 1, Tuesday = 2)
Add HTML Formatting in PHPmailer
Get the Metadata of an Order Item in Woocommerce 3
How to Check in PHP That I'm in a Static Context (Or Not)
Send Fcm Messages from Server Side to Android Device