Php, Pass Parameters from Command Line to a PHP Script

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)

PHP, pass parameters from command line to a PHP script

When calling a PHP script from the command line you can use $argc to find out how many parameters are passed and $argv to access them. For example running the following script:

<?php
var_dump($argc); //number of arguments passed
var_dump($argv); //the arguments passed
?>

Like this:-

php script.php arg1 arg2 arg3

Will give the following output

int(4)
array(4) {
[0]=>
string(21) "d:\Scripts\script.php"
[1]=>
string(4) "arg1"
[2]=>
string(4) "arg2"
[3]=>
string(4) "arg3"
}

See $argv and $argc for further details.

To do what you want, lets say

php script.php arg1=4

You would need to explode the argument on the equals sign:-

list($key, $val) = explode('=', $argv[1]);
var_dump(array($key=>$val));

That way you can have whatever you want in front of the equals sign without having to parse it, just check the key=>value pairs are correct. However, that is all a bit of a waste, just instruct the user on the correct order to pass the arguments.

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);

How do I pass parameters into a PHP script through a webpage?

Presumably you're passing the arguments in on the command line as follows:

php /path/to/wwwpublic/path/to/script.php arg1 arg2

... and then accessing them in the script thusly:

<?php
// $argv[0] is '/path/to/wwwpublic/path/to/script.php'
$argument1 = $argv[1];
$argument2 = $argv[2];
?>

What you need to be doing when passing arguments through HTTP (accessing the script over the web) is using the query string and access them through the $_GET superglobal:

Go to http://yourdomain.example/path/to/script.php?argument1=arg1&argument2=arg2

... and access:

<?php
$argument1 = $_GET['argument1'];
$argument2 = $_GET['argument2'];
?>

If you want the script to run regardless of where you call it from (command line or from the browser) you'll want something like the following:

as pointed out by Cthulhu in the comments, the most direct way to test which environment you're executing in is to use the PHP_SAPI constant. I've updated the code accordingly:

<?php
if (PHP_SAPI === 'cli') {
$argument1 = $argv[1];
$argument2 = $argv[2];
}
else {
$argument1 = $_GET['argument1'];
$argument2 = $_GET['argument2'];
}
?>

Send request parameters when calling a PHP script via command line

No, there is no easy way to achieve that. The web server will split up the request string and pass it into the PHP interpreter, who will then store it in the $_REQUEST array.

If you run from the command line and you want to accept similar parameters, you'll have to parse them yourself. The command line has completely different syntax for passing parameters than HTTP has. You might want to look into getopt.

For a brute force approach that doesn't take user error into account, you can try this snippet:

<?php
foreach( $argv as $argument ) {
if( $argument == $argv[ 0 ] ) continue;

$pair = explode( "=", $argument );
$variableName = substr( $pair[ 0 ], 2 );
$variableValue = $pair[ 1 ];
echo $variableName . " = " . $variableValue . "\n";
// Optionally store the variable in $_REQUEST
$_REQUEST[ $variableName ] = $variableValue;
}

Use it like this:

$ php test.php --param1=val1 --param2=val2
param1 = val1
param2 = val2

Passing parameters from command line to script

function getArgumentValues($argv, $seperator){
$values = "$argv[1]";
foreach($argv as $key=>$value){
if($key>1){
$values.="$seperator$value";
}
}
return $values;
}

function get_content_movies($linkMovies, $argv){
$htmlContent = file_get_contents($linkMovies);
$argvValue = getArgumentValues($argv, " ");
if(!preg_match("/href\=\"(.*?)\".*?oldtitle\=\"$argvValue/i", $htmlContent, $search)){
return null;
};
$key = array_values($search)[1];
$htmlContent = file_get_contents($key);
if(preg_match("/series/", $key)){
main();
}
else{
preg_match('/\<h3 itemprop\=\"name\"\>(.*)<\/h3>/iSU', $htmlContent, $title);
preg_match('/<iframe.*data-lazy-src=\"(.*)\".*><\/iframe>/iSU', $htmlContent, $embed_url);
preg_match('/<meta.*property="og:url".*content="(.*)".*\/>/iSU', $htmlContent, $meta_url);
$date = date('m/d/Y H:i', time());
echo "{$date} \t {$embed_url[1]} \t {$meta_url[1]} \t {$title[1]} \n";
$result = array($date, $embed_url[1], $meta_url[1], $title[1]);
$output = 'scrape.txt';
file_put_contents($output, print_r($result, true), FILE_APPEND);
}
}
get_content_movies($linkMovies, $argv);

How can I pass parameters from the command line to $_POST in a PHP script?

That's not easily doable. You can invoke the php-cgi binary and pipe a fake POST request in. But you'll need to set up a whole lot of CGI environment variables:

echo 'var1=123&var2=abc' | REQUEST_METHOD=POST  SCRIPT_FILENAME=script.php REDIRECT_STATUS=CGI CONTENT_TYPE=application/www-form-urlencoded php-cgi 

Note: Insufficient, doesn't work like that. But something like that...


It's certainly easier if you just patch the script, and let it load the $_POST array from a predefined environment variable.

$_POST = parse_url($_SERVER["_POST"]);

Then you can invoke it like _POST=var=123 php script.php for simplicity.

Pass parameters from command line to a PHP Class and Functions script

Try this code

class fibo
{
function fibo1($n)
{
$first = 0;
$second = 1;
echo "Fibonacci Series \n";
echo $first." ".$second." ";

for ($i=2; $i < $n ; $i++)
{

$third = $first + $second;
echo $third." ";

$first = $second;
$second = $third;
}
}
}
$n=getopt(null, ["n:"]);
fibo::fibo1($n['n']);

Run This Command
$ php fibo.php --n=6



Related Topics



Leave a reply



Submit