|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <signal.h> |
|
#include <strings.h> |
|
#include <sapi/embed/php_embed.h> |
|
|
|
int execute_php_code(const char *code, const char *description) { |
|
printf("実行中: %s\n", description); |
|
|
|
int result = 0; |
|
zend_first_try { |
|
if (zend_eval_string((char*)code, NULL, description) == SUCCESS) { |
|
printf("✓ %s の実行が成功しました\n", description); |
|
result = 1; |
|
} else { |
|
printf("✗ %s の実行に失敗しました\n", description); |
|
if (PG(last_error_message) && ZSTR_LEN(PG(last_error_message)) > 0) { |
|
printf(" エラー: %s\n", ZSTR_VAL(PG(last_error_message))); |
|
} |
|
} |
|
} zend_catch { |
|
printf("✗ %s で例外が発生しました\n", description); |
|
result = 0; |
|
} zend_end_try(); |
|
|
|
return result; |
|
} |
|
|
|
int execute_php_file(const char *filename) { |
|
printf("ファイル実行中: %s\n", filename); |
|
|
|
int result = 0; |
|
zend_file_handle file_handle; |
|
zend_stream_init_filename(&file_handle, filename); |
|
|
|
zend_first_try { |
|
if (zend_execute_scripts(ZEND_REQUIRE, NULL, 1, &file_handle) == SUCCESS) { |
|
printf("✓ ファイル %s の実行が成功しました\n", filename); |
|
result = 1; |
|
} else { |
|
printf("✗ ファイル %s の実行に失敗しました\n", filename); |
|
} |
|
} zend_catch { |
|
printf("✗ ファイル %s の実行で例外が発生しました\n", filename); |
|
result = 0; |
|
} zend_end_try(); |
|
|
|
zend_destroy_file_handle(&file_handle); |
|
return result; |
|
} |
|
|
|
int main(int argc, char *argv[]) |
|
{ |
|
printf("=== PHP埋め込み実行プログラム (最終版) ===\n"); |
|
printf("PHP Version: %s\n", PHP_VERSION); |
|
|
|
// PHPの初期化 |
|
if (php_embed_init(argc, argv) != SUCCESS) { |
|
fprintf(stderr, "エラー: PHPの初期化に失敗しました\n"); |
|
return 1; |
|
} |
|
printf("✓ PHP埋め込みSAPIの初期化が成功しました\n"); |
|
|
|
// リクエスト開始 |
|
if (php_request_startup() == FAILURE) { |
|
fprintf(stderr, "エラー: リクエスト開始に失敗しました\n"); |
|
php_embed_shutdown(); |
|
return 1; |
|
} |
|
printf("✓ PHPリクエストの開始が成功しました\n"); |
|
|
|
// エラー表示を有効にする |
|
PG(display_errors) = 1; |
|
EG(error_reporting) = E_ALL; |
|
|
|
printf("\n=== PHPコード実行テスト ===\n"); |
|
|
|
// 基本的なecho文 |
|
execute_php_code("echo 'Hello from embedded PHP!' . \"\\n\";", "基本echo文"); |
|
|
|
// 変数と演算 |
|
execute_php_code("$a = 10; $b = 20; echo 'Result: ' . ($a + $b) . \"\\n\";", "変数と演算"); |
|
|
|
// 配列操作 |
|
execute_php_code("$arr = [1, 2, 3, 4, 5]; echo 'Array sum: ' . array_sum($arr) . \"\\n\";", "配列操作"); |
|
|
|
// 文字列操作 |
|
execute_php_code("$str = 'Hello World'; echo 'Length: ' . strlen($str) . \"\\n\"; echo 'Uppercase: ' . strtoupper($str) . \"\\n\";", "文字列操作"); |
|
|
|
// 条件分岐 |
|
execute_php_code("$x = 15; if ($x > 10) { echo 'x is greater than 10' . \"\\n\"; } else { echo 'x is not greater than 10' . \"\\n\"; }", "条件分岐"); |
|
|
|
// ループ |
|
execute_php_code("for ($i = 1; $i <= 3; $i++) { echo 'Count: ' . $i . \"\\n\"; }", "forループ"); |
|
|
|
// 関数定義と呼び出し |
|
execute_php_code("function greet($name) { return 'Hello, ' . $name . '!'; } echo greet('PHP') . \"\\n\";", "関数定義・呼び出し"); |
|
|
|
// 日付・時刻 |
|
execute_php_code("echo 'Current time: ' . date('Y-m-d H:i:s') . \"\\n\";", "日付・時刻"); |
|
|
|
// コマンドライン引数のファイル実行 |
|
if (argc > 1) { |
|
printf("\n=== ファイル実行 ===\n"); |
|
execute_php_file(argv[1]); |
|
} else { |
|
printf("\n=== サンプルファイル作成・実行 ===\n"); |
|
|
|
// サンプルPHPファイルを作成 |
|
FILE *fp = fopen("sample.php", "w"); |
|
if (fp) { |
|
fprintf(fp, "<?php\n"); |
|
fprintf(fp, "echo \"=== Sample PHP File ===\\n\";\n"); |
|
fprintf(fp, "$data = ['apple', 'banana', 'orange'];\n"); |
|
fprintf(fp, "foreach ($data as $index => $fruit) {\n"); |
|
fprintf(fp, " echo ($index + 1) . \". $fruit\\n\";\n"); |
|
fprintf(fp, "}\n"); |
|
fprintf(fp, "echo \"Total items: \" . count($data) . \"\\n\";\n"); |
|
fprintf(fp, "?>"); |
|
fclose(fp); |
|
|
|
printf("sample.phpを作成しました\n"); |
|
execute_php_file("sample.php"); |
|
remove("sample.php"); |
|
} |
|
} |
|
|
|
// 出力フラッシュ |
|
printf("\n=== 出力フラッシュ ===\n"); |
|
fflush(stdout); |
|
php_output_flush_all(); |
|
printf("✓ 出力フラッシュが完了しました\n"); |
|
|
|
// クリーンアップ |
|
printf("\n=== クリーンアップ ===\n"); |
|
php_embed_shutdown(); |
|
printf("✓ プログラム終了\n"); |
|
|
|
return 0; |
|
} |