C++ PHP Embed load php script from string

131 Views Asked by At

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?

0

There are 0 best solutions below