How to call a PHP method in Rails Application? - php

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.

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.

link PHP with Octave or Matlab

Suppose I have a lot of math calculations which are quite tedious to implement in php. Is it possible to somehow link PHP and Octave on the server in such a way, that php sends parameters to Octave and receives answers back.
Has anyone tried anything similar?
Another solution is to use octave-daemon, which was written specifically for this purpose. Works on Linux, don't know about Windows.
You can use matlab compiler to make an executable matlab application, that you can call from php.
GNU Octave can be called from PHP using the Linux command line, using commands like exec() or passthru(). Anyway, their appropriate use depends on what you are trying to do (there are no details of your problem).
One way to do it, in Windows, is to compile Matlab as a DLL and include it ona web app (a WFC service, for example). At that point you have a functional "matlab service" and then you can access that service from PHP or any other language.
It is also possible to create a .NET component using Matlab Builder NE, and deploy it using SilverLight on the web.

How to run a command line script through a normal Zend Framework application

I am working on a ZF application that needs to run a command line script and then parse the results into something meaningful and return to the user.
I know there are various PHP functions, like exec and system, but I was wondering if there is anything built into Zend Framework that does command line scripting easily.
Even if there isn't a ZF specific function, what is the best function/method to use for running a command line script and then retrieving the results in PHP upon completion of the script.
I would write a Service which uses exec to get what you need. You can add some basic error handling there.
You can start an process under linux with this:
ZendX_Console_Process_Unix
But never tryed it..
We recently needed to do this (we use ZF too), hope this helps: http://www.kintek.com.au/web-design-blog/how-to-run-a-php-script-from-command-line/

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