burek Posted November 26, 2008 Share Posted November 26, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/ Share on other sites More sharing options...
Mchl Posted November 26, 2008 Share Posted November 26, 2008 Given that you can download PHP source, you could probably do this, but I've never heard of any "ready-to-include" C++ library Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-699435 Share on other sites More sharing options...
corbin Posted November 27, 2008 Share Posted November 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-700135 Share on other sites More sharing options...
Mchl Posted November 27, 2008 Share Posted November 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-700205 Share on other sites More sharing options...
burek Posted November 27, 2008 Author Share Posted November 27, 2008 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.. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-700471 Share on other sites More sharing options...
corbin Posted November 28, 2008 Share Posted November 28, 2008 Ohhhhhh! I thought you wanted it to interact with your program. It should be simple. Either find the hooks into the PHP stuff, or just run a system cmd that executes a PHP binary. First way would be better. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-700837 Share on other sites More sharing options...
Mchl Posted November 28, 2008 Share Posted November 28, 2008 The system qould have to have PHP installed for it then. So it's not 'embedded' into application. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-700849 Share on other sites More sharing options...
corbin Posted November 28, 2008 Share Posted November 28, 2008 Ahh true..... Wasn't thinking embedded anymore. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-700852 Share on other sites More sharing options...
burek Posted November 28, 2008 Author Share Posted November 28, 2008 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).. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-700951 Share on other sites More sharing options...
DarkWater Posted November 28, 2008 Share Posted November 28, 2008 Make your own language then. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-701350 Share on other sites More sharing options...
Mchl Posted November 28, 2008 Share Posted November 28, 2008 What for? I guess there are other scripting languages, that are easy ti embed into C++ application. PHP being web oriented just isn't one of them. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-701352 Share on other sites More sharing options...
burek Posted November 29, 2008 Author Share Posted November 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-701407 Share on other sites More sharing options...
trq Posted November 29, 2008 Share Posted November 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-701417 Share on other sites More sharing options...
corbin Posted November 29, 2008 Share Posted November 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-701519 Share on other sites More sharing options...
Mchl Posted November 29, 2008 Share Posted November 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-701632 Share on other sites More sharing options...
DarkWater Posted November 29, 2008 Share Posted November 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-701727 Share on other sites More sharing options...
corbin Posted November 29, 2008 Share Posted November 29, 2008 Couldn't you compile PHP into a lib and build the app against the lib? I've never actually tried to mix C++ with a C lib though.... Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-701745 Share on other sites More sharing options...
coalgames Posted June 29, 2009 Share Posted June 29, 2009 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++. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-865853 Share on other sites More sharing options...
kenjichanhkg Posted July 2, 2009 Share Posted July 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-867897 Share on other sites More sharing options...
Ian Robertson Posted July 10, 2009 Share Posted July 10, 2009 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++... Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-872780 Share on other sites More sharing options...
Mchl Posted July 10, 2009 Share Posted July 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-872808 Share on other sites More sharing options...
corbin Posted July 10, 2009 Share Posted July 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-872989 Share on other sites More sharing options...
kenjichanhkg Posted July 12, 2009 Share Posted July 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-873934 Share on other sites More sharing options...
burek Posted July 15, 2009 Author Share Posted July 15, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/134344-how-to-embed-php-script-into-c-application/#findComment-875556 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.