Skip to content

Instantly share code, notes, and snippets.

@paresy
Last active November 26, 2020 04:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paresy/b4babb919a86e9764bc4 to your computer and use it in GitHub Desktop.
Save paresy/b4babb919a86e9764bc4 to your computer and use it in GitHub Desktop.
PHP 7.3 ZTS Embed Example
#include <iostream>
#include <thread>
#include <main/php.h>
#include <main/SAPI.h>
#include <main/php_main.h>
#if defined(ZTS)
ZEND_TSRMLS_CACHE_EXTERN();
#endif
#if defined(PHP_WIN32) && defined(ZTS)
ZEND_TSRMLS_CACHE_DEFINE();
#endif
zend_module_entry ips_module_entry = {
STANDARD_MODULE_HEADER,
"XYZ",
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
static size_t php_embed_read_post(char *str, size_t str_length)
{
return 0;
}
static char* php_embed_read_cookies()
{
return NULL;
}
static size_t php_embed_ub_write(const char *str, size_t str_length)
{
std::cout << str;
return str_length;
}
static void php_embed_flush(void *server_context)
{
//
}
static void php_embed_send_header(sapi_header_struct *sapi_header, void *server_context)
{
//
}
static void php_embed_log_message(char *message, int syslog_type_int)
{
fprintf(stderr, "%s\n", message);
}
static void php_embed_register_variables(zval *track_vars_array)
{
//
}
static int php_embed_startup(sapi_module_struct *sapi_module)
{
if (php_module_startup(sapi_module, NULL, 0) == FAILURE) {
return FAILURE;
}
return SUCCESS;
}
sapi_module_struct php_embed_module = {
(char*)"XYZ", /* name */
(char*)"PHP for XYZ", /* pretty name */
php_embed_startup, /* startup */
php_module_shutdown_wrapper, /* shutdown */
NULL, /* activate */
NULL, /* deactivate */
php_embed_ub_write, /* unbuffered write */
php_embed_flush, /* flush */
NULL, /* get uid */
NULL, /* getenv */
php_error, /* error handler */
NULL, /* header handler */
NULL, /* send headers handler */
php_embed_send_header, /* send header handler */
php_embed_read_post, /* read POST data */
php_embed_read_cookies, /* read Cookies */
php_embed_register_variables, /* register server variables */
php_embed_log_message, /* Log message */
NULL, /* Get request time */
NULL, /* Child terminate */
STANDARD_SAPI_MODULE_PROPERTIES
};
int main(int argc, const char * argv[]) {
tsrm_startup(128, 1, 0, NULL);
zend_signal_startup();
sapi_startup(&php_embed_module);
if (php_embed_module.startup(&php_embed_module) == FAILURE) {
throw std::runtime_error("Could not start PHP!");
}
for(int i = 0; i < 10; i++) {
std::thread([&argv]{
ts_resource(0);
while(true){
zend_file_handle file_handle;
file_handle.type = ZEND_HANDLE_FILENAME;
file_handle.filename = "empty.php";
file_handle.handle.fp = NULL;
file_handle.opened_path = NULL;
file_handle.free_filename = 0;
if (php_request_startup() == FAILURE) {
//std::cout << "ERR" << std::endl;
php_request_shutdown(NULL);
continue;
}
if(php_execute_script(&file_handle) == FAILURE)
break;
//std::cout << "RUN" << std::endl;
php_request_shutdown(NULL);
}
std::cout << "DIED" << std::endl;
}).detach();
}
while(true);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment