Creating a simple PHP interpreter in Visual C++ - php

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.

Related

Convert a PHP script into a stand-alone windows executable

I want to automate a fairly simple task. For this I have written a small PHP script which I run from the command line using PHP-CLI. Now I want to hand over this script to someone but I do not want to:
give away the source code
ask him to install PHP on the system
Is there a way to create a .exe version of the PHP script. I am really not much worried about de-compilation; I am more worried about asking people to install and configure PHP.
Peachpie
http://www.peachpie.io
https://github.com/iolevel/peachpie
Peachpie is PHP 7 compiler based on Roslyn by Microsoft and drawing from popular Phalanger. It allows PHP to be executed within the .NET/.NETCore by compiling the PHP code to pure MSIL.
Phalanger
http://v4.php-compiler.net/
http://wiki.php-compiler.net/Phalanger_Wiki
https://github.com/devsense/phalanger
Phalanger is a project which was started at Charles University in Prague and was supported by Microsoft. It compiles source code written in the PHP scripting language into CIL (Common Intermediate Language) byte-code. It handles the beginning of a compiling process which is completed by the JIT compiler component of the .NET Framework. It does not address native code generation nor optimization. Its purpose is to compile PHP scripts into .NET assemblies, logical units containing CIL code and meta-data.
Bambalam
https://github.com/xZero707/Bamcompile/
Bambalam PHP EXE Compiler/Embedder is a free command line tool to convert PHP applications to standalone Windows .exe applications. The exe files produced are totally standalone, no need for php dlls etc. The php code is encoded using the Turck MMCache Encode library so it's a perfect solution if you want to distribute your application while protecting your source code. The converter is also suitable for producing .exe files for windowed PHP applications (created using for example the WinBinder library). It's also good for making stand-alone PHP Socket servers/clients (using the php_sockets.dll extension).
It's NOT really a compiler in the sense that it doesn't produce native machine code from PHP sources, but it works!
ZZEE PHPExe
http://www.zzee.com/phpexe/
ZZEE PHPExe compiles PHP, HTML, Javascript, Flash and other web files into Windows GUI exes. You can rapidly develop Windows GUI applications by employing the familiar PHP web paradigm. You can use the same code for online and Windows applications with little or no modification. It is a Commercial product.
phc-win
http://wiki.swiftlytilting.com/Phc-win
The PHP extension bcompiler is used to compile PHP script code into PHP bytecode. This bytecode can be included just like any php file as long as the bcompiler extension is loaded. Once all the bytecode files have been created, a modified Embeder is used to pack all of the project files into the program exe.
Requires
php5ts.dll
php_win32std.dll
php_bcompiler.dll
php-embed.ini
ExeOutput
http://www.exeoutput.com/
Commercial
WinBinder
http://winbinder.org/ - no longer available
(original: https://github.com/stefan-loewe/WinBinder)
(fork: https://github.com/wagy/WinBinder)
WinBinder is an open source extension to PHP, the script programming language. It allows PHP programmers to easily build native Windows applications, producing quick and rewarding results with minimum effort. Even short scripts with a few dozen lines can generate a useful program, thanks to the power and flexibility of PHP.
PHPDesktop
https://github.com/cztomczak/phpdesktop
PHP Desktop is an open source project founded by Czarek Tomczak in 2012 to provide a way for developing native desktop applications using web technologies such as PHP, HTML5, JavaScript & SQLite. This project is more than just a PHP to EXE compiler, it embeds a web-browser (Internet Explorer or Chrome embedded), a Mongoose web-server and a PHP interpreter. The development workflow you are used to remains the same, the step of turning an existing website into a desktop application is basically a matter of copying it to "www/" directory. Using SQLite database is optional, you could embed mysql/postgresql database in application's installer.
PHP Nightrain
https://github.com/kjellberg/nightrain
Using PHP Nightrain you will be able to deploy and run HTML, CSS, JavaScript and PHP web applications as a native desktop application on Windows, Mac and the Linux operating systems. Popular PHP Frameworks (e.g. CakePHP, Laravel, Drupal, etc…) are well supported!
phc-win "fork"
https://github.com/RDashINC/phc-win
A more-or-less forked version of phc-win, it uses the same techniques as phc-win but supports almost all modern PHP versions. (5.3, 5.4, 5.5, 5.6, etc) It also can use Enigma VB to combine the php5ts.dll with your exe, aswell as UPX compress it. Lastly, it has win32std and winbinder compilied statically into PHP.
EDIT
Another option is to use
http://www.appcelerator.com/products/titanium-cross-platform-application-development/
an online compiler that can build executables for a number of different platforms, from a number of different languages including PHP
TideSDK
http://www.tidesdk.org/
TideSDK is actually the renamed Titanium Desktop project. Titanium remained focused on mobile, and abandoned the desktop version, which was taken over by some people who have open sourced it and dubbed it TideSDK.
Generally, TideSDK uses HTML, CSS and JS to render applications, but it supports scripted languages like PHP, as a plug-in module, as well as other scripting languages like Python and Ruby.
I had problems with most of the tools in other answers as they are all very outdated.
If you need a solution that will "just work", pack a bare-bones version of php with your project in a WinRar SFX archive, set it to extract everything to a temporary directory and execute php your_script.php.
To run a basic script, the only files required are php.exe and php5.dll (or php5ts.dll depending on version).
To add extensions, pack them along with a php.ini file:
[PHP]
extension_dir = "."
extension=php_curl.dll
extension=php_xxxx.dll
...
I tried most of solution given in the 1st answer, the only one that worked for me and is non-commercial is php-desktop.
I simply put my php files in the www/ folder, changed the name of .exe and was able to run my php as an exe !!
Also there is a complete documentation, up to date support, windows and linux (and soon mac) compatibility and options can easily be changed.
RapidEXE is exactly for this job:
SoftPedia page
Author's original page
It converts a php project to a standalone exe. I had enough of all other compilers, tried them one by one and they all disappointed me one way or another. Be my guest, feedbacks are always welcome!
Side note: the mechanism behind it is quite similar to the WinRAR SFX approach; extract engine, extract source, then run. It's just faster and easier to work with. One-command compilation, compressed, smart unpack, auto cleanup, easy config, full control of php engine & version; also extensible with minimal effort.
Happy developing!
ExeOutput is also can Turn PHP Websites into Windows Applications and Software
Turn PHP Websites into Windows Applications and Software
Applications made with ExeOutput for PHP run PHP scripts, PHP applications, and PHP websites natively, and do not require a web server, a web browser, or PHP distribution. They are stand-alone and work on any computer with recent Windows versions.
ExeOutput for PHP is a powerful website compiler that works with all of the elements found on modern sites: PHP scripts, JavaScript, HTML, CSS, XML, PDF files, Flash, Flash videos, Silverlight videos, databases, and images. Combining these elements with PHP Runtime and PHP Extensions, ExeOutput for PHP builds an EXE file that contains your complete application.
http://www.exeoutput.com/
My experience in this matter tells me , most of these software work good with small projects .
But what about big projects? e.g: Zend Framework 2 and some things like that.
Some of them need browser to run and this is difficult to tell customer "please type http://localhost/" in your browser address bar !!
I create a simple project to do this : PHPPy
This is not complete way for create stand alone executable file for running php projects but helps you to do this.
I couldn't compile python file with PyInstaller or Py2exe to .exe file , hope you can.
You don't need uniformserver executable files.
The current PHP Nightrain (4.0.0) is written in Python and it uses the
wxPython libraries. So far wxPython has been working well to get PHP
Nightrain where it is today but in order to push PHP Nightrain to its
next level, we are introducing a sibling of PHP Nightrain, the
PHPWebkit!
It's an update to PHP Nightrain.
https://github.com/entrypass/nightrain-ep

Linking PHP with local C++ program

I've been looking into this for several hours, but everything I've looked at seems rather daunting. I've been using PHP for all of the simple stuff on my website so far. I'm doing a lot of statistical work, and I would like to have c++ available for the more intense calculations.
The c++ would be running locally on the same Unix machine as PHP.
Something like the following is what I'm at a loss for how to do:
<?php
//c++ program has a counter initialized to 0
//PHP tells c++ to add 5 to the counter. $incremented is 5
$incremented = increment_in_cpp_and_return(5);
//$incremented_again will be 7
$incremented_again = increment_in_cpp_and_return(2);
?>
Of course, I'm running some monte-carlo simulations and traversing really big trees instead of incrementing numbers, but that's not what's holding me back.
C++ just needs to listen for a number and return another number (maybe some stuff in JSON at most). It is important for the c++ to keep track of its variables between calls.
I've done a lot of reading on TCP, socket programming, etc and I'm just a little doubtful that this as complicated as the examples make it out to be. A lot of things have pointed me to this https://beej.us/guide/bgnet/html/multi/clientserver.html#simpleserver
If it really is more than 100 lines of c++, are there some popular libraries, or is there a simple implementation in another language?
Thanks!
If you only want to access your C++ program from PHP (or use PHP as the web frontend for your C++ code), an alternative to communicating over a socket would be to embed the C++ code into PHP as an extension.
There's a fair amount of boilerplate code associated with it, but most of it is generated for you by the ext_skel script (included in the PHP source).
Most information online about writing PHP extensions relates to using C, see Extending PHP with C++? for a couple of gotchas related to using C++ for this.
If your C++ is executeable, you could open it as a program, pass data to it via STDIN and pass the return value back to PHP via STDOUT. See proc_open­Docs.
Your standard C++ library should offer access to STDIN and STDOUT, so you already have what you need.
I'm a little confused about how the c++ keeps its state between calls. Can proc_open be used to pass via STDIN to a c++ program that is continuously running? I'm worried that it starts a new instance of the c++
You might be looking for a Named pipe­Wikipedia, a form of inter-process communication (see as well: What are named pipes?), that is supported by most operating systems. It's simple I/O (FIFO) and similar compared to STDIN and STDOUT. You can keep your executable in memory while the other processes can send data to it.
Another simple way is to use sockets, those are supported by PHP as well and should be with your C/C++. Sockets will work across different machines, so you can run your (memory/CPU intensive?) own executable on a dedicated server that does only the calculation for example. Just looks what suits better in your case, from the comment I read you're looking for interprocess communication.
posix_mkfifo­Docs (including sample PHP code in user-notes)
Sockets Tutorial - a simple tutorial on using sockets for interprocess communication (Linux Howtos)
Introduction to Named Pipes (Linux Journal)
Introduction to Interprocess Communication Using Named Pipes (Sun Developer Network)
(these are just some resources, you naturally can find more with a little research, I think for both named pipes and sockets you should be able to find source-code examples for your case)
You could try:
exec()
You send the data from PHP as arguments for the C++ written program that will be executed and the program will return the output so you can use it in PHP after the C++ program's execution.
I've been using gSOAP for remote procedure calls between C++ and PHP, and if you're using PHP5, the interaction is made very easy; see http://www.php.net/manual/en/class.soapclient.php

PHP: possible to compile a web-application into a windows application?

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é

PHP script as single binary

I was whatching facebook's development steps and getting codes live , i think they are making their script as a single binary and pushing a single file. We got a portal with alot of files inside which coded with PHP . is there any encoder or compiler can bundle all files in single one ?
While we are pushing our codes to our servers when renaming folder or extracting files and override , it starts to give error spam.
Also any solution or approach to send the webpages alive without getting any error are welcome.
thanks
You probably need PHAR
FaceBook use a tool named HipHop which compiles PHP into a single binary executable.
For Windows, Linux and FreeBSD there's Roadsend PHP Compiler. It truly compiles PHP source code into native binary format. No interpreter is required.
They claim: "Roadsend PHP is a free, open source implementation of the PHP language. It includes a compiler that produces native binaries (no interpreter required). Roadsend Compiler can build online web applications with Fast/CGI, offline web applications with an embedded web server (MicroServer), and console applications."
They say: "Applications compiled with the Roadsend Compiler do not require Zend or any other interpreter to execute. Roadsend is not affiliated with Zend or the PHP Group and does not share (nor is it derived from) the same code base as their PHP implementation. Roadsend PHP is an entirely new implementation of PHP that is syntactically and semantically compatible with PHP Group's PHP 5.2.x and the Zend Engine 2." – Sebastián Grignoli 41 secs ago edit
"How do I know that my application won't be decompiled back to the PHP source file?"
"The compiling of a PHP application with the Roadsend Compiler is much the same as compiling a C file to binary format. The program goes through a process that converts the plain text source into a binary format that is suitable for the processor to execute, and which is extremely hard to decompile back to any sort of high level programming language."

Moving from PHP to Python

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/

Categories