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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Gagne

    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?
  • NC

    #2
    Re: Can PHP be dynamically loaded and called from a C program?

    Thomas Gagne wrote:[color=blue]
    >
    > 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?[/color]

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

    Cheers,
    NC

    Comment

    • Andy Hassall

      #3
      Re: Can PHP be dynamically loaded and called from a C program?

      On Tue, 29 Mar 2005 10:25:20 -0500, Thomas Gagne <tgagne@wide-open-west.com>
      wrote:
      [color=blue]
      >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?[/color]

      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:


      --
      Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
      <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

      Comment

      • Chung Leong

        #4
        Re: Can PHP be dynamically loaded and called from a C program?


        "Thomas Gagne" <tgagne@wide-open-west.com> wrote in message
        news:OY6dned3Q4 TF7tTfRVn-pA@wideopenwest .com...[color=blue]
        > 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?[/color]

        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.


        Comment

        • Colin McKinnon

          #5
          Re: Can PHP be dynamically loaded and called from a C program?

          NC wrote:
          [color=blue]
          > Thomas Gagne wrote:[color=green]
          >>
          >> 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?[/color]
          >
          > No, but there is a command-line PHP interpreter that you can
          > use without Apache or any HTTP server whatsoever.
          >[/color]

          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.

          Comment

          • Andy Hassall

            #6
            Re: Can PHP be dynamically loaded and called from a C program?

            On Wed, 30 Mar 2005 02:43:18 -0500, "Chung Leong" <chernyshevsky@ hotmail.com>
            wrote:
            [color=blue]
            >"Thomas Gagne" <tgagne@wide-open-west.com> wrote in message
            >news:OY6dned3Q 4TF7tTfRVn-pA@wideopenwest .com...[color=green]
            >> 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?[/color]
            >
            >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.[/color]

            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 / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
            <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

            Comment

            • Chung Leong

              #7
              Re: Can PHP be dynamically loaded and called from a C program?

              "Andy Hassall" <andy@andyh.co. uk> wrote in message
              news:l7cm4151m9 6ns5auc5331dncu ojbo44438@4ax.c om...[color=blue]
              > On Wed, 30 Mar 2005 02:43:18 -0500, "Chung Leong"[/color]
              <chernyshevsky@ hotmail.com>[color=blue]
              > wrote:
              >[color=green]
              > >"Thomas Gagne" <tgagne@wide-open-west.com> wrote in message
              > >news:OY6dned3Q 4TF7tTfRVn-pA@wideopenwest .com...[color=darkred]
              > >> 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,[/color][/color][/color]
              but[color=blue][color=green][color=darkred]
              > >> my C program won't have a full Apache environment. Can the libphp.so[/color][/color][/color]
              be[color=blue][color=green][color=darkred]
              > >> used otherwise?[/color]
              > >
              > >You will never be able to use libphp.so without building Apache into your[/color][/color]
              C[color=blue][color=green]
              > >program. The PHP module calls a number of functions within Apache. Unless
              > >you implement your own version of these, the dynamic library will not[/color][/color]
              even[color=blue][color=green]
              > >load.[/color]
              >
              > That's exactly what the embed SAPI is for. Clearly it's possible to load[/color]
              PHP[color=blue]
              > 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[/color]
              by a[color=blue]
              > 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[/color]
              not[color=blue]
              > got it to actually link properly yet, but the principle looks sound.
              >[/color]

              IIRC, libphp.so is the name of the mod_php binary. I could be wrong though.


              Comment

              • Thomas Gagne

                #8
                Re: Can PHP be dynamically loaded and called from a C program?

                Colin McKinnon wrote:[color=blue]
                > NC wrote:
                >
                >[color=green]
                >>Thomas Gagne wrote:
                >>[color=darkred]
                >>>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?[/color]
                >>
                >>No, but there is a command-line PHP interpreter that you can
                >>use without Apache or any HTTP server whatsoever.
                >>[/color]
                >
                >
                > 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.
                >[/color]
                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.

                Comment

                Working...