468,247 Members | 1,317 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

Can PHP be dynamically loaded and called from a C program?

I anticipate wanting PHP to evaluate a lot of expressions and would
rather not create subprocesses to do it from inside my C program.

I've googled around for calling PHP from C, and know Apache does it, but
my C program won't have a full Apache environment. Can the libphp.so be
used otherwise?
Jul 17 '05 #1
7 2319
NC
Thomas Gagne wrote:

I anticipate wanting PHP to evaluate a lot of expressions and would
rather not create subprocesses to do it from inside my C program.

I've googled around for calling PHP from C, and know Apache does it,
but my C program won't have a full Apache environment. Can the
libphp.so be used otherwise?


No, but there is a command-line PHP interpreter that you can
use without Apache or any HTTP server whatsoever.

Cheers,
NC

Jul 17 '05 #2
On Tue, 29 Mar 2005 10:25:20 -0500, Thomas Gagne <tg****@wide-open-west.com>
wrote:
I anticipate wanting PHP to evaluate a lot of expressions and would
rather not create subprocesses to do it from inside my C program.

I've googled around for calling PHP from C, and know Apache does it, but
my C program won't have a full Apache environment. Can the libphp.so be
used otherwise?


Looks like it's possible; although a bit tricky to search for, since most hits
for "embedding php" come up with embedding PHP in HTML in a webserver-parsed
page.

The trail seems to start here:
http://www.zend.com/lists/php-dev/200209/msg01063.html

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #3

"Thomas Gagne" <tg****@wide-open-west.com> wrote in message
news:OY********************@wideopenwest.com...
I anticipate wanting PHP to evaluate a lot of expressions and would
rather not create subprocesses to do it from inside my C program.

I've googled around for calling PHP from C, and know Apache does it, but
my C program won't have a full Apache environment. Can the libphp.so be
used otherwise?


You will never be able to use libphp.so without building Apache into your C
program. The PHP module calls a number of functions within Apache. Unless
you implement your own version of these, the dynamic library will not even
load.

Writing a PHP module that encapsute your C code is the better approach.
Jul 17 '05 #4
NC wrote:
Thomas Gagne wrote:

I anticipate wanting PHP to evaluate a lot of expressions and would
rather not create subprocesses to do it from inside my C program.

I've googled around for calling PHP from C, and know Apache does it,
but my C program won't have a full Apache environment. Can the
libphp.so be used otherwise?


No, but there is a command-line PHP interpreter that you can
use without Apache or any HTTP server whatsoever.


Is there a reason why you (OP) specificaly want to embed PHP? If all you
need is an embedded scripting language, both TCL and python have a long
pedigree in this department.

HTH

C.
Jul 17 '05 #5
On Wed, 30 Mar 2005 02:43:18 -0500, "Chung Leong" <ch***********@hotmail.com>
wrote:
"Thomas Gagne" <tg****@wide-open-west.com> wrote in message
news:OY********************@wideopenwest.com...
I anticipate wanting PHP to evaluate a lot of expressions and would
rather not create subprocesses to do it from inside my C program.

I've googled around for calling PHP from C, and know Apache does it, but
my C program won't have a full Apache environment. Can the libphp.so be
used otherwise?


You will never be able to use libphp.so without building Apache into your C
program. The PHP module calls a number of functions within Apache. Unless
you implement your own version of these, the dynamic library will not even
load.


That's exactly what the embed SAPI is for. Clearly it's possible to load PHP
into an executable without Apache being involved at all - else running PHP
under IIS would be impossible.

The embed SAPI, from the looks of it, fills all the gaps usually provided by a
webserver's API, allowing PHP to be called from arbitrary C programs.

It's woefully underdocumented and from a few minutes playing with it I've not
got it to actually link properly yet, but the principle looks sound.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #6
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:l7********************************@4ax.com...
On Wed, 30 Mar 2005 02:43:18 -0500, "Chung Leong" <ch***********@hotmail.com> wrote:
"Thomas Gagne" <tg****@wide-open-west.com> wrote in message
news:OY********************@wideopenwest.com...
I anticipate wanting PHP to evaluate a lot of expressions and would
rather not create subprocesses to do it from inside my C program.

I've googled around for calling PHP from C, and know Apache does it, but my C program won't have a full Apache environment. Can the libphp.so be used otherwise?
You will never be able to use libphp.so without building Apache into your Cprogram. The PHP module calls a number of functions within Apache. Unless
you implement your own version of these, the dynamic library will not evenload.


That's exactly what the embed SAPI is for. Clearly it's possible to load

PHP into an executable without Apache being involved at all - else running PHP
under IIS would be impossible.

The embed SAPI, from the looks of it, fills all the gaps usually provided by a webserver's API, allowing PHP to be called from arbitrary C programs.

It's woefully underdocumented and from a few minutes playing with it I've not got it to actually link properly yet, but the principle looks sound.


IIRC, libphp.so is the name of the mod_php binary. I could be wrong though.
Jul 17 '05 #7
Colin McKinnon wrote:
NC wrote:

Thomas Gagne wrote:
I anticipate wanting PHP to evaluate a lot of expressions and would
rather not create subprocesses to do it from inside my C program.

I've googled around for calling PHP from C, and know Apache does it,
but my C program won't have a full Apache environment. Can the
libphp.so be used otherwise?


No, but there is a command-line PHP interpreter that you can
use without Apache or any HTTP server whatsoever.

Is there a reason why you (OP) specificaly want to embed PHP? If all you
need is an embedded scripting language, both TCL and python have a long
pedigree in this department.

I've a business-rule engine written in Smalltalk that exists in such a
way that any language could be used to describe a rule. Because we've
PHP programmers on-staff I thought PHP would be a valuable extension.
Since Smalltalk can call C library functions I thought that might be one
way to talk to PHP. So in essence, calling C from inside PHP doesn't
work because I want to get into PHP from the outside.

Thanks for the reminders on Python and TCL. With both being open-source
I could theoretically load these and allow access to Smalltalk objects
from within them. That would be especially cool for Python.
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by cjl | last post: by
4 posts views Thread by Harry | last post: by
3 posts views Thread by supvine | last post: by
12 posts views Thread by Steve_Black | last post: by
7 posts views Thread by Nathan Sokalski | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.