Receive hash from Perl Script to use in PHP - php

I have a perl script that does a lot of config file parsing for me and creates a hash with all the information I need.
I want to call that script from PHP and have PHP get the hash to be able to work with the hash in php and not just returning some html code from the perl script.
Is that possible? Haven't found any way yet and just know that I am able to return lots of html code as output, but that's not what I want the perl script to do.

The simplest way, serialize this hash into json in perl and print resulting string to STDOUT.
In PHP it can be easily decoded into array or object...

If the platform that is executing the PHP allows for it, you can call the exec() function to execute external files like:
$result = exec( "/path_to/your_script.pl", $lines, $state);

Related

How to run file in background by adding variable

Hey PHP developers I am newbie.
Today I want to run my process.php file in the background because it takes too much time to load... Here is the code that I want to use.
$proc=new BackgroundProcess();
$proc->setCmd('exec php <BASE_PATH>/process.php hello world');
$proc->start();
And I want to add this ids=$postid&reaction=$reaction variable instead of hello world.
And want to receive it with post in process.php file like this
$id =$_POST['ids'];
$type = $_POST['reaction'];
I am using this GitHub file
https://github.com/pandasanjay/php-script-background-processer/blob/master/README.md
Before doing downvote answer me I am a newbie in PHP.
You can try exec() for this. If you want to pass parameters then try like this.
//it will store logs to log_data.log
exec("php process.php $id $type >log_data.log &");
Hope this will work for you :)
Try like this
function execInBackground() {
//this will run in background
exec("php process.php $id $type > /dev/null &");
}
As soon as it is not HTTP request at all, you cannot access $_GET and $_POST superglobals. The right way to receive arguments in this case, is to access the array $argv. See official documentation:
http://php.net/manual/en/reserved.variables.argv.php
UPD: And, well, if you really want to pass $_GET/$_POST params to this script executed via shell, here is a dirty trick:
$get_params_as_string = base64_encode(json_encode($_GET));
$proc=new BackgroundProcess();
$proc->setCmd("exec php <BASE_PATH>/process.php {$get_params_as_string}");
$proc->start();
And in your process.php access it like this:
$get_params = json_decode(base64_decode($argv[1]), true);
So, we are just created JSON from $_GET array. Then, as we know that JSON string contains special characters(like ", {, }, etc), and to avoid dealing with problems of escaping and unescaping, we simply encode this string as base64. It guarantees us absence of special characters in result string. Now we can use this string as a single argument, which we will pass to shell command (your BackgroundProcess). And finally, in process.php we can access this string from $args[1], then decode from base64, then decode from JSON to a regular PHP array. Here we go.
This solution is provided only for educational purpose, please don't ever do it in real life.

php equivalent of python lxlm.etree fromstring

I'm fairly new to python and I'm re-writing a python script that accesses an API.
In the python script, I'm trying to find the equivalient of this:
response = get_resource(url, auth=(username,password), params=params)
return lxml.etree.fromstring(response.content)
returns <http://www.w3.org/2005/Atom} feed at 0x2f12b70>
What is the equivalent of this code in php? And I'm not even sure what this is returning?
Thanks.

How to filter user' s input ( html, with PHP backend) with python?

There is a web application written in PHP and HTML. What I want is to filter a users input for a variety of cases and sanitize it. For example, I want to compare the input from a form (string) with a list of allowed strings and depending if it is right or wrong to trigger the suitable PHP function to handle this.
My question is how to bind the user input with the python script and then the outcome of this python script as an input for PHP?
thanks
You can call the Python script from your PHP file as a shell command, passing it JSON-formatted arguments. Then have the Python script output the response (also JSON encoded) and have the PHP file capture that. Here's an example I used recently, cobbled together from the links below:
PHP file:
$py_input = ... // Your data goes here.
// Call the Python script, passing it the JSON argument, and capturing the result.
$py_output = shell_exec('python script.py ' . escapeshellarg(json_encode($py_input)));
$py_result = json_decode($py_output);
Python file:
import json
php_input = json.loads(sys.argv[1]) # The first command line argument.
# Do your thing.
php_output = ... # Whatever your output is.
print json.dumps(php_output) # Print it out in JSON format.
Passing a Python list to php
executing Python script in PHP and exchanging data between the two

PHP - Syntax of exec() function to call another php file

This question is in reference to:
Free (preferably) PHP RTF to HTML converter?
I'm trying to execute that last line of code in my php:
exec(rtf2htm file.rtf file.html)
I understand what parameters need to go within the parentheses, I just do not know how to write it. I've looked at multiple examples along with the php documentation and still I remain confused, so could someone show me how it is written? rtf2htm refers to a PHP file which converts RTF to HTML.
Ultimately what I am trying to do is convert the content of numerous RTF docs to HTML, maintaining the formatting, while not creating tags such as<head> or <body> which programs like Word or TextEdit generate when converting to HTML.
rtf2htm is not a php script, it is a program installed on the server. exec() is used to call external applications.
EDIT: After looking up this script, it seems that it is indeed a php script. But it has been coded to be usable from the command line only.
This should work:
<?php
exec('php /path/to/rtf2htm /path/to/source.rtf /path/to/output.html');
?>

php `exec` calls python script. how return strings from python?

I use exec function calling python script from PHP script. Python writes to standard output two strings which I need in PHP script. The problem is that in these strings could be end of line characters \n ( so formally there are many lines in output), and according to exec manual array $output will contain
each line in it. What is elegant way to escape \n characters so that $output will contain only two string I want and no post processing of these two string needed?
EDIT: I can change python script.
Print the output in an easily parsable format, such as JSON, and parse it from PHP. For example, instead of:
print foo
print bar
Use something like:
import json
print json.dumps([foo, bar])
You read the JSON output form PHP and decode it using json_decode($output) into the desired array.
There really is nothing you can do about this outside of changing the way the python script outputs.
It is really easy to clean the returned data though.
exec('yourcommand', $array);
array_walk($array, function($value, $key) {
return trim($value);
});

Categories