I have a file hello.pl and i want to call in php. I used exec(perl perl_tests\hello.pl) and I
want to send variables in to this pl file can you give me advice?
exec('perl perl_tests\hello.pl arg1 arg2 arg3 ....') will do the trick.
After a minute of googling: http://www.ryerson.ca/perl/runningAScript.html. There is a command line entry at the bottom.
Function exec() simply runs console command. So to call your script with parameters just put inside this function a string which you run in command line.
Related
When I use web browser I pass parameters by this way:
http://localhost/script.php?nr=444524
and get it this way:
$var = $_GET('nr');
print_r($var);
but how to achieve the same result (pass and get parameters) when I compile same script with cmd on windows?
c:\php.exe script.php ?nr=444524
this way doesn't work
It doesn't work that way. $_GET is a variable created to feed in data from an HTTP request.
On the command line you enter arguments as:
php script.php 444524
From here you can ready the arguments as print_r($argv);.
All the words you put into the command line, starting with the script name can be found in the global variable $argv. Launch your script with various parameters and check the output of print_r($argv); to see what you get.
Check the documentation here
I'm working on a PHP script in which I have to run shell script,
I have 2 option:
If I'm able to send php variable value to shell
Or I can write shell directly in PHP
I used
shell_exec(dirname(__FILE__) ."/shl.sh");
for execute shell .... Now the problem is .. if i use #!/usr/bin/php in shell it only resolve code within <?php ?> and print shell statement directly on screen.
The best option (by far!) would be to modify the external script to accept command line parameters.
So instead of
shell_exec('sh.sh');
where all variables are embedded, make it into
shell_exec("./sh.sh $opt1 $opt2");
where you can pass your variables easily.
An example for a bash script to use these arguments would be :
#!/bin/bash
echo "My $1 will kick your $2 anytime"
which will replace $1 with the first argument, and $2 with the second.
OK, the question is simple though I can't find a real working solution.
I want to be able to define something while invoking a specific script.
I have tried it like php -d DEBUG_ON myscript.php but it's not working (when testing if (defined("DEBUG_ON")) { } inside the script, it returns false)
Also tried something along the lines of php -r ('define("DEBUG_ON",1);') myscript.php; which doesn't work either.
So, any ideas? (Or any suggestions on how I could achieve the very same effect?)
Use $argv to pass arguments from command line. Then define them accordingly in the beginning of the script.
if(php_sapi_name() === 'cli') {
define('DEBUG_ON', $argv[1]);
}
If you put this code in the beginning of your script, it should then define DEBUG_ON to whatever you pass as argument from commandline: php myscript.php arg1
You can also define($argv[1], $argv[2]); and then use php myscript.php DEBUG_ON 1 to define DEBUG_ON as 1.
I am creating PHP script which is taking arguments from command line right now but after sometime it may change to simply including my PHP script.
How can I prepare my script for both scenarios?
Can I create such PHP script so that it can take argument from command line and from other script which is simply including my script?
let's say script A is the first script that can be called from the commnad line and included in another script and script B is the one that includes it.
if you include script A in B you'll have access to any variable in script B. so why don't you add a check in script A to see if a param has been passed from the command line, if no params have been passed from the command line use whatever variables you created in script B
If I understand well, you'll be forced to use a parameter to tell if your script is using the commandline parameter, or if he will call your other script.
In your script, you can do something like :
if (!strcmp($argv[1], "-s"))
shell_exec('php YourOtherScript.php [put_args_here]');
else
your_current_script($argv);
Then, if you want to execute your other script, you'll make a command like :
php MyScript.php -s [args]
Otherwise, simply put the standard arguments :
php MyScript.php [args]
could be as simple as:
if(isset($argv[1])){
//commadn line arguments
$foo=$argv[1]
}else{
//not
$foo=$foo
}
I working in Rails and I need to call to an PHP-script.
I can connect to the script like this:
system('php public/myscript.php')
But I need to send some parameters with it.
How do I do that?
Thanks
You can provide command-line arguments to your PHP script:
system('php public/myscript.php arg1 arg2')
They will be available from your PHP code like this:
echo $argv[0]; // public/myscript.php
echo $argv[1]; // arg1
echo $argv[2]; // arg2
You can just specify the parameters on the command line, such as system('php -f public/myscript.php argument1 argument2 [...]') and they will be available in the $argv[] array, starting from $argv[1]. See the doc page here for more info.
Yes its right way to use system('php public/myscript.php arg1 arg2') as SirDarius answered .
but system command will return the true or false in that case.
system() will return TrueClass or FalseClass and display output, try it on console .
I suggest , You can use the open method on any URL to call it, so you can call your PHP script using that:
require 'open-uri'
open('YOUR PHP SCRIPT PATH WITH PARAMETER') do |response|
content = response.read
end