Currently I do all of my web-based programming in PHP, and each day I get more and more anxious to try Python.
Not that I haven't played with it in an interpreter, but I mean really, write a web-based project in Python, and possibly move pretty much exclusively over to Python.
But, I know that Python isn't strictly a web-based programming language (which is awesome) which makes me wonder if there are any hoops to jump through to use it on the web.
A simple example would be, that I'm sure there's no $_POST like variables in Python by default. How does functionality like that find its way into Python?
How do I move from PHP to Python smoothly? How do I use Python on the web?
Here are some other web frameworks for your reference.
Python is unlike PHP executed by a CGI like interface. This interface has an API to such variables.
Frameworks like Django incorporate that API. It's recommended to use such a framework which makes a lot of things easier.
Have a look at django web framework.
Many web frameworks exist for Python, such as Django, werkzeug, etc. If you want to get "closer to the metal" then look into WSGI.
There is nothing wrong with PHP, except that it's not Python.
So it's not Pythonic!
If you often have to go between the Python world and the PHP world
(WordPress, Drupal, or other PHP framework), you can feel my pains for
not being able to use tons and tons of native Python libraries with ease,
such as SQLAlchemy, Machine Learning,
Deep Learning such as TensorFlow, etc.
PHP frameworks such as WordPress do offer xml-rpc, wp-api, wp-cli,
and other APIs to interface with Python and other languages.
However, preparing Python programs for such APIs and having PHP to
interface with the API on the other end is error prone, not robust,
costly to API client and Web server in terms of CPU and Memory just
for the sake of packing, transmitting, and unpacking for the API,
hard to troubleshoot on both ends of the API, and not scalable.
Hence, enterprises, web, or big-data applications cannot rely on those APIs
for high-speed and high-volume Web applications and large scale data sets.
So comes pyx.php to PHP-to-Python programmers' rescue! (I wrote this.)
Why convert from PHP to Python?
If you ask this question, you probably shouldn't use pyx.php.
pyx.php is a Cython compiled module that you can use to convert or
translate 99% of most common PHP source codes to pure Python.
In another words, it is a PHP-to-Python syntax emulation library in Cython.
The converted Python codes only require a Python 3.x interpreter,
the modules in the pyx repository, and some standard Python libraries.
PHP interpreter is not needed at all.
If we have already translated most of the WordPress core and other
scripts from PHP to Python using pyx.php, you can convert almost
any PHP code into Python.
With the speed of compiled Cython, running Python code translated from PHP
using pyx.php might be even faster than running the original PHP code in
the same computer.
Installation
Download from your browser, or from Linux shell:
$ wget https://wordpy.com/pyx/pyx.tgz
$ tar xvfpz pyx.tgz
Alternatively, you can:
$ git clone https://github.com/wordpy/pyx/
Currently, pyx.php is only available for Python 3.x running 64-bit Linux.
Python 2.x, Mac, or other platforms can be compiled when there are many
requests.
Quick Start
$ python # or ipython
>>> import pyx.php as Php; array = Php.array
>>> arr1 = array( (0,'1-0'),('a','1-a'),('b','1-b'),)
>>> arr2 = array( (0,'2-0'),( 1,'2-1'),('b','2-b'),('c','2-c'),)
>>> arr1 + arr2 # same as: Php.array_plus(arr1, arr2), see below
>>> Php.array_merge(arr1, arr2)
For more, please see https://wordpy.com/pyx/php/
Related
My LAMP server is CentOS 7.4 with Apache 2.4, PHP 5.4, and Python 3.6.
I am new to Python; I migrated from R to Python just now. I need some Python package to do statistics, and then deliver the output to PHP.
I reviewed lots of similar questions. The answers are around exec(), passthru(), system(), and shell_exec(). They are dangerous commands and should not be enabled in PHP.
In the Python official manual, "Integrating Python With Other Languages", mentioned are only two tools, ppython and PHP "Serialize" in Python.
ppython seemed no longer maintained, but that's what I need, just like Rserve when I use R.
I also read this post:
Simple and standard solution is using Socket or Webservice(API)
Now, how do I run a Python script in PHP without using exec(),system()...(maybe socket communication)?
Everything is dangerous (even a fork) if you don't know how to use it. Well, you have several options:
Standard: Running the Python interpreter in PHP with exec() / shell_exec(), etc. Plus there will be a small latency and ability to run Python compiled byte-code, so performance wins here.
Non-standard: If you are concerned a lot about security issues at hand I suggest better to insert Python commands into some batch table and run these regularly with the CRON scheduler. After execution, fetch results with PHP. In this way PHP / Python execution will be de-coupled and you will have a better control on how / when to execute Python scripts.
Non-standard (avoid at all costs): Your mentioned project is moved to Git at php-python. It simply starts a new Python server on port 21230 and waits for Python commands from a PHP scripts. Now, THESE solutions are a most dangerous one, because of the additional opened port in the web server, which is a big headache to administrators and thus highly not recommended.
The last option is to question an assumption that Python is needed at all in web development of PHP. The more different languages in the company IT farm - the harder it will be to maintain all sources and harder to beat time-to-market of new features / bugs fixing. So before considering execution of Python script(s), at first think about re-writing them to plain PHP.
You can do it automatically, but these type of translators are very error-prone and incomplete - for example this one doesn't supports imports. (What the hell? Python without imports is like a bread without a flour). The second option is to learn Python and re-write code at hand into PHP. Or simply get a customer requirements and code these into PHP. Everything that can be done in Python, can be done in PHP too (at least in web development perspective).
Convert your Python script to the Django REST API, and then call it using cURL.
When I look through the source code files I have downloaded from the PHP website, I get lost so easy. What I would like to do is obtain all the source code needed to create a Console application that takes your PHP code and executes it. I would also like to maybe be able to add C++ functions and calll them from PHP. I have done this quite simply with Lua on my Mac via Xcode, but I don't think it will be that easy using PHP on a different system using a different program.
You can write PHP extensions using C (or C++, I suppose) to expose libraries to PHP, but PHP isn't designed to be embedded in applications other than web servers.
Little 2019 update about #duskwuff comment.
PHP8 will have JIT compiler, so, no, PHP will no longer be used only for web.
As you can see on image, PHP8 compiler is faster than Java, C compiler etc.
I have developed a PHP web application, but a client insists on a real Windows application, since he doesn't like running the software inside a browser.
Are there any solutions for this, any compilers to turn a web project into a Windows exe ?
I have looked at Phc-Win , but that seems more suited for small command line utils, not for entire web-applications...
UPDATE:
just found this myself, both look quite promising...
http://www.zzee.com/phpexe/
http://www.exeoutput.com/index.php
There's no tool for this, short of a simple wrapper app that embeds a browser inside an otherwise normal application window. Your PHP app would have to be completely re-written to include ALL of the overhead code necessary to build a GUI - basically all the 'display' stuff that a browser does automatically, would have to added to your app.
Well some of you did not google good enough:
http://www.appcelerator.com/products/titanium-cross-platform-application-development/
Supports most of the "web-languages" to write native Applications.
Tutorial Reference for Appcelerator: http://appceleratortitanium.com/tutorials/3-appcelerator-titanium-tutorial-beginners.html
Quote:
"Q: What languages can I use to make desktop apps?
A: Javascript, PHP, Python, Ruby, HTML, HTML5, and CSS."
Not exactly what you are looking for: http://www.roadsend.co/home/index.php?pageID=compiler
You may look into "HipHop" (made by Facebook): Converts PHP to compileable C++-
There's a tool for this. :-) I never used it but you can try this: Winbinder.
It is simple to compile your PHP source code into an executable. Facebook released a compiler for PHP in early-2010, called HipHop, which aims to create C++. You could then compile this code, for example using gcc, to machine-code.
The more difficult point for a complex Web-App like yours is the user interface. When compiling the way I described above, the application can be run from command line - this might work for simple tasks, but not, if your application returns HTML.
One possibility to solve this problem is PHP-GTK. PHP-GTK is a API for GTK (the graphical user interface used by Linux Ubuntu by default), written in PHP. Using this solution would have to read some documentation about this API, and you would need to rewrite some parts of your program, but it would probably be the most beautiful solution, because it would create a "native" experience.
Another possible solution could involve Adobe AIR, which lets your create programs for the desktop, using HTML, CSS and JavaScript, but I don't know if and how this would work together with your compiled PHP.
Please also note that it isn't absolutely necessary to compile your PHP for it to run on the client's computer; You could also ship the PHP-interperter right with your (uncompiled) PHP-script. Of course, compiling brings benefits, such as faster execution of the program.
-- André
I was search for a video platform which I can use as a web-service and install locally on my local server to my PHP website.
I know we can run PHP shell commands but I need to implement a solid system with the Transcoder.
So, it should provide a API using REST or SOAP to convert videos, convert videos in efficient way. Create video thumbnails. If it a ruby or Python then it would be great and should be a free and open source one.
I could see those Software on Github, seems its good but having limitations. https://github.com/streamio/streamio-ffmpeg.
Is this a bad idea to use Perl or Python written application using with PHP? what are the available FOS software
Is this a bad idea to use Perl or Python written application using with PHP?
A bad idea? No. But it can be annoying sometimes.
If you really want to use a web service for this instead of just calling exec/proc_open like the rest of the world, you can go ahead and do that without too much concern.
Another option available to you is Gearman a distributed work queue/RPC service with bindings for many languages, including PHP, Python and Ruby. You can easily write a daemon in Ruby that uses that ffmpeg wrapper, and call methods directly from your PHP code. Thanks to how Gearman works, you can easily scale out the video encoding services by just adding more workers.
You can use FFMPEG binary for conversion/thumb extraction with command line like exec() in PHP
Is it possible to access & control external devices in PHP?
For example, would it be capable of changing the speed of a USB fan or change the direction of a wireless toy car.
I'm trying to go beyond web dev and I'm wondering if the programming language I use is capable of handling my ideas or should I consider changing the environment.
If these would be possible, I'd very much appreciate any pointers to reading materials or suggestions on other languages that might be more suitable.
Thanks!
On Linux it surely is possible by accessing /dev/ files. But it'll be very tedious. I'd recommend you switching to Python, Ruby, Lua or Java.
For example there are bindings for libusb for Python, Ruby, Lua and Java.
You could write an external program, then use PHP's exec (or was it system?) function to interact with the executable or script.
Seems like the most sane way to do it. Another good alternative is to build a program or script that controls an external device that can communicate with a RESTfull type API exposed via HTTP - and then use lib_curl in PHP land to talk back and forth between it. Believe me, building a basic HTTP server in C++ that can be used to be remote controlled with PHP (or JS for that matter) is very simple.
Wait
I think I read the question wrong ;)
If you want to get into really cool stuff, I say that you learn C++. C++ is a great language that not only opens a lot of doors, but also provides a good learning experience. C++ is lots and lots of fun.
In response to comment
In the case with USB its a bit different and more complicated (as USB has an established protocol and such) but serial is as easy as dumping data into a handle.
You should be able to pick up C++ to get to that point fairly soon. Either way it's a great experience.
This would be possible with a extension, but not with pure PHP code. I don't know of any extensions being able to do something like this, but I think it should be possible.
If you find a command line tool that does this, then you can control it using exec(), system(), passthru() or shell_exec() (based on what output the program gives you back)
Just be sure to escapeshellcmd() if you give access to this program from a public website.