{"id":59515,"date":"2024-10-12T22:36:17","date_gmt":"2024-10-12T14:36:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/59515\/"},"modified":"2024-10-12T22:36:17","modified_gmt":"2024-10-12T14:36:17","slug":"c-post%e5%af%a6%e7%8f%be%e7%b6%b2%e9%a0%81%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8f%90%e4%ba%a4-c-post%e5%90%91%e7%b6%b2%e9%a0%81%e6%8f%90%e4%ba%a4%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/59515\/","title":{"rendered":"C Post\u5be6\u73fe\u7db2\u9801\u6578\u64da\u5eab\u63d0\u4ea4 (c post\u5411\u7db2\u9801\u63d0\u4ea4\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"c-post%e5%af%a6%e7%8f%be%e7%b6%b2%e9%a0%81%e6%95%b8%e6%93%9a%e5%ba%ab%e6%8f%90%e4%ba%a4-c-post%e5%90%91%e7%b6%b2%e9%a0%81%e6%8f%90%e4%ba%a4%e6%95%b8%e6%93%9a%e5%ba%ab-pYRdvryILk\">C Post\u5be6\u73fe\u7db2\u9801\u6578\u64da\u5eab\u63d0\u4ea4 (c post\u5411\u7db2\u9801\u63d0\u4ea4\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u64cd\u4f5c\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u7121\u8ad6\u662f\u7528\u6236\u8a3b\u518a\u3001\u767b\u9304\uff0c\u9084\u662f\u6578\u64da\u7684\u589e\u522a\u6539\u67e5\uff0c\u9019\u4e9b\u64cd\u4f5c\u90fd\u9700\u8981\u901a\u904e\u7db2\u9801\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u7684POST\u8acb\u6c42\u4f86\u5be6\u73fe\u7db2\u9801\u6578\u64da\u5eab\u7684\u63d0\u4ea4\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afpost%e8%ab%8b%e6%b1%82%ef%bc%9f-pYRdvryILk\">\u4ec0\u9ebc\u662fPOST\u8acb\u6c42\uff1f<\/h2>\n<p>POST\u8acb\u6c42\u662f\u4e00\u7a2eHTTP\u8acb\u6c42\u65b9\u6cd5\uff0c\u4e3b\u8981\u7528\u65bc\u5411\u670d\u52d9\u5668\u63d0\u4ea4\u6578\u64da\u3002\u8207GET\u8acb\u6c42\u4e0d\u540c\uff0cPOST\u8acb\u6c42\u7684\u6578\u64da\u4e0d\u6703\u986f\u793a\u5728URL\u4e2d\uff0c\u800c\u662f\u653e\u5728\u8acb\u6c42\u7684\u4e3b\u9ad4\u4e2d\uff0c\u9019\u4f7f\u5f97\u5b83\u66f4\u9069\u5408\u7528\u65bc\u63d0\u4ea4\u654f\u611f\u4fe1\u606f\u6216\u5927\u91cf\u6578\u64da\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8c%e8%aa%9e%e8%a8%80%e7%99%bc%e9%80%81post%e8%ab%8b%e6%b1%82-pYRdvryILk\">\u4f7f\u7528C\u8a9e\u8a00\u767c\u9001POST\u8acb\u6c42<\/h2>\n<p>\u5728C\u8a9e\u8a00\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528libcurl\u5eab\u4f86\u767c\u9001HTTP\u8acb\u6c42\u3002libcurl\u662f\u4e00\u500b\u5f37\u5927\u7684\u5eab\uff0c\u652f\u6301\u591a\u7a2e\u5354\u8b70\uff0c\u5305\u62ecHTTP\u3001HTTPS\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528libcurl\u767c\u9001POST\u8acb\u6c42\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9dlibcurl-pYRdvryILk\">\u5b89\u88ddlibcurl<\/h3>\n<p>\u5728Linux\u7cfb\u7d71\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88ddlibcurl\uff1a<\/p>\n<pre><code>sudo apt-get install libcurl4-openssl-dev<\/code><\/pre>\n<h3 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-pYRdvryILk\">\u793a\u4f8b\u4ee3\u78bc<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684C\u8a9e\u8a00\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528libcurl\u767c\u9001POST\u8acb\u6c42\uff1a<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;curl\/curl.h&gt;\n\nint main(void) {\n    CURL *curl;\n    CURLcode res;\n\n    curl_global_init(CURL_GLOBAL_DEFAULT);\n    curl = curl_easy_init();\n    if(curl) {\n        \/\/ \u8a2d\u7f6eURL\n        curl_easy_setopt(curl, CURLOPT_URL, \"http:\/\/example.com\/submit\");\n\n        \/\/ \u8a2d\u7f6ePOST\u6578\u64da\n        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, \"name=John&amp;age=30\");\n\n        \/\/ \u57f7\u884c\u8acb\u6c42\n        res = curl_easy_perform(curl);\n\n        \/\/ \u6aa2\u67e5\u932f\u8aa4\n        if(res != CURLE_OK) {\n            fprintf(stderr, \"curl_easy_perform() failed: %sn\", curl_easy_strerror(res));\n        }\n\n        \/\/ \u6e05\u7406\n        curl_easy_cleanup(curl);\n    }\n\n    curl_global_cleanup();\n    return 0;\n}\n<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e8%99%95%e7%90%86-pYRdvryILk\">\u6578\u64da\u5eab\u7684\u8655\u7406<\/h2>\n<p>\u7576POST\u8acb\u6c42\u767c\u9001\u5230\u670d\u52d9\u5668\u5f8c\uff0c\u670d\u52d9\u5668\u9700\u8981\u8655\u7406\u9019\u4e9b\u6578\u64da\u4e26\u5c07\u5176\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\u3002\u9019\u901a\u5e38\u6d89\u53ca\u5230\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<ul>\n<li>\u63a5\u6536\u8acb\u6c42\u6578\u64da<\/li>\n<li>\u9a57\u8b49\u6578\u64da\u7684\u6709\u6548\u6027<\/li>\n<li>\u5c07\u6578\u64da\u63d2\u5165\u5230\u6578\u64da\u5eab\u4e2d<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528PHP\u8655\u7406POST\u8acb\u6c42\u4e26\u5c07\u6578\u64da\u63d2\u5165MySQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"database\";\n\n\/\/ \u5275\u5efa\u9023\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"Connection failed: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u63a5\u6536POST\u6578\u64da\n$name = $_POST['name'];\n$age = $_POST['age'];\n\n\/\/ \u63d2\u5165\u6578\u64da\n$sql = \"INSERT INTO users (name, age) VALUES ('$name', '$age')\";\nif ($conn-&gt;query($sql) === TRUE) {\n    echo \"New record created successfully\";\n} else {\n    echo \"Error: \" . $sql . \"&lt;br&gt;\" . $conn-&gt;error;\n}\n\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-pYRdvryILk\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4f7f\u7528C\u8a9e\u8a00\u7684libcurl\u5eab\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u767c\u9001POST\u8acb\u6c42\uff0c\u4e26\u5c07\u6578\u64da\u63d0\u4ea4\u5230\u7db2\u9801\u670d\u52d9\u5668\u3002\u96a8\u5f8c\uff0c\u670d\u52d9\u5668\u53ef\u4ee5\u4f7f\u7528\u76f8\u61c9\u7684\u5f8c\u7aef\u6280\u8853\uff08\u5982PHP\uff09\u4f86\u8655\u7406\u9019\u4e9b\u6578\u64da\u4e26\u5c07\u5176\u5b58\u5132\u5230\u6578\u64da\u5eab\u4e2d\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u4ea4\u4e92\u7684\u5b89\u5168\u6027\uff0c\u9084\u80fd\u6709\u6548\u5730\u7ba1\u7406\u7528\u6236\u6578\u64da\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728\u9999\u6e2f\u7684VPS\u74b0\u5883\u4e2d\u5be6\u73fe\u9019\u4e9b\u529f\u80fd\u611f\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4e86\u89e3\u66f4\u591a\u8cc7\u8a0a\uff0c\u63a2\u7d22\u6211\u5011\u7684<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u89e3\u6c7a\u65b9\u6848\uff0c\u52a9\u60a8\u8f15\u9b06\u642d\u5efa\u9ad8\u6548\u7684\u7db2\u9801\u61c9\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528C Post\u5be6\u73fe\u7db2\u9801\u6578\u64da\u5eab\u63d0\u4ea4\uff0c\u7c21\u5316\u6578\u64da\u8655\u7406\u6d41\u7a0b\uff0c\u63d0\u5347\u7db2\u7ad9\u529f\u80fd\u8207\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-59515","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=59515"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59515\/revisions"}],"predecessor-version":[{"id":59516,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59515\/revisions\/59516"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=59515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=59515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=59515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}