Jump to content

How to embed php script into c++ application?


burek

Recommended Posts

Hi,

 

A simple question. If I have one application, written in c++, is there any way I can implement a php scripting support, so the users of that application can write scripts in php, that my application can understand and execute.

 

So, shortly, my goal is to embed php scripts into my application (something like python/perl/lua scripting support, but with php).

 

Is this even possible?

If it is, please give me as much links and informations possible, because google didn't help a lot.

 

Thanks a lot whoever helps on this.

burek

Link to comment
Share on other sites

To expand on Mchl's response, you could probably build PHP into a library or something, and then make calls to it inside of your application feeding it files.  Getting it to react with components of your C++ app would be a bitch though.  Like, I wouldn't even consider trying level hard.  You would have to mod the PHP source code to be aware of and be able to interface with your project.  Then you would need to compile it as a library and make calls to the parsing functions of it from within your App.

 

Maybe I'm making it harder than it is, but that's the only way I can think of doing it without writing a new PHP parser from scratch ;p.

Link to comment
Share on other sites

Maybe I'm making it harder than it is, but that's the only way I can think of doing it without writing a new PHP parser from scratch ;p.

 

Actually I know a project, where they did just that (writing a parser from scratch) to give the users possibility to use basic syntax of scripting language (not PHP, but they had it in plans as well last time I checked). No advanced functions though.

Link to comment
Share on other sites

I don't think it's a good idea to write a parser from scratch, if we already have the php parser that works excellent. I was just trying to make something like php scripts (like python/perl/lua..) so, when a user writes any script in php, my application can execute that script, passing parameters to it and taking the results after execution..

 

I believe this shouldn't be so hard to implement, but, I didn't see any obvious examples on the net, so that's basically why I'm asking for a help..

Link to comment
Share on other sites

Dark Water, thanks, but you're not helping..

 

Mchl, actually, php is not just "web oriented".. Its use is far wider than just a web scripting language, but surely, web scripting is, by far, the most common use..

 

This is a quote from php help:

 

There are three main areas where PHP scripts are used.

  • Server-side scripting. This is the most traditional and main target field for PHP. You need three things to make this work. The PHP parser (CGI or server module), a web server and a web browser. You need to run the web server, with a connected PHP installation. You can access the PHP program output with a web browser, viewing the PHP page through the server. All these can run on your home machine if you are just experimenting with PHP programming. See the installation instructions section for more information.
  • Command line scripting. You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. This type of usage is ideal for scripts regularly executed using cron (on *nix or Linux) or Task Scheduler (on Windows). These scripts can also be used for simple text processing tasks. See the section about Command line usage of PHP for more information.
  • Writing desktop applications. PHP is probably not the very best language to create a desktop application with a graphical user interface, but if you know PHP very well, and would like to use some advanced PHP features in your client-side applications you can also use PHP-GTK to write such programs. You also have the ability to write cross-platform applications this way. PHP-GTK is an extension to PHP, not available in the main distribution. If you are interested in PHP-GTK, visit » its own website.

 

Why am I choosing php to be my scripting engine?

- It's the most popular scripting language;

- It's easy to learn, because of its similarity to c programming language;

- It's open source and has a great parsing engine implemented;

- etc.

 

Also, thanks for helping me on this so far, I do appreciate it.

Link to comment
Share on other sites

I'm not a C or C++ programmer so I'm not sure how you would go about embedding php into a C++ application. I'm sure however it can be done, I'm just not sure how well documented it would be.

 

Python on the other hand has allot of official documentation related to extending and embedding. For that reason alone it may be a better choice. It is also popular, easy to learn and extremely powerful.

 

Just my 2 cents.

Link to comment
Share on other sites

Maybe my english is bad, but I would like users to be able to write scripts for my application (something like writting scripts for mIRC, which then interprets and executes them)..

 

 

mIRC uses a custom scripting languages....

 

I personally don't think it will be possible to have a PHP script interact with your script with out writing some sort of secondary parser (or extending PHP) anyway.

Link to comment
Share on other sites

I don't care what PHP marketing department says. PHP roots are in web, most PHP extensions are web related, and of course web is the most popular use. I know you can use it in different areas (hell, I've been modelling an artificial neural network in PHP once), but in my opinion PHP should stay in web domain.

 

I'd advise you to try Python. Might not be as popular, but is catching up fast.

Link to comment
Share on other sites

I don't think that's it's even close to possible to embed PHP into C++ without a complete rewrite because PHP is entirely written in C, and Zend has a billion and two macros for memory management written in, you guess it, C.  You'd have a hell of a time getting it to work in C++.  And my suggestion of writing your own language still stands; it's not too hard if you know what you're doing.

Link to comment
Share on other sites

  • 7 months later...

I have been thinking about this for quite a while.

 

You first edit the php parser in that you add another function the connects with the main c++ code to return a string or something to edit the behavior of the c++ code. For example, one could make another function that operates like a header file (Move: someitem x,y).

 

Then embed the php cli into the c++ application and call it using c++.

Link to comment
Share on other sites

Hey, I am actually using php in my c++ program.

Well, I won't have much time at the moment. But I want to say it just works for me.

 

You should download the source and have a look at the php_embed.h and php_embed.c

if you download the win32 php zip package, you should have the php_embed.lib that you can use it without compiling php by yourself.

 

For me, I did some tidy modification to php_embed.h or .c to get it working correctly.

 

Try it yourself before you believe something else.

 

http://phi.lv/?p=376  <- this is good

http://books.slashdot.org/article.pl?sid=06/07/31/1416207&from=rss

http://somabo.de/talks/200311_php_conference_frankfurt_php5_oo_part_1.pdf

http://somabo.de/talks/200311_php_conference_frankfurt_php5_oo_part_2.pdf

 

 

Link to comment
Share on other sites

This seems like a pretty simple problem to me, but maybe I am missing something. I would just use C++'s file management functions to create the script and save to disk, then externally run the script through php command line using C++ system(), then dump the results of the php script into a file, and read the file contents with C++...

Link to comment
Share on other sites

But then you must make sure, that the PHP is available on the computer the program is running (and it is correct version with correct extensions, etc).  The point of embedding is to not to have to worry about such things.

Link to comment
Share on other sites

If it were done the way kenjichanhkg mentioned, then it would already be bundled since it would be statically linked.

 

 

 

 

But yeah, Ian's way would require PHP be sent with the program.  Also, it would probably be slower.

Link to comment
Share on other sites

Well, I thought he wanted to embed php interpreter into his program, didn't he?

 

It really depends on what he needs, but what I understand isn't just execute some php scripts. Instead, he wanted to be able to make parts of his program "scriptable", eg.g like VBA in MS office, that exposes itself to VB.

 

Another simple example is that if you want the user be able to provide a formula for some calculation. Do you still want to save the formula in some php script, execute it with php and then somehow reading back the result?

 

Hey burek! Say something!

 

"PHP is for web"

Um, I know many people agree with this statement, But that is not what I see. I am not just believing, coz my company is using PHP for a lot of stuffs. And I'm not a web developer, nor using php to write web pages very often. What I see PHP is a tool, use it for the right purpose.

 

 

Link to comment
Share on other sites

Thanks a lot !! :)

I think that's what I've been looking for :) its just I didn't have notifications about new posts on this topic, so I finally had some spare time to google again on this topic and here I am again :)

I'm gonna check all the links and try to see if that is what I need :)

Once again, thanks to everyone for your help.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.