﻿{"id":3326,"date":"2024-11-11T15:22:15","date_gmt":"2024-11-11T07:22:15","guid":{"rendered":"http:\/\/www.jiayus.com\/?p=3326"},"modified":"2024-11-11T15:22:15","modified_gmt":"2024-11-11T07:22:15","slug":"%e5%a6%82%e4%bd%95%e7%94%a8api%e6%8e%a5%e5%8f%a3%e8%b0%83%e7%94%a8%e7%94%b2%e9%b1%bc%e8%88%86%e6%83%85%e7%9b%91%e6%b5%8b%e8%bd%af%e4%bb%b6%e9%87%87%e9%9b%86%e5%88%b0%e7%9a%84%e5%85%a8%e7%bd%91","status":"publish","type":"post","link":"http:\/\/www.jiayus.com\/?p=3326","title":{"rendered":"\u5982\u4f55\u7528API\u63a5\u53e3\u8c03\u7528\u7532\u9c7c\u8206\u60c5\u76d1\u6d4b\u8f6f\u4ef6\u91c7\u96c6\u5230\u7684\u5168\u7f51\u5386\u53f210\u5e74\u516c\u5f00\u8d44\u8baf\u6587\u672c\u7c7b\u6570\u636e"},"content":{"rendered":"\n<p>\u8ba1\u7b97\u673a\u8bed\u97f3\uff1aSQLite\u6570\u636e\u5e93\u3001python<\/p>\n\n\n\n<p>\u516c\u5f00\u8d44\u8baf\u6587\u672c\u6570\u636e\u4e3b\u8981\u5e73\u53f0\uff1a\u6296\u97f3\u3001\u767e\u5ea6\u3001\u5c0f\u7ea2\u4e66\u3001\u4eca\u65e5\u5934\u6761\u3001\u65b0\u6d6a\u5fae\u535a\u3001\u5fae\u4fe1\u516c\u4f17\u53f7\u3001\u77e5\u4e4e\u3001B\u7ad9\u3001\u8c46\u74e3\u3001\u65b0\u534e\u7f51\u3001\u4eba\u6c11\u7f51\u3001\u6c7d\u8f66\u4e4b\u5bb6\u3001\u65b0\u95fb\u7f51\u7ad9\u3001\u8bba\u575b\u3001APP\u3001\u8bc4\u8bba\u7b49\u6570\u636e<\/p>\n\n\n\n<p>\u9700\u6c421\uff1a\u901a\u8fc7API\u63a5\u53e3\u8c03\u7528\u67d0\u4e2a\u5173\u952e\u8bcd\u6216\u8005\u67d0\u4e2a\u903b\u8f91\u7b97\u6cd5\u5728\u4e92\u8054\u7f51\u5386\u53f210\u5e74\u5168\u7f51\u516c\u5f00\u8d44\u8baf\u6587\u672c\u7c7b\u6570\u636e\uff1b<\/p>\n\n\n\n<p>\u9700\u6c422\uff1a\u901a\u8fc7AP\u63a5\u53e3\u8c03\u7528\u67d0\u4e2a\u7f51\u7ad9\u6216\u8005\u67d0\u4e2a\u81ea\u5a92\u4f53\u8d26\u53f7\u5728\u4e92\u8054\u7f51\u5386\u53f210\u5e74\u5168\u7f51\u516c\u5f00\u8d44\u8baf\u6587\u672c\u7c7b\u6570\u636e\uff1b<\/p>\n\n\n\n<p>\u5b9e\u73b0\u9700\u6c42\u7684\u8ba1\u7b97\u673a\u8bed\u8a00\u4ee3\u7801\u5982\u4e0b\uff08\u524d\u63d0\u662f\u63d0\u4f9b\u6570\u636e\u65b9\u5f00\u901aAPI\u63a5\u53e3\u6743\u9650\uff09\uff1a<\/p>\n\n\n\n<p>import requests<br>import sqlite3<br>import datetime<\/p>\n\n\n\n<p># \u6570\u636e\u5e93\u521d\u59cb\u5316<br>def init_db():<br>&nbsp; &nbsp; conn = sqlite3.connect(&#8216;api_call_history.db&#8217;)<br>&nbsp; &nbsp; c = conn.cursor()<br>&nbsp; &nbsp; c.execute(&#8221;&#8217;CREATE TABLE IF NOT EXISTS api_calls<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(id INTEGER PRIMARY KEY AUTOINCREMENT,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endpoint TEXT,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response_status INTEGER,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response_body TEXT,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)&#8221;&#8217;)<br>&nbsp; &nbsp; conn.commit()<br>&nbsp; &nbsp; conn.close()<\/p>\n\n\n\n<p># \u8bb0\u5f55API\u8c03\u7528<br>def record_api_call(endpoint, response):<br>&nbsp; &nbsp; conn = sqlite3.connect(&#8216;api_call_history.db&#8217;)<br>&nbsp; &nbsp; c = conn.cursor()<br>&nbsp; &nbsp; c.execute(&#8220;INSERT INTO api_calls (endpoint, response_status, response_body) VALUES (?, ?, ?)&#8221;,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (endpoint, response.status_code, response.text))<br>&nbsp; &nbsp; conn.commit()<br>&nbsp; &nbsp; conn.close()<\/p>\n\n\n\n<p># \u8c03\u7528API<br>def call_api(endpoint):<br>&nbsp; &nbsp; try:<br>&nbsp; &nbsp; &nbsp; &nbsp; response = requests.get(endpoint)<br>&nbsp; &nbsp; &nbsp; &nbsp; record_api_call(endpoint, response)<br>&nbsp; &nbsp; &nbsp; &nbsp; return response<br>&nbsp; &nbsp; except requests.RequestException as e:<br>&nbsp; &nbsp; &nbsp; &nbsp; print(f&#8221;Error calling API: {e}&#8221;)<br>&nbsp; &nbsp; &nbsp; &nbsp; return None<\/p>\n\n\n\n<p># \u67e5\u8be2API\u8c03\u7528\u5386\u53f2<br>def query_api_calls():<br>&nbsp; &nbsp; conn = sqlite3.connect(&#8216;api_call_history.db&#8217;)<br>&nbsp; &nbsp; c = conn.cursor()<br>&nbsp; &nbsp; c.execute(&#8220;SELECT * FROM api_calls ORDER BY timestamp DESC&#8221;)<br>&nbsp; &nbsp; rows = c.fetchall()<br>&nbsp; &nbsp; for row in rows:<br>&nbsp; &nbsp; &nbsp; &nbsp; print(row)<br>&nbsp; &nbsp; conn.close()<\/p>\n\n\n\n<p># \u4e3b\u7a0b\u5e8f<br>if __name__ == &#8220;__main__&#8221;:<br>&nbsp; &nbsp; init_db() &nbsp;# \u521d\u59cb\u5316\u6570\u636e\u5e93<br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; # \u793a\u4f8bAPI\u8c03\u7528<br>&nbsp; &nbsp; endpoint = &#8220;https:\/\/jsonplaceholder.typicode.com\/posts\/1&#8221;<br>&nbsp; &nbsp; response = call_api(endpoint)<br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; if response:<br>&nbsp; &nbsp; &nbsp; &nbsp; print(f&#8221;API Call Successful: {response.status_code}&#8221;)<br>&nbsp; &nbsp; &nbsp; &nbsp; print(response.json())<br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; # \u67e5\u8be2API\u8c03\u7528\u5386\u53f2<br>&nbsp; &nbsp; query_api_calls()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8ba1\u7b97\u673a\u8bed\u97f3\uff1aSQLite\u6570\u636e\u5e93\u3001python \u516c\u5f00\u8d44\u8baf\u6587\u672c\u6570\u636e\u4e3b\u8981\u5e73\u53f0\uff1a\u6296\u97f3\u3001\u767e\u5ea6\u3001\u5c0f\u7ea2\u4e66\u3001\u4eca\u65e5\u5934\u6761\u3001\u65b0\u6d6a\u5fae\u535a\u3001\u5fae\u4fe1\u516c\u4f17\u53f7\u3001\u77e5\u4e4e\u3001B\u7ad9\u3001\u8c46\u74e3\u3001\u65b0\u534e\u7f51\u3001\u4eba\u6c11\u7f51\u3001\u6c7d\u8f66\u4e4b\u5bb6\u3001\u65b0\u95fb\u7f51\u7ad9\u3001\u8bba\u575b\u3001APP\u3001\u8bc4\u8bba\u7b49\u6570\u636e \u9700\u6c421\uff1a\u901a\u8fc7API\u63a5\u53e3\u8c03\u7528\u67d0\u4e2a\u5173\u952e\u8bcd\u6216\u8005\u67d0\u4e2a\u903b\u8f91\u7b97\u6cd5\u5728\u4e92\u8054\u7f51\u5386\u53f210\u5e74\u5168\u7f51\u516c\u5f00\u8d44\u8baf\u6587\u672c\u7c7b\u6570\u636e\uff1b \u9700\u6c422\uff1a\u901a\u8fc7AP\u63a5\u53e3\u8c03\u7528\u67d0\u4e2a\u7f51\u7ad9\u6216\u8005\u67d0\u4e2a\u81ea\u5a92\u4f53\u8d26\u53f7\u5728\u4e92\u8054\u7f51\u5386\u53f210\u5e74\u5168\u7f51\u516c\u5f00\u8d44\u8baf\u6587\u672c\u7c7b\u6570\u636e\uff1b \u5b9e\u73b0\u9700\u6c42\u7684\u8ba1\u7b97\u673a\u8bed\u8a00\u4ee3\u7801\u5982\u4e0b\uff08\u524d\u63d0\u662f\u63d0\u4f9b\u6570\u636e\u65b9\u5f00\u901aAPI\u63a5\u53e3\u6743\u9650\uff09\uff1a import requestsimport sqlite3import datetime # \u6570\u636e\u5e93\u521d\u59cb\u5316def init_db():&nbsp; &nbsp; conn = sqlite3.connect(&#8216;api_call_history.db&#8217;)&nbsp; &nbsp; c = conn.cursor()&nbsp; &nbsp; c.execute(&#8221;&#8217;CREATE TABLE IF NOT EXISTS api_calls&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(id INTEGER PRIMARY KEY AUTOINCREMENT,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endpoint TEXT,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&hellip; <br \/> <a class=\"read-more\" href=\"http:\/\/www.jiayus.com\/?p=3326\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3326","post","type-post","status-publish","format-standard","hentry","category-gsxw"],"_links":{"self":[{"href":"http:\/\/www.jiayus.com\/index.php?rest_route=\/wp\/v2\/posts\/3326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.jiayus.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jiayus.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jiayus.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jiayus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3326"}],"version-history":[{"count":1,"href":"http:\/\/www.jiayus.com\/index.php?rest_route=\/wp\/v2\/posts\/3326\/revisions"}],"predecessor-version":[{"id":3327,"href":"http:\/\/www.jiayus.com\/index.php?rest_route=\/wp\/v2\/posts\/3326\/revisions\/3327"}],"wp:attachment":[{"href":"http:\/\/www.jiayus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jiayus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3326"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jiayus.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}