L o a d i n g . . .
主打一个C++
文章详情

C++调用php函数

Posted on 2024-09-05 23:46:18 by 主打一个C++

php解释器Interpreter

当前操作环境:Linux

1.下载源代码,我直接克隆了

git clone https://github.com/php/php-src.git

2.进入克隆的php源代码目录

cd php-src

3.生成configure脚本

./buildconf

4.启用php镶入式环境

./configure --enable-embed

5.创建一个C++测试程序来测试执行php代码

#include <iostream>
#include <string>
//引入php解析库
#include <sapi/embed/php_embed.h>

//简单封装一个php调用函数
void run_php(const std::string& _phpcode){

    zend_eval_string(_phpcode.c_str(), nullptr, const_cast<char*>("PHP Code") ZEND_FILE_LINE_CC);
}
int main(int argc, char* argv[]) {
    // 初始化PHP嵌入式环境
    PHP_EMBED_START_BLOCK(argc, argv);
    // 测试定义一个php函数,(实际可以直接操作php文件)
    const std::string phpCode = R"(
        <?php
        function hello($name) {
            return "Hello, " . $name . "!";
        }
        echo hello("World");
        ?>
    )";
    // 执行PHP代码
    run_php(phpCode.c_str());
    // 清理PHP嵌入式环境
    PHP_EMBED_END_BLOCK();
    return 0;

}


*转载请注明出处:原文链接:https://cpp.vin/page/26.html

作者近期文章
提示
×
确定
数据库执行: 7次 总耗时: 0.01s
页面加载耗时: 



wechat +447752296473
wechat cpp-blog