1

I am working on a project to embed PHP in my C++ program. I found the following code. https://github.com/php/php-src/tree/master/sapi/embed

I am struggling to find an example that doesn't use a file handler. I need a function where I can pass the contents of a php script to be executed.

I am not looking for someone to write the code for me, but instead I am wondering if this function exists in the code and I am missing it. I cannot find any documentation on the php embed other than the page I posted and I don't want to have to update php itself to support this.

The following code is what I am starting with, which works fine with an actual file, but I can't think of a good way to do it with a static string (to be replaced later).

#include <sapi/embed/php_embed.h>

int main(int argc, char **argv)
{
    PHP_EMBED_START_BLOCK(argc, argv)

    zend_file_handle file_handle;
    zend_stream_init_filename(&file_handle, "example.php");

    if (php_execute_script(&file_handle) == FAILURE) {
        php_printf("Failed to execute PHP script.\n");
    }

    PHP_EMBED_END_BLOCK()
}

My understanding of Eval is that it is ONLY for the php scripts and not for a full HTML file with embedded php. My C++ is rusty, so I may be missing something really obvious.

Edit: Thinking about the eval I did find information on using something like this pseudo code

zend_eval_stringl(ZEND_STRL(" ?>" + HTMLwithPHP + "<?php "))

I am not sure of what the limitations of the " ?>" and "<?php " notation is. Would this break any scripts?

Krum110487
  • 611
  • 1
  • 7
  • 20
  • Bit of a workaround and terribly inefficient, but you could save the php string to file, say to /var/tmp/ to keep it in RAM. – JPh Mar 21 '23 at 21:45
  • Yeah, I thought about just caching it to a file and loading it, as much as I don't want to. I updated the ticket with an idea of what may work, but I am not sure on the limitations. – Krum110487 Mar 21 '23 at 22:24

0 Answers0