Rails, PHP and parameters - php

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

Related

exec hello.pl file in php by sending variable

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.

Define variable in command line

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.

How to get params with shell_exec() in php

I have two php files.
first one execute the second one
I want to send a params with the exec
I have no idea how to get the params i sent in the second file
first.php:
$params="hello";
shell_exec('php file.php $params > /dev/null 2>/dev/null &')
file.php:
echo $params;
Thanks!
Use the $argv array
In your case
echo $argv[1];
$argv contains any arguments passed to the script.
See http://php.net/manual/en/reserved.variables.argv.php
Try echo $argv[1]
Take a look at this page from the documentation

Cronjob issue. Doesn't recognize variable

Works:
php -q /home/site/public_html/cron/file.php
Doesn't work:
php -q /home/site/public_html/cron/file.php?variable=1
Any suggestions? I need to send the variable as $_GET (or not)
do it something like this
curl http://hostname/cron/file.php?variable=1
and in the file.php you will be managing the code to get the $_GET[variable]
this woould behave as a simple browser call but only in your shell/terminal
Hope this helps
Command Line arguments are passed in $argv instead of the normal $_GET/$_POST-Arrays
Of course this does not work with URI-style parameters (that ?variable=1-part). So you have to call it like: php -q /path/to/script.php 1.
As an alternative you could use getopt:
<?php
$shortopts = implode("", array(
"v:"
));
$longopts = array(
"variable:", // Required value
);
$options = getopt($shortopts, $longopts);
var_dump($options);
And call it like php -q /path/to/script.php --variable=1.
The easiest way to work around this (assuming public_html, is, well, public WWW), is to have cron call wget or curl to access the PHP file, so URL variables are processed as normal.
-q means no head, so there is no space for the get-fields i assume, at least i hope so :D
Greetz

Calling Python in PHP

I have a Python script I recently wrote that I call using the command line with some options. I now want a very thin web interface to call this script locally on my Mac.
I don't want to go through the minor trouble of installing mod_python or mod_wsgi on my Mac, so I was just going to do a system() or popen() from PHP to call the Python script.
Any better ideas?
Depending on what you are doing, system() or popen() may be perfect. Use system() if the Python script has no output, or if you want the Python script's output to go directly to the browser. Use popen() if you want to write data to the Python script's standard input, or read data from the Python script's standard output in php. popen() will only let you read or write, but not both. If you want both, check out proc_open(), but with two way communication between programs you need to be careful to avoid deadlocks, where each program is waiting for the other to do something.
If you want to pass user supplied data to the Python script, then the big thing to be careful about is command injection. If you aren't careful, your user could send you data like "; evilcommand ;" and make your program execute arbitrary commands against your will.
escapeshellarg() and escapeshellcmd() can help with this, but personally I like to remove everything that isn't a known good character, using something like
preg_replace('/[^a-zA-Z0-9]/', '', $str)
The shell_exec() operator will also allow you to run python scripts using similar syntax to above
In a python file called python.py:
hello = "hello"
world = "world"
print hello + " " + world
In a php file called python.php:
$python = shell_exec(python python.py);
echo $python;
You can run a python script via php, and outputs on browser.
Basically you have to call the python script this way:
$command = "python /path/to/python_script.py 2>&1";
$pid = popen( $command,"r");
while( !feof( $pid ) )
{
echo fread($pid, 256);
flush();
ob_flush();
usleep(100000);
}
pclose($pid);
Note: if you run any time.sleep() in you python code, it will not outputs the results on browser.
For full codes working, visit How to execute python script from php and show output on browser
I do this kind of thing all the time for quick-and-dirty scripts. It's quite common to have a CGI or PHP script that just uses system/popen to call some external program.
Just be extra careful if your web server is open to the internet at large. Be sure to sanitize your GET/POST input in this case so as to not allow attackers to run arbitrary commands on your machine.
Your call_python_file.php should look like this:
<?php
$item='Everything is awesome!!';
$tmp = exec("py.py $item");
echo $tmp;
?>
This executes the python script and outputs the result to the browser.
While in your python script the (sys.argv[1:]) variable will bring in all your arguments. To display the argv as a string for wherever your php is pulling from so if you want to do a text area:
import sys
list1 = ' '.join(sys.argv[1:])
def main():
print list1
if __name__ == '__main__':
main()
The above methods seems to be complex. Use my method as a reference.
I have this two files
run.php
mkdir.py
Here, I've created a html page which contains GO button. Whenever you press this button a new folder will be created in directory whose path you have mentioned.
run.php
<html>
<body>
<head>
<title>
run
</title>
</head>
<form method="post">
<input type="submit" value="GO" name="GO">
</form>
</body>
</html>
<?php
if(isset($_POST['GO']))
{
shell_exec("python /var/www/html/lab/mkdir.py");
echo"success";
}
?>
mkdir.py
#!/usr/bin/env python
import os
os.makedirs("thisfolder");
Note that if you are using a virtual environment (as in shared hosting) then you must adjust your path to python, e.g: /home/user/mypython/bin/python ./cgi-bin/test.py
is so easy 😁
You can use [phpy - library for php][1]
php file
<?php
require_once "vendor/autoload.php";
use app\core\App;
$app = new App();
$python = $app->python;
$output = $python->set(your python path)->send(data..)->gen();
var_dump($ouput);
python file:
import include.library.phpy as phpy
print(phpy.get_data(number of data , first = 1 , two =2 ...))
you can see also example in github page
[1]: https://github.com/Raeen123/phpy
If you want to execute your Python script in PHP, it's necessary to do this command in your php script:
exec('your script python.py')

Categories