Calling Python script in PHP post PHP 5.4 - php

Basically I want to call a simple python script from PHP. I have a PHP page that is adding requests from an html page to a Mysql table, and I'd like this PHP page to call a python script which email administrators to my site once the PHP has finished. I'm currently trying to use the command exec('sudo python /var/www/alertAdmins.py'). However, after reading the PHP documentation it seems that exec requires safe mode, which has been removed as of PHP 5.4 to my understanding. Is there an alternative function I can use? Any workarounds?

No, exec() does not require Safe Mode.
It used to have additional restrictions when Safe Mode was on, though.

Related

Call python script from PHP - parallel requests problem

I call Python script via exec blocking function inside php file which is served by Nginx. It works as expected. Python script executes Selenium using webdriver Chrome and returns xml output.
The problem is when I create more requests (2 and more) in the same time. First request is processed (nginx, php, python) and the second waits for finish of the first. They are not parallel executed.
Python script executed directly from shell can be parallel executed without problems.
You have two option.
Multi-Threading
PHP does not support threads natively, but there is this solution. It is still experimental and it requires PHP 7.2+, but if you are already using PHP 7.2+ it will work like a charm.
Multi-Processing
You can make new processes by forking. You can read more about forking in PHP in this answer and more about it as a concept here.
Or you can also do it in the current way with exec(), but you have to put & at the end of the command, so the PHP might pass it to the console and it can run in the background and run the 2nd one.
There is a third way using MQ Engines, but I cannot help with that, but you can read about it too if you are interested. Hope something of these helps.
PHP isn't directly asyncronious, use the pcntl extencion and look at this

Calling C program with PHP

I’ve written a small database engine in C that works by reading commands you input in the console and it outputs the result.
Is there any way of me writting some PHP code that could send arguments to the console and recieve the output back to PHP without restarting the compiled program.
Is there a better way of doing this?
You say you want the PHP to send and receive messages to your program without restarting the compiled program.
So I don't think using shell_exec or proc_open will work how you want, since these commands both load a fresh instance of the compiled program.
Instead, I suggest you look into sockets, and how you would rewrite your database engine to use those instead of STDIN/STDOUT. Then you can use PHP's socket functions to communicate between your applications. And you'll have just one instance of your compiled program running in the background, even with multiple hits to your PHP script.

Passing js file from php to mongo

I have complex js file that do some heavy calculation and save result to collection.
This written in js because i wan't to avid data transmission.
currently i pass script to mongo shell in this way :
$mongodb < path_to_script
The script consists of several functions?
Is it possible to execute it from PHP?
I saw there is 'nolock' parameter that can be pass to evel method, is it possible to use it when executing from shell or from PHP?
What is consider more safe , using php execute wrapper or executing script from shell ?
When you run a .js file in the mongo shell, it's not running the javascript on the server; it's running it in the shell. Are you using server-side javascript features like db.eval and map/reduce in the mongo shell script?
Either way, I'd suggest forgoing the shell script and server-side javascript functions and either using the aggregation framework for server-side processing or implementing the logic in PHP application code. Server-side javascript has serious performance and security limitations and it's best to avoid using it when possible.
You can read the js file and execute it in PHP using execute which is a wrapper to the eval command. The nolock option can be added in the arguments.
$code = file_get_contents('filenam.js');
$db->execute($code, array('nolock' => true));
The php execute wrapper should be safer and better solution. If you execute js using shell in PHP, you will need to give permission to php to execute shell script using shell_exec(), which is impossible if you use hosting service and don't have admin right on the host.

Is there any way to communicate from Perl to PHP using internal redirect and apache_notes?

My main app is written using PHP, but I'm using a Perl script to process a request, and I want to pass information to the PHP app using apache_notes. I would prefer not to use query parameters.
Here's the documentation on apache notes I found
for PHP
for Perl
On the PHP documentation, there is an example of how to use apache notes from PHP->Perl. Does anyone have an example of how to go from Perl->PHP, or suggest another way to securely communicate from Perl to PHP without having to go through query parameters?
You can use $r->subprocess_env("foo", $value), which from PHP will be visible as $_SERVER["REDIRECT_foo"].

Is it possible to use Python with php

I have a VPS Linux webserver with PHP installed.
I want to code some search engine or data mining stuff in one application which I am thinking of building in python.
I want to know if it is possible to use python and php together like calling functions of python in php and vice versa.
As it is my VPS server, I can install anything on that.
Has anyone tried using python in php? Are there any performance issues in real time??
You can execute python scripts using the exec() function in your php script.
Also this seems to provide an answer or two to your question.
Calling Python in PHP
You could have a look at PiP
To that end, I've [site author] written a Python extension for PHP. In short, this extensions allows the Python interpretter to be embedded inside of PHP (think of PHP as the parent language with Python as its child). This allows native Python objects to be instantiated and manipulated from within PHP. There is also initial support for accessing PHP functions and data from within the embedded Python environment.
However, I cannot comment on its reliability. Might need to test it for yourself.
You're trying to do something like
def helloWorld():
print 'Hello, World'
<?php helloWorld(); ?>
I'd say that you most certainly can't.
Edit: Have a look at php's shell_exec though.
I think that should be
<?php
$try = exec("/var/htdocs/folder/test.py")
?>
Try and see if it works
Better you have to do, use api, different application like one application on python server having python application and one server having php application with api. Like you store or delete data by using api in android or ios.

Categories