I want to implement one logic which is written in python, this code will do some searching stuffs, and I have a website done in PHP. can any one tell me whether I can include python script in PHP? if yes , how can I do that ?
Criteria :
Input to the python script will come from php or html [either text or file]. and output of python is directly displayed to the page or through php or store it in mysql and show it through PHP.[Please suggest me the best one in this].
If you have access to exec, you can run the python interpreter. However, that's:
Overkill
Not necessarily wise
A major waste of resources
If your logic is simple, why don't you write it in PHP? Furthermore, if your logic is not simple...why don't you make an API of some sort to access it and favour communication rather than code deduplication?
Related
First of all, I know that you can't convert a PHP file to HTML due to PHPs nature. So what I rather want to do is have a script run through and scrape all the pages and save them down correspondingly (eg. /help/how-to-stackoverflow should become /help/how-to-stackoverflow.html).
The site I want to convert doesn't use dynamic server-side logic, meaning that no matter who you are, the same URL always gives you the same result and there's nothing going on behind the scenes. It also doesn't use parameters and such.
If possible, the process should be easy to integrate into automation (aka be a command to run), however, this is not a requirement.
So, what is the best way to do that without going through and exporting every page manually?
I'd like to build a website that reads data from a database, lets me filter that data with different Frontend dropdown-filters and presents me the filtered data in a table shown on the Webpage.
In addition I'd like to have the possibility to use that data further to store it into some file, or use it in a python script that handles that data further.
Should I build the webpage using PHP or what would you recommend me?
I'm new on this area so I don't know much about website interaction yet, just have some experience writing python scripts until now.
Thank you for advices.
It depends on how familiar you are with PHP or Python. If you are new to both, I recommend you to start with PHP. If you are familiar with Python, I recommend you to use Flask or Django to make your project.
I'm building a Joomla 3 web site but I have the need to customize quite a few pages. I know I can use PHP with Joomla, but is it also possible to use Python with it? Specifically, I'm looking to use CherryPy to write some custom pieces of code but I want them to be displayed in native Joomla pages (not just iFrames). Is this possible?
PHP execution of Python "scripts"
This will work for scripts, which do stuff and return the output, not for CherryPy.
<?php
// execute your Python script from PHP
$command = escapeshellcmd('myPythonScript.py');
$output = shell_exec($command);
echo $output;
// take response content to embed it into the page
?>
PHP to access a Python/CherryPy served website
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
cherrypy.quickstart(HelloWorld())
This starts a http://localhost:8080 and you should see Hello world!.
Now you could access CherryPy's output by accessing it at it's localhost:port.
Not good performance-wise, but works.
<?php
$output = file_get_contents('http://localhost:8080/');
echo $output;
?>
Joomla + Ajax to access a Pyhton/CherryPy served website
An alternative solution would be, to not use PHP to fetch the content, but to do the fetch from client-side. Basically, you would use an Ajax-Request to the CherryPy served website, to fetch it's content and embed it into the dom of the Joomla served page.
// add jQuery Ajax reqeust from your Joomla page to CherryPy
$.ajax({
url: "https://localhost:8080/", // <-- access the 2nd served website
type: 'GET',
success: function(res) {
//console.log(res);
alert(res);
$("#someElement").html(res);
}
});
Probably the most efficient way of constructing pages from different sources (Joomla and CherryPy) is probably using Edge Side Includes esi.
Basically you run a Joomla instance that serves the documents that contain ESI tags. You run a CherryPy instance that serves the pieces you want to put on the location of the esi tags. And you run a reverse proxy that supports ESI like varnish to stitch it all together.
That may look like a lot of moving parts, but it will be responsive like... something that is really responsive. Tune the caching parts and you relieve your Joomla and database from a lot of work.
The exec way from the other answers will work for small python scripts that print something, but will not work for CherryPy.
You can execute it through a php file. Try this:
exec("python/path/your-script.py");
It's possible, but not very efficient.
You can execute a Python script from PHP using the exec() function:
PHP Code:
exec("python /path/to/python-script.py");
There are also a variety of similar PHP functions that can be used to
accomplish the same thing with minor differences to the way input and
output are handled (passthru, system, proc_open, backticks).
The Python script will be executed using its command line interface -
not using CGI (or similar) interface as you would have if the web
server were directly executing the Python script. This means that the
Python script will not have access to information about the HTTP
request - GET/POST values, the client's IP address, the page URL, etc.
You could pass this information from PHP to Python using command line
parameters, a pipe, a temporary file or some other form of
inter-process communication, but you need to pass each piece of
required information explicitly.
The reason this is inefficient is because every call to exec will
spawn a whole new process for the Python script. That's a fairly
expensive operation to do on every HTTP request (this is why servers
like Apache and interfaces like Fast-CGI re-use child processes and
threads instead of creating new ones). Additionally, if you have more
than one call to exec, every single one is going to spawn a new
process.
Extracted from here.
More info : Unable to put Python code to Joomla
Before considering Python
What are you wanting to customize? (perhaps some clever Javascript or a Joomla extension already exists)
Is the Joomla-way not a better solution for your problem, given the fact that you're using Joomla? (change the template, or the view-templates of the modules and component in particular)
i.o.w.: do you understand Joomla enough to know that you need something else? See below:
If Python is still the way to go:
Does your hosting support Python?
Should you reconsider your choice of CMS?
I like your choice of CherryPy.
I am relatively new to web-development and am encountering an issue using some inline PHP code.
The page is a JavaServer Page (.jsp) and I am trying to implement a JFormer form.
When I add my JFormer PHP code to my .jsp page, it just displays as plain-text and refuses to cooperate with me (even when using demo code from the site's documentation). Is this because of some sort of incompatibility between using PHP on a .jsp page?
If that is the case, what are some work-around that I could use? Should I use an iframe?
I need to preserve the use of the .jsp page and would prefer very much to use JFormer, but if I have to I can toss it.
Example of something similar to what I am doing can be found at: http://www.jformer.com/documentation/getting-started/installation/
JSP and PHP are both server-side languages. As such, all scripting in a given file must be processed by the required engines on the server to produce the necessary HTML output.
I suppose it is possible to rig multiple engines inline to process first JSP, then PHP, but that seems cumbersome and error prone.
Instead, consider using an iframe (as you suggested) or load the PHP content via an AJAX call.
PHP is executed by a PHP interpreter and output HTML. JSP is compiled and executed by a Java VM, and output HTML. You can't execute PHP inside JSP code (and vice-versa). It's like if you put Chinese words inside an English speech. Nobody can understand.
I think the point of this is that the examples for jFormer use PHP for the server side logic. If you want to integrate jFormer into your JSP project, learn how to code the equivalent PHP functionality in JSP. You may need to create a Servlet for portions of the logic.
It looks like JFormer requires PHP so you can't make this work on a JSP page easily. You can rewrite the JFormer PHP code in Java/JSP but this may be a lot of work.
The container (like Tomcat) you're using may be able to run PHP scripts as CGI scripts. If you do this you can't easily share session information between PHP and Java. Javascript could be used to accomplish this, but beware of security issues. If you still want to use JSP you could make an iframe that points to the PHP page, as you said.
Here's an article on setting that up for Tomcat:
http://wiki.apache.org/tomcat/UsingPhp
Disclaimer: I don't know JFormer.
The situation is next:
I have php file, which parses a web-page. on that web-page is a phone number and it's digits are mixed with each other. The only way to put each digit on the correct place is to use some JS functions (on the client side). So, when I execute that php file in linux console, it gives me all that I need, except js function's result (no wonder - JavaScript is not a server-side language). So all I see from JS - only a code, that I have written.
The question: can I execute js files via php and how?
Results of a quick google search (terms = javascript engine php)
J4P5 -- not developed since 2005 [BAD](according to its News)
PECL package spidermonkey
a 2008 post by jeresig points to PHPJS but can't see when it was last updated.
I'm sure you'll find many more links on that google search.
Alternatively:
you say that the digits are scrambled and you need to "unscramble" them using js. Can you code that unscrambling logic into a PHP function and just use it? Will sure save you a lot of trouble, but if learning to use js in php is what you're after, then its a whole different story...
Zend tutorial: "Using javascript in PHP with PECL and spidermonkey"?
http://en.wikipedia.org/wiki/Comparison_of_Server-side_JavaScript_solutions
Alternatively, PHP has simple functions for executing other programs and retrieving their output (I used this along with GPG once to create a PHP file manager which could live-encrypt files as you were uploading them and live-decrypt as you were downloading them)
Using those functions along with http://code.google.com/p/v8/ you should be able to interpret any javascript.
Not unless you know someone who's implemented a Javascript engine in PHP.
In other words, probably not.
Without some sort of browser emulation or passing the unparsed js off to a server side implementation of javascript (maybe node.js?), you won't be able to execute it.
However, does the page use the same js function to unscramble the phone number every time? You should be able to read the incorrect digits and shuffle them with PHP.
If you're prepared to do a bit of work building your own JS runtime to work with it, Tim Whitlock has written a javascript tokenizer and parser in pure PHP
node.js is server-side... but full JS :) no PHP in it so I don't it answer your needs...
Anyway, here is an example : a chat in JS both client & server-side : http://chat.nodejs.org/
Plus, not every host allows you to use the v8 engine...
If you have Javascript data objects, and you need to convert them to/from PHP arrays, that's quite easy using PHP's json_encode() and json_decode() functions.
But actually running Javascript code? No. You can't. You might be able to find a JS interpreter written in PHP (a few other answers have pointed a links that may or may not help you here), or more likely execute the JS using a stand-alone JS interpreter on your server which you call out to from PHP. However if the JS code includes references to the browser's DOM (which is highly likely), that's a whole other set of issues which will almost certainly make it impossible.
Given the way you describe the question, I'd say the easiest solution for you would just be to re-implement the JS code as PHP code; it's unlikely that all the work arounds being suggested would be appropriate for what sounds like a fairly simple bit of utility code.