{"id":48406,"date":"2024-12-02T14:21:56","date_gmt":"2024-12-02T06:21:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48406\/"},"modified":"2024-12-02T14:21:56","modified_gmt":"2024-12-02T06:21:56","slug":"%e5%b0%86-php-%e5%87%bd%e6%95%b0%e9%9b%86%e6%88%90%e5%88%b0-c-%e6%89%a9%e5%b1%95%e4%b8%ad%e7%9a%84%e6%ad%a5%e9%aa%a4%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48406\/","title":{"rendered":"\u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55\u4e2d\u7684\u6b65\u9aa4\u8be6\u89e3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55\u4e2d\u7684\u6b65\u9aa4\u8be6\u89e3<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55\u4e2d\u7684\u6b65\u9aa4\u8be6\u89e3\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p>\u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55\u4e2d\u53ef\u6269\u5c55 PHP \u6838\u5fc3\u529f\u80fd\uff0c\u5b9e\u73b0\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa PHP \u914d\u7f6e\u6587\u4ef6\uff0c\u4e3a\u6269\u5c55\u6dfb\u52a0\u5165\u53e3\uff0c\u5b9e\u73b0\u6269\u5c55\uff0c\u5728\u6269\u5c55\u521d\u59cb\u5316\u51fd\u6570\u4e2d\u6ce8\u518c PHP \u51fd\u6570\uff0c\u6700\u540e\u5728 PHP \u4ee3\u7801\u4e2d\u52a0\u8f7d\u5e76\u4f7f\u7528\u6269\u5c55\u3002\u5982\u9700\u521b\u5efa\u4e00\u4e2a\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u6269\u5c55\u51fd\u6570\uff0c\u9700\u8981\u5728 C \u4ee3\u7801\u4e2d\u5b9a\u4e49\u51fd\u6570\u5e76\u6ce8\u518c\u5e38\u91cf\uff0c\u5728 PHP \u4ee3\u7801\u4e2d\u52a0\u8f7d\u6269\u5c55\u5e76\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730024791671e15573c189.jpg\" class=\"aligncenter\" title=\"\u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55\u4e2d\u7684\u6b65\u9aa4\u8be6\u89e3\u63d2\u56fe\" alt=\"\u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55\u4e2d\u7684\u6b65\u9aa4\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55<\/strong><\/p>\n<p>PHP \u6269\u5c55\u662f\u589e\u5f3a PHP \u6838\u5fc3\u529f\u80fd\u7684\u4e8c\u8fdb\u5236\u6a21\u5757\u3002\u96c6\u6210 PHP \u51fd\u6570\u662f\u6269\u5c55\u8fd9\u79cd\u529f\u80fd\u7684\u6709\u6548\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u6b65\u9aa4<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa PHP \u914d\u7f6e\u6587\u4ef6<\/strong><br \/>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>php.ini<\/code> \u7684\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<pre>extension=C \u6269\u5c55\u540d\u79f0.so<\/pre>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u6269\u5c55<\/strong><br \/>\u4f7f\u7528 C \u8bed\u8a00\u5b9e\u73b0\u6269\u5c55\uff0c\u521b\u5efa\u4ee5\u4e0b\u51fd\u6570\uff1a<\/p>\n<pre>PHP_FUNCTION(php_example)\n{\n    \/\/ ... \u51fd\u6570\u4f53\n}<\/pre>\n<\/li>\n<li>\n<p><strong>\u6ce8\u518c\u6269\u5c55<\/strong><br \/>\u5728 <code>php.h<\/code> \u5934\u6587\u4ef6\u4e2d\u58f0\u660e\u4ee5\u4e0b\u51fd\u6570\uff1a<\/p>\n<pre>zend_module_entry php_example_module_entry = {\n    STANDARD_MODULE_HEADER,\n    php_example_init,      \/\/ \u521d\u59cb\u5316\u51fd\u6570\n    NULL,\n    NULL,\n    NULL,\n    NULL,\n    NULL,\n    NULL,\n    \"1.0\",\n};<\/pre>\n<\/li>\n<li>\n<p><strong>\u521d\u59cb\u5316\u6269\u5c55<\/strong><br \/>\u5728 <code>php_example_init<\/code> \u51fd\u6570\u4e2d\u6ce8\u518c PHP \u51fd\u6570\uff1a<\/p>\n<pre>PHP_MINIT_FUNCTION(php_example)\n{\n    REGISTER_LONG_CONSTANT(\"EXAMPLE_CONSTANT\", 1234, CONST_CS);\n    REGISTER_STRING_CONSTANT(\"EXAMPLE_STRING\", \"Hello\", CONST_CS | CONST_PERSISTENT);\n}<\/pre>\n<\/li>\n<\/ol>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a C \u6269\u5c55\uff0c\u63d0\u4f9b\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1a<\/p>\n<p><strong>C \u4ee3\u7801<\/strong><\/p>\n<pre>#include \"php.h\"\n\nPHP_FUNCTION(example_get_string)\n{\n    char *str = \"Hello World!\";\n    RETURN_STRING(str);\n}\n\nPHP_MINIT_FUNCTION(example)\n{\n    REGISTER_LONG_CONSTANT(\"EXAMPLE_CONSTANT\", 1234, CONST_CS);\n    REGISTER_STRING_CONSTANT(\"EXAMPLE_STRING\", \"Hello\", CONST_CS | CONST_PERSISTENT);\n    REGISTER_STRING_FUNCTION(example_get_string, \"example_get_string\");\n}<\/pre>\n<p><strong>PHP \u4ee3\u7801<\/strong><\/p>\n<pre>&lt;?php\n\n\/\/ \u52a0\u8f7d\u6269\u5c55\ndl('C \u6269\u5c55\u540d\u79f0.so');\n\n\/\/ \u4f7f\u7528\u6269\u5c55\u51fd\u6570\necho example_get_string();<\/pre>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55\u4e2d\u7684\u6b65\u9aa4\u8be6\u89e3\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55\u4e2d\u7684\u6b65\u9aa4\u8be6\u89e3 \u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55\u4e2d\u7684\u6b65\u9aa4\u8be6\u89e3\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01 \u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55\u4e2d\u53ef\u6269\u5c55 PHP \u6838\u5fc3\u529f\u80fd\uff0c\u5b9e\u73b0\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa PHP \u914d\u7f6e\u6587\u4ef6\uff0c\u4e3a\u6269\u5c55\u6dfb\u52a0\u5165\u53e3\uff0c\u5b9e\u73b0\u6269\u5c55\uff0c\u5728\u6269\u5c55\u521d\u59cb\u5316\u51fd\u6570\u4e2d\u6ce8\u518c PHP \u51fd\u6570\uff0c\u6700\u540e\u5728 PHP \u4ee3\u7801\u4e2d\u52a0\u8f7d\u5e76\u4f7f\u7528\u6269\u5c55\u3002\u5982\u9700\u521b\u5efa\u4e00\u4e2a\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u6269\u5c55\u51fd\u6570\uff0c\u9700\u8981\u5728 C \u4ee3\u7801\u4e2d\u5b9a\u4e49\u51fd\u6570\u5e76\u6ce8\u518c\u5e38\u91cf\uff0c\u5728 PHP \u4ee3\u7801\u4e2d\u52a0\u8f7d\u6269\u5c55\u5e76\u8c03\u7528\u51fd\u6570\u3002 \u5c06 PHP \u51fd\u6570\u96c6\u6210\u5230 C \u6269\u5c55 PHP \u6269\u5c55\u662f\u589e\u5f3a PHP \u6838\u5fc3\u529f\u80fd\u7684\u4e8c\u8fdb\u5236\u6a21\u5757\u3002\u96c6\u6210 PHP \u51fd\u6570\u662f\u6269\u5c55\u8fd9\u79cd\u529f\u80fd\u7684\u6709\u6548\u65b9\u6cd5\u3002 \u6b65\u9aa4 \u521b\u5efa PHP \u914d\u7f6e\u6587\u4ef6\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a php.ini \u7684\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a extension=C \u6269\u5c55\u540d\u79f0.so \u5b9e\u73b0\u6269\u5c55\u4f7f\u7528 C \u8bed\u8a00\u5b9e\u73b0\u6269\u5c55\uff0c\u521b\u5efa\u4ee5\u4e0b\u51fd\u6570\uff1a PHP_FUNCTION(php_example) { \/\/ &#8230; \u51fd\u6570\u4f53 } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-48406","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=48406"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48406\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}