Skip to content

Instantly share code, notes, and snippets.

@paresy
Last active December 21, 2022 01:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paresy/3cbd4c6a469511ac7479aa0e7c42fea7 to your computer and use it in GitHub Desktop.
Save paresy/3cbd4c6a469511ac7479aa0e7c42fea7 to your computer and use it in GitHub Desktop.
PHP Embed Example
#include <stdio.h>
#include <iostream>
#include <thread>
#include <list>
#include <sapi/embed/php_embed.h>
int main(int argc, char* argv[]) {
int threadCount = 5;
int repeatCount = 1;
PHP_EMBED_START_BLOCK(argc, argv)
std::list<std::shared_ptr<std::thread>> threads;
for (int i = 0; i < threadCount; i++) {
threads.push_back(std::make_shared<std::thread>([repeatCount]{
ts_resource(0);
#if defined(PHP_WIN32) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
for (int i = 0; i < repeatCount; i++) {
if (php_request_startup() == FAILURE) {
//std::cout << "ERR" << std::endl;
php_request_shutdown(NULL);
continue;
}
zend_file_handle file_handle;
//This one requires PHP 7.4+ (see history for old version)
zend_stream_init_filename(&file_handle, "test.php");
if (php_execute_script(&file_handle) == FAILURE)
break;
//std::cout << "RUN" << std::endl;
php_request_shutdown(NULL);
}
std::cout << "FINISHED" << std::endl;
ts_free_thread();
}));
}
for(auto& thread: threads) {
thread->join();
}
PHP_EMBED_END_BLOCK()
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment