Passing js file from php to mongo - php

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.

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.

Calling Python script in PHP post PHP 5.4

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.

PHP: what happens when I call exec() function?

Let's say I want to fetch contents from a URL using a PHP script.
One way to do that would be to use PHP function such as
echo file_get_contents("http://www.example.com/file.xml");
Another way would be to use UNIX tools such as wget or curl, or any other tool accessible from shell
echo exec("wget http://www.example.com/file.xml");
Is there a significant performance difference between using exec() and PHP build-in functions to achieve same thing, assuming that both UNIX tools and PHP functions have similar implementation and perform with same efficiency?
What exactly happens when you call an exec() function in terms of resources? Does it actually create a new shell session, or does it run on top of the current php shell session?
The exec() function creates a new shell instance, with its own environment variables, so there is a performance hit.

How to call a PHP method in Rails Application?

Is there any way to include php file using require and calling a php method in rails application ? I dont want to use phusion passenger as my server .
As far as I know, there is no PHP interpreters for Ruby, so your best bet would be to make an HTTP request from your Rails app to your PHP app to get its output. This of course won't allow you to directly call any specific function or such.
It might also be possible to run your Ruby application using JRuby, and at the same time, use Quercus. Quercus is a Java based PHP interpreter, so you might be able to interact through the JRuby Java interface with the Quercus parsed PHP script.
Disclaimer: Never used Ruby, JRuby or Quercus.
You could just run it through a shell command. There are several ways to do it, but here's one:
`php /path/to/your/script.php`
This will run the PHP script in the argument. If you care about the result of the script:
result = `php /path/to/your/script.php`
Disclaimer: I've never actually tried this so I don't know what it returns, but theoretically it should return whatever the script returns, if anything.

Categories