{"id":59012,"date":"2024-10-12T21:08:17","date_gmt":"2024-10-12T13:08:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/59012\/"},"modified":"2024-10-12T21:08:17","modified_gmt":"2024-10-12T13:08:17","slug":"api%e4%b8%80%e9%8d%b5%e5%b0%8e%e5%87%ba%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e6%93%8d%e4%bd%9c%e7%b0%a1%e5%96%ae%e5%bf%ab%e6%8d%b7-api%e5%b0%8e%e5%87%ba%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/59012\/","title":{"rendered":"API\u4e00\u9375\u5c0e\u51fa\u6578\u64da\u5eab\uff0c\u64cd\u4f5c\u7c21\u55ae\u5feb\u6377 (api\u5c0e\u51fa\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"api%e4%b8%80%e9%8d%b5%e5%b0%8e%e5%87%ba%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e6%93%8d%e4%bd%9c%e7%b0%a1%e5%96%ae%e5%bf%ab%e6%8d%b7-ImYYioyiiw\">API\u4e00\u9375\u5c0e\u51fa\u6578\u64da\u5eab\uff0c\u64cd\u4f5c\u7c21\u55ae\u5feb\u6377<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u548c\u64cd\u4f5c\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002\u96a8\u8457\u6280\u8853\u7684\u9032\u6b65\uff0c\u958b\u767c\u8005\u548c\u7cfb\u7d71\u7ba1\u7406\u54e1\u9700\u8981\u66f4\u9ad8\u6548\u7684\u5de5\u5177\u4f86\u8655\u7406\u6578\u64da\u3002API\uff08\u61c9\u7528\u7a0b\u5f0f\u4ecb\u9762\uff09\u4f5c\u70ba\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u7c21\u5316\u6578\u64da\u5eab\u7684\u5c0e\u51fa\u904e\u7a0b\uff0c\u8b93\u64cd\u4f5c\u8b8a\u5f97\u7c21\u55ae\u5feb\u6377\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u901a\u904eAPI\u4e00\u9375\u5c0e\u51fa\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afapi%e5%b0%8e%e5%87%ba%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-ImYYioyiiw\">\u4ec0\u9ebc\u662fAPI\u5c0e\u51fa\u6578\u64da\u5eab\uff1f<\/h2>\n<p>API\u5c0e\u51fa\u6578\u64da\u5eab\u662f\u6307\u901a\u904eAPI\u63a5\u53e3\u5c07\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u5c0e\u51fa\u5230\u6307\u5b9a\u683c\u5f0f\uff08\u5982JSON\u3001CSV\u7b49\uff09\u7684\u904e\u7a0b\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u8655\u7406\u7684\u6548\u7387\uff0c\u9084\u6e1b\u5c11\u4e86\u624b\u52d5\u64cd\u4f5c\u7684\u932f\u8aa4\u7387\u3002\u4f7f\u7528API\u5c0e\u51fa\u6578\u64da\u5eab\u7684\u4e3b\u8981\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u81ea\u52d5\u5316\uff1a\u901a\u904e\u7de8\u5beb\u8173\u672c\uff0c\u53ef\u4ee5\u81ea\u52d5\u5316\u6578\u64da\u5c0e\u51fa\u904e\u7a0b\uff0c\u7bc0\u7701\u6642\u9593\u548c\u4eba\u529b\u6210\u672c\u3002<\/li>\n<li>\u9748\u6d3b\u6027\uff1aAPI\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9032\u884c\u5b9a\u5236\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u683c\u5f0f\u7684\u5c0e\u51fa\u3002<\/li>\n<li>\u53ef\u91cd\u7528\u6027\uff1a\u4e00\u65e6\u8a2d\u7f6e\u597dAPI\uff0c\u672a\u4f86\u7684\u6578\u64da\u5c0e\u51fa\u53ef\u4ee5\u91cd\u8907\u4f7f\u7528\uff0c\u7121\u9700\u91cd\u65b0\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8api%e5%b0%8e%e5%87%ba%e6%95%b8%e6%93%9a%e5%ba%ab-ImYYioyiiw\">\u5982\u4f55\u4f7f\u7528API\u5c0e\u51fa\u6578\u64da\u5eab<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528API\u5c0e\u51fa\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e8%a8%ad%e7%bd%aeapi%e6%8e%a5%e5%8f%a3-ImYYioyiiw\">1. \u8a2d\u7f6eAPI\u63a5\u53e3<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u8a2d\u7f6e\u4e00\u500bAPI\u63a5\u53e3\u4f86\u8655\u7406\u6578\u64da\u5c0e\u51fa\u8acb\u6c42\u3002\u9019\u901a\u5e38\u6d89\u53ca\u5230\u5f8c\u7aef\u958b\u767c\uff0c\u4f7f\u7528\u5982Node.js\u3001Python\u6216PHP\u7b49\u8a9e\u8a00\u4f86\u5275\u5efaAPI\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684Node.js\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nconst express = require('express');\nconst app = express();\nconst mysql = require('mysql');\n\nconst connection = mysql.createConnection({\n    host: 'localhost',\n    user: 'root',\n    password: 'password',\n    database: 'your_database'\n});\n\napp.get('\/export', (req, res) =&gt; {\n    connection.query('SELECT * FROM your_table', (error, results) =&gt; {\n        if (error) throw error;\n        res.json(results);\n    });\n});\n\napp.listen(3000, () =&gt; {\n    console.log('API is running on http:\/\/localhost:3000\/export');\n});\n<\/code><\/pre>\n<h3 id=\"2-%e7%99%bc%e9%80%81%e8%ab%8b%e6%b1%82-ImYYioyiiw\">2. \u767c\u9001\u8acb\u6c42<\/h3>\n<p>\u8a2d\u7f6e\u597dAPI\u5f8c\uff0c\u53ef\u4ee5\u901a\u904eHTTP\u8acb\u6c42\u4f86\u5c0e\u51fa\u6578\u64da\u3002\u4f7f\u7528\u5982Postman\u6216cURL\u7b49\u5de5\u5177\u767c\u9001GET\u8acb\u6c42\uff1a<\/p>\n<pre><code>\ncurl -X GET http:\/\/localhost:3000\/export\n<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u6578\u64da\u5eab\u4e2d\u6307\u5b9a\u8868\u7684\u6240\u6709\u6578\u64da\uff0c\u683c\u5f0f\u70baJSON\u3002<\/p>\n<h3 id=\"3-%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86-ImYYioyiiw\">3. \u6578\u64da\u8655\u7406<\/h3>\n<p>\u7372\u53d6\u6578\u64da\u5f8c\uff0c\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9032\u884c\u9032\u4e00\u6b65\u8655\u7406\uff0c\u4f8b\u5982\u5c07\u5176\u8f49\u63db\u70baCSV\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684JavaScript\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nconst json2csv = require('json2csv').parse;\n\nconst jsonData = [ \/* \u5f9eAPI\u7372\u53d6\u7684\u6578\u64da *\/ ];\nconst csv = json2csv(jsonData);\nconsole.log(csv);\n<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-ImYYioyiiw\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528API\u5c0e\u51fa\u6578\u64da\u5eab\u6642\uff0c\u6709\u5e7e\u500b\u6ce8\u610f\u4e8b\u9805\u9700\u8981\u8003\u616e\uff1a<\/p>\n<ul>\n<li>\u5b89\u5168\u6027\uff1a\u78ba\u4fddAPI\u63a5\u53e3\u7684\u5b89\u5168\u6027\uff0c\u9632\u6b62\u672a\u7d93\u6388\u6b0a\u7684\u8a2a\u554f\u3002<\/li>\n<li>\u6027\u80fd\uff1a\u5c0d\u65bc\u5927\u578b\u6578\u64da\u96c6\uff0c\u8003\u616e\u4f7f\u7528\u5206\u9801\u6216\u9650\u5236\u8fd4\u56de\u7684\u6578\u64da\u91cf\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li>\u932f\u8aa4\u8655\u7406\uff1a\u5728API\u4e2d\u5be6\u73fe\u932f\u8aa4\u8655\u7406\u6a5f\u5236\uff0c\u4ee5\u4fbf\u5728\u51fa\u73fe\u554f\u984c\u6642\u80fd\u5920\u53ca\u6642\u53cd\u994b\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ImYYioyiiw\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904eAPI\u4e00\u9375\u5c0e\u51fa\u6578\u64da\u5eab\u7684\u904e\u7a0b\u4e0d\u50c5\u7c21\u55ae\u5feb\u6377\uff0c\u9084\u80fd\u63d0\u9ad8\u6578\u64da\u7ba1\u7406\u7684\u6548\u7387\u3002\u7121\u8ad6\u662f\u958b\u767c\u8005\u9084\u662f\u7cfb\u7d71\u7ba1\u7406\u54e1\uff0c\u90fd\u53ef\u4ee5\u5229\u7528\u9019\u7a2e\u65b9\u6cd5\u4f86\u7c21\u5316\u65e5\u5e38\u5de5\u4f5c\u3002\u82e5\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684API\u958b\u767c\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5feb\u901f\u7c21\u4fbf\u7684API\u4e00\u9375\u5c0e\u51fa\u6578\u64da\u5eab\uff0c\u8f15\u9b06\u7372\u53d6\u6240\u9700\u6578\u64da\uff0c\u63d0\u5347\u5de5\u4f5c\u6548\u7387\uff0c\u8b93\u6578\u64da\u7ba1\u7406\u66f4\u9ad8\u6548\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-59012","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59012","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=59012"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59012\/revisions"}],"predecessor-version":[{"id":59013,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/59012\/revisions\/59013"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=59012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=59012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=59012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}