{"id":48002,"date":"2024-12-02T17:27:10","date_gmt":"2024-12-02T09:27:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48002\/"},"modified":"2024-12-02T17:27:10","modified_gmt":"2024-12-02T09:27:10","slug":"%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e5%85%b7%e6%9c%89-php-%e5%87%bd%e6%95%b0%e7%9a%84%e8%87%aa%e5%ae%9a%e4%b9%89-c-%e6%89%a9%e5%b1%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48002\/","title":{"rendered":"\u5982\u4f55\u521b\u5efa\u5177\u6709 PHP \u51fd\u6570\u7684\u81ea\u5b9a\u4e49 C \u6269\u5c55\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u521b\u5efa\u5177\u6709 PHP \u51fd\u6570\u7684\u81ea\u5b9a\u4e49 C \u6269\u5c55\uff1f<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u521b\u5efa\u5177\u6709 PHP \u51fd\u6570\u7684\u81ea\u5b9a\u4e49 C \u6269\u5c55\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p>\u521b\u5efa\u81ea\u5b9a\u4e49 C \u6269\u5c55\u5177\u6709\u4ee5\u4e0b 3 \u4e2a\u6b65\u9aa4\uff1a\u521b\u5efa\u5305\u542b PHP \u51fd\u6570\u7684 C \u6587\u4ef6\u6ce8\u518c\u51fd\u6570\u5e76\u521b\u5efa\u6269\u5c55\u6a21\u5757\u7f16\u8bd1\u548c\u5b89\u88c5\u6269\u5c55<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729948480671ceb40a36e3.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u521b\u5efa\u5177\u6709 PHP \u51fd\u6570\u7684\u81ea\u5b9a\u4e49 C \u6269\u5c55\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u521b\u5efa\u5177\u6709 PHP \u51fd\u6570\u7684\u81ea\u5b9a\u4e49 C \u6269\u5c55\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u521b\u5efa\u5177\u6709 PHP \u51fd\u6570\u7684\u81ea\u5b9a\u4e49 C \u6269\u5c55<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u81ea\u5b9a\u4e49 C \u6269\u5c55\u5141\u8bb8\u60a8\u901a\u8fc7 C \u4ee3\u7801\u6269\u5c55 PHP \u5185\u7f6e\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u6269\u5c55\u73b0\u6709\u7684\u51fd\u6570\u6216\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7684\u4ee3\u7801\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u5177\u6709 PHP \u51fd\u6570\u7684\u7b80\u5355 C \u6269\u5c55\u3002<\/p>\n<p><strong>\u5148\u51b3\u6761\u4ef6<\/strong><\/p>\n<ul>\n<li>C \u7f16\u8bd1\u5668\uff08\u4f8b\u5982 GCC \u6216 Clang\uff09<\/li>\n<li>PHP \u5f00\u53d1\u73af\u5883<\/li>\n<li>PHP \u5934\u6587\u4ef6<\/li>\n<li>\u6587\u672c\u7f16\u8f91\u5668<\/li>\n<\/ul>\n<p><strong>\u6b65\u9aa4<\/strong><\/p>\n<ol>\n<li><strong>\u521b\u5efa C \u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>myextension.c<\/code> \u7684 C \u6e90\u6587\u4ef6\u3002\u8fd9\u5c06\u5305\u542b\u60a8\u7684\u6269\u5c55\u4ee3\u7801\u3002<\/p>\n<ol>\n<li><strong>\u5305\u542b PHP \u5934\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p>\u5728\u60a8\u7684 C \u6587\u4ef6\u4e2d\uff0c\u5305\u542b\u4ee5\u4e0b PHP \u5934\u6587\u4ef6\uff1a<\/p>\n<pre>#include &lt;php.h&gt;<\/pre>\n<ol>\n<li><strong>\u521b\u5efa PHP \u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u8981\u6dfb\u52a0\u5230 PHP \u7684 PHP \u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>my_greet<\/code> \u7684\u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>PHP_FUNCTION(my_greet)\n{\n    char *name;\n    size_t name_len;\n\n    if (zend_parse_parameters(ZEND_NUM_ARGS(), \"s\", &amp;name, &amp;name_len) == FAILURE) {\n        return;\n    }\n\n    printf(\"Hello, %s!\\n\", name);\n}<\/pre>\n<ol>\n<li><strong>\u6ce8\u518c PHP \u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p>\u60a8\u9700\u8981\u5c06\u60a8\u7684\u51fd\u6570\u6ce8\u518c\u5230 PHP \u624d\u80fd\u4f7f\u5176\u53ef\u7528\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u6ce8\u518c <code>my_greet<\/code> \u51fd\u6570\uff1a<\/p>\n<pre>zend_function_entry myextension_functions[] = {\n    PHP_FE(my_greet, NULL)\n};<\/pre>\n<ol>\n<li><strong>\u521b\u5efa\u6269\u5c55\u6a21\u5757<\/strong><\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6269\u5c55\u6a21\u5757\u4ee5\u6253\u5305\u60a8\u7684\u51fd\u6570\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b C \u4ee3\u7801\u6765\u5b8c\u6210\uff1a<\/p>\n<pre>zend_module_entry myextension_module_entry = {\n    STANDARD_MODULE_HEADER,\n    NULL,\n    NULL,\n    myextension_functions,\n    NULL,\n    NULL,\n    NULL,\n    NULL,\n    \"My Custom Extension\",\n    \"1.0\"\n};<\/pre>\n<ol>\n<li><strong>\u7f16\u8bd1\u6269\u5c55<\/strong><\/li>\n<\/ol>\n<p>\u4f7f\u7528 C \u7f16\u8bd1\u5668\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u7f16\u8bd1\u60a8\u7684\u6269\u5c55\uff1a<\/p>\n<pre>gcc -shared -o myextension.so myextension.c -I\/usr\/include\/php5<\/pre>\n<p>\u6ce8\u610f\uff1aPHP \u5934\u6587\u4ef6\u8def\u5f84\u53ef\u80fd\u56e0\u60a8\u7684\u7cfb\u7edf\u800c\u5f02\u3002<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u6269\u5c55<\/strong><\/li>\n<\/ol>\n<p>\u5c06\u751f\u6210\u7684 <code>.so<\/code> \u6587\u4ef6\u590d\u5236\u5230 PHP \u6269\u5c55\u76ee\u5f55\u4e2d\u3002\u5728 Ubuntu \u4e0a\uff0c\u6b64\u76ee\u5f55\u901a\u5e38\u4f4d\u4e8e <code>\/usr\/lib\/php5\/modules\/<\/code> \u4e2d\u3002<\/p>\n<ol>\n<li><strong>\u542f\u7528\u6269\u5c55<\/strong><\/li>\n<\/ol>\n<p>\u7f16\u8f91\u60a8\u7684 <code>php.ini<\/code> \u6587\u4ef6\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<pre>extension=myextension.so<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a PHP \u811a\u672c\u4ee5\u8c03\u7528\u65b0\u521b\u5efa\u7684\u51fd\u6570\uff1a<\/p>\n<pre>&lt;?php\n\/\/ \u8c03\u7528 my_greet \u51fd\u6570\u5e76\u4f20\u9012\u4e00\u4e2a\u540d\u79f0\nmy_greet('John');\n?&gt;<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>Hello, John!<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u5177\u6709 PHP \u51fd\u6570\u7684\u81ea\u5b9a\u4e49 C \u6269\u5c55\u3002\u8fd9\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6269\u5c55 PHP \u7684\u529f\u80fd\u5e76\u521b\u5efa\u6709\u7528\u7684\u5de5\u5177\u548c\u5e93\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u521b\u5efa\u5177\u6709 PHP \u51fd\u6570\u7684\u81ea\u5b9a\u4e49 C \u6269\u5c55\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ephp,C\u6269\u5c55\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u521b\u5efa\u5177\u6709 PHP \u51fd\u6570\u7684\u81ea\u5b9a\u4e49 C \u6269\u5c55\uff1f \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u521b\u5efa\u5177\u6709 PHP \u51fd\u6570\u7684\u81ea\u5b9a\u4e49 C \u6269\u5c55\uff1f\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ \u521b\u5efa\u81ea\u5b9a\u4e49 C \u6269\u5c55\u5177\u6709\u4ee5\u4e0b 3 \u4e2a\u6b65\u9aa4\uff1a\u521b\u5efa\u5305\u542b PHP \u51fd\u6570\u7684 C \u6587\u4ef6\u6ce8\u518c\u51fd\u6570\u5e76\u521b\u5efa\u6269\u5c55\u6a21\u5757\u7f16\u8bd1\u548c\u5b89\u88c5\u6269\u5c55 \u5982\u4f55\u521b\u5efa\u5177\u6709 PHP \u51fd\u6570\u7684\u81ea\u5b9a\u4e49 C \u6269\u5c55 \u7b80\u4ecb \u81ea\u5b9a\u4e49 C \u6269\u5c55\u5141\u8bb8\u60a8\u901a\u8fc7 C \u4ee3\u7801\u6269\u5c55 PHP \u5185\u7f6e\u529f\u80fd\u3002\u60a8\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u6269\u5c55\u73b0\u6709\u7684\u51fd\u6570\u6216\u5b9e\u73b0\u9762\u5411\u5bf9\u8c61\u7684\u4ee3\u7801\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u5177\u6709 PHP \u51fd\u6570\u7684\u7b80\u5355 C \u6269\u5c55\u3002 \u5148\u51b3\u6761\u4ef6 C \u7f16\u8bd1\u5668\uff08\u4f8b\u5982 GCC \u6216 Clang\uff09 PHP \u5f00\u53d1\u73af\u5883 PHP \u5934\u6587\u4ef6 \u6587\u672c\u7f16\u8f91\u5668 \u6b65\u9aa4 \u521b\u5efa C \u6587\u4ef6 \u521b\u5efa\u4e00\u4e2a\u540d\u4e3a myextension.c \u7684 C [&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-48002","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48002","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=48002"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48002\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}