How can I call PHP functions from my C application?

Example:

include <php.h>

int main()
{
  return json_encode(""); // This is a PHP function coming from php.h
}

Note: PHP function collections is very managed and organized, i just want to have that logic in my C application, everything organized on-demand. Thats the beauty i found in PHP language.

Comments

AFAIK this won't be possible or really difficult and lead to many problems. And since the major part of PHP is written in C, there's nothing you can't do in C with a library or language functionnality. Why do you want to do that ? Why not use PHP directly ?

Written by Krtek

Without some crazy bit of hacking you shouldn't be able - PHP extension functions are defined in a very different way than in C. To use them you would need to push in zvals into the PHP functions, provide them with a PHPy-enough enviroment, a.s.o.

Written by NikiC

If you add an explanation of why you want to do this (that is, what you're really trying to do), you'll get a much nicer set of answers. If the "why" is because you're used to php and need to write some C code, the answer is to learn C, or just use PHP.

Written by jeremiahd

There's a book called Extending and Embedding PHP that apparently deals with this. However, it was written in 2006 and could be out of date. Here's a Slashdot review and an Amazon page that may help.

Written by CanSpice

You may also find this blog post informative.

Written by CanSpice

@jeremiahd: PHP function have a managed programming concept/standards like quick push to move fast and fury. In C language a common pattern does not exist (string/socket/etc etc not managed), everything requires to build from day 1. So if i can import PHP standard functions in pure C code i will use PHP functions only, without PHP interpreter involved.

Written by IamSon0fRaja

all my life i ran implementing c functions in php, n u jst surprised me by asking this question, i m really interested in knowing your app demands that forces u to call php from inside of c

Written by Abhinav Singh

@Abhinav Singh: Its very organized language not only the shape but the beauty of functions collection, such as phpjs.org is doing using js. I am willing to have header file which can allow me to do it with pure C without involving PHP interpreter.

Written by IamSon0fRaja

PHP is not "very managed and organized". There are no naming conventions nor parameter order conventions or any conventions of any sort. The PHP standard function library is an ugly mess.

Written by Matti Virkkunen

I wish comments had downvote buttons

Written by Matti Virkkunen

@Matti I agree with the statement, that PHP is a mess. With time it is becoming more and more organized I think, though. Unfortunately, it does not seem they will be fixing mistakes from the past...

Written by Tadeck

Accepted Answer

Since I also want my downvote today, I will try to answer to this question ;)

I don't know if what you're asking is possible. But I'm sure of some other things :

  1. The result will be really complicated, slow and will lead to many problems with dependencies and portability.
  2. You will get much better answer if you try to explain why you're asking this and what are the goals you want to achieve
  3. The major part of PHP is implemented in C, so everything your doing in PHP can be done in C too. Concerning this, here's some reading about Turing Completeness.
  4. Learning the C language instead of wanting to use some known PHP functions will open you new perspectives which will only improve your coding skills.
  5. You should ask you if you really want / have to use C, or if it's better to use some other higher level language.
  6. If you want to decode JSON messages in C, http://www.json.org/ lists plenty of C implementation of the standard you can use. For base64 decoding, see this How do I base64 encode (decode) in C? for example.
Written by Krtek
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki