{"id":57200,"date":"2025-03-01T08:42:47","date_gmt":"2025-03-01T00:42:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/57200\/"},"modified":"2025-03-01T08:42:47","modified_gmt":"2025-03-01T00:42:47","slug":"%e4%bd%bf%e7%94%a8php%e6%93%8d%e4%bd%9cvultr-api%e7%9a%84%e5%85%a8%e9%9d%a2%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/57200\/","title":{"rendered":"\u4f7f\u7528PHP\u64cd\u4f5cVultr API\u7684\u5168\u9762\u6307\u5357"},"content":{"rendered":"<h2>\u4ec0\u4e48\u662fVultr API\uff1f<\/h2>\n<p>Vultr API\u662fVultr\u63d0\u4f9b\u7684\u4e00\u5957RESTful\u63a5\u53e3\uff0c\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u7f16\u7a0b\u65b9\u5f0f\u7ba1\u7406\u5176\u4e91\u8ba1\u7b97\u8d44\u6e90\u3002\u901a\u8fc7Vultr API\uff0c\u7528\u6237\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5316\u90e8\u7f72\u3001\u8d44\u6e90\u76d1\u63a7\u7b49\u529f\u80fd\uff0c\u4ece\u800c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<h2>\u4e3a\u4ec0\u4e48\u9009\u62e9PHP\uff1f<\/h2>\n<p>PHP\u662f\u4e00\u79cd\u6d41\u884c\u7684\u670d\u52a1\u5668\u7aef\u811a\u672c\u8bed\u8a00\uff0c\u5e7f\u6cdb\u7528\u4e8eWeb\u5f00\u53d1\u3002\u9009\u62e9PHP\u6765\u8c03\u7528Vultr API\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\uff1a<\/p>\n<ul>\n<li><strong>\u6613\u4e8e\u5b66\u4e60<\/strong>\uff1a\u5bf9\u4e8e\u5927\u591a\u6570\u5f00\u53d1\u8005\u800c\u8a00\uff0cPHP\u8bed\u8a00\u7b80\u5355\u6613\u5b66\uff0c\u5165\u95e8\u5feb\u3002<\/li>\n<li><strong>\u793e\u533a\u652f\u6301<\/strong>\uff1aPHP\u62e5\u6709\u5e9e\u5927\u7684\u5f00\u53d1\u8005\u793e\u533a\uff0c\u9047\u5230\u95ee\u9898\u65f6\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u89e3\u51b3\u65b9\u6848\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027<\/strong>\uff1aPHP\u53ef\u4ee5\u4e0e\u591a\u79cd\u6570\u636e\u5e93\u53ca\u670d\u52a1\u517c\u5bb9\uff0c\u9002\u5408\u6784\u5efa\u52a8\u6001\u7f51\u9875\u3002<\/li>\n<\/ul>\n<h2>Vultr API\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5728\u6df1\u5165\u5177\u4f53\u7684PHP\u5b9e\u73b0\u4e4b\u524d\uff0c\u4e86\u89e3\u4e00\u4e9bVultr API\u7684\u57fa\u672c\u6982\u5ff5\u975e\u5e38\u91cd\u8981\uff1a<\/p>\n<ul>\n<li><strong>API\u5bc6\u94a5<\/strong>\uff1a\u4f7f\u7528Vultr API\u7684\u7b2c\u4e00\u6b65\u662f\u751f\u6210\u4e00\u4e2aAPI\u5bc6\u94a5\uff0c\u8fd9\u4e2a\u5bc6\u94a5\u7528\u4e8e\u8eab\u4efd\u9a8c\u8bc1\u3002<\/li>\n<li><strong>API\u7aef\u70b9<\/strong>\uff1aVultr API\u7684\u6bcf\u4e2a\u529f\u80fd\u90fd\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u7aef\u70b9\uff0c\u901a\u8fc7\u8fd9\u4e2a\u7aef\u70b9\u53ef\u4ee5\u53d1\u8d77\u8bf7\u6c42\u3002<\/li>\n<li><strong>\u8bf7\u6c42\u7c7b\u578b<\/strong>\uff1aVultr API\u4e3b\u8981\u652f\u6301GET\u3001POST\u3001DELETE\u7b49\u8bf7\u6c42\u65b9\u5f0f\uff0c\u7528\u4e8e\u83b7\u53d6\u4fe1\u606f\u6216\u8fdb\u884c\u8d44\u6e90\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h2>\u5982\u4f55\u83b7\u53d6Vultr API\u5bc6\u94a5\uff1f<\/h2>\n<ol>\n<li>\u767b\u5f55\u5230Vultr\u63a7\u5236\u53f0\u3002<\/li>\n<li>\u5728\u4fa7\u8fb9\u680f\u4e2d\u627e\u5230\u201cAPI\u201d\u9009\u9879\u3002<\/li>\n<li>\u751f\u6210\u4e00\u4e2a\u65b0\u7684API\u5bc6\u94a5\uff0c\u5e76\u4fdd\u5b58\u597d\u3002<\/li>\n<\/ol>\n<h2>\u4f7f\u7528PHP\u8c03\u7528Vultr API\u7684\u6b65\u9aa4<\/h2>\n<h3>1. \u5b89\u88c5PHP\u7684cURL\u6269\u5c55<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fddPHP\u73af\u5883\u4e2d\u5b89\u88c5\u4e86cURL\u6269\u5c55\u3002cURL\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u662f\u4e0eVultr API\u4ea4\u4e92\u7684\u5fc5\u8981\u5de5\u5177\u3002<\/p>\n<h3>2. \u521b\u5efa\u57fa\u672c\u7684\u8bf7\u6c42\u51fd\u6570<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684PHP\u51fd\u6570\uff0c\u7528\u4e8e\u5411Vultr API\u53d1\u9001GET\u8bf7\u6c42\uff1a php function callVultrAPI($endpoint, $method = \u2018GET\u2019, $data = null) { $apiKey = \u2018\u4f60\u7684API\u5bc6\u94a5\u2019; \/\/ \u66ff\u6362\u4e3a\u4f60\u7684API\u5bc6\u94a5 $url = \u2018https:\/\/api.vultr.com\/v2\/\u2019 . $endpoint;<\/p>\n<pre><code>$ch = curl_init();\ncurl_setopt($ch, CURLOPT_URL, $url);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\ncurl_setopt($ch, CURLOPT_HTTPHEADER, [\n    'Authorization: Bearer ' . $apiKey,\n    'Content-Type: application\/json'\n]);\n\nif ($method == 'POST') {\n    curl_setopt($ch, CURLOPT_POST, true);\n    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));\n}\n\n$response = curl_exec($ch);\ncurl_close($ch);\nreturn json_decode($response, true);}\n<\/code><\/pre>\n<h3>3. \u53d1\u9001API\u8bf7\u6c42<\/h3>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u53d1\u9001\u8bf7\u6c42\u7684\u793a\u4f8b\uff1a php $response = callVultrAPI(\u2018instances\/list\u2019); print_r($response);<\/p>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c06\u4f1a\u8fd4\u56de\u5f53\u524d\u6240\u6709\u5b9e\u4f8b\u7684\u5217\u8868\u3002<\/p>\n<h2>\u5e38\u7528\u7684Vultr API\u8bf7\u6c42\u793a\u4f8b<\/h2>\n<h3>\u521b\u5efa\u5b9e\u4f8b<\/h3>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Vultr\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a php $data = [ \u2018label\u2019 =&gt; \u2018My Instance\u2019, \u2018region\u2019 =&gt; \u2018ewr\u2019, \/\/ \u9009\u62e9\u533a\u57df \u2018plan\u2019 =&gt; \u2018vc2-1c-1gb\u2019, \/\/ \u9009\u62e9\u65b9\u6848 \u2018os_id\u2019 =&gt; 215, \/\/ \u9009\u62e9\u64cd\u4f5c\u7cfb\u7edf ]; $response = callVultrAPI(\u2018instances\/create\u2019, \u2018POST\u2019, $data); print_r($response);<\/p>\n<h3>\u5220\u9664\u5b9e\u4f8b<\/h3>\n<p>\u5220\u9664\u4e00\u4e2a\u5b9e\u4f8b\u7684\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a php $instanceId = \u2018\u4f60\u7684\u5b9e\u4f8bID\u2019; $response = callVultrAPI(\u2018instances\/destroy\u2019, \u2018POST\u2019, [\u2018instance_id\u2019 =&gt; $instanceId]); print_r($response);<\/p>\n<h2>\u5e38\u89c1\u95ee\u9898\u89e3\u7b54\uff08FAQ\uff09<\/h2>\n<h3>1. \u5982\u4f55\u5904\u7406Vultr API\u7684\u9519\u8bef\uff1f<\/h3>\n<p>\u5982\u679c\u8bf7\u6c42\u5931\u8d25\uff0cVultr API\u4f1a\u8fd4\u56de\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u3002\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u54cd\u5e94\u4e2d\u7684<code>error<\/code>\u5b57\u6bb5\u6765\u83b7\u53d6\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<h3>2. Vultr API\u7684\u8c03\u7528\u9891\u7387\u9650\u5236\u662f\u591a\u5c11\uff1f<\/h3>\n<p>Vultr API\u5bf9\u8bf7\u6c42\u9891\u7387\u6709\u9650\u5236\uff0c\u901a\u5e38\u4e3a\u6bcf\u79d2\u949f10\u4e2a\u8bf7\u6c42\u3002\u8d85\u51fa\u9650\u5236\u540e\u53ef\u80fd\u4f1a\u6536\u5230429\u9519\u8bef\u3002<\/p>\n<h3>3. \u662f\u5426\u9700\u8981HTTPS\u8bf7\u6c42\uff1f<\/h3>\n<p>\u662f\u7684\uff0cVultr API\u8981\u6c42\u6240\u6709\u8bf7\u6c42\u4f7f\u7528HTTPS\u534f\u8bae\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u6027\u3002<\/p>\n<h3>4. \u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u8bed\u8a00\u8c03\u7528Vultr API\u5417\uff1f<\/h3>\n<p>\u5f53\u7136\uff0cVultr API\u662f\u57fa\u4e8eREST\u7684\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ea\u8981\u80fd\u591f\u53d1\u9001HTTP\u8bf7\u6c42\u5373\u53ef\u3002<\/p>\n<h2>\u7ed3\u8bed<\/h2>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528PHP\u8c03\u7528Vultr API\uff0c\u5305\u62ec\u83b7\u53d6API\u5bc6\u94a5\u3001\u57fa\u672c\u8bf7\u6c42\u65b9\u6cd5\u4ee5\u53ca\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\u793a\u4f8b\u3002\u638c\u63e1\u8fd9\u4e9b\u5185\u5bb9\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u901a\u8fc7\u7f16\u7a0b\u7684\u65b9\u5f0f\u9ad8\u6548\u7ba1\u7406\u4f60\u7684Vultr\u4e91\u8ba1\u7b97\u8d44\u6e90\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662fVultr API\uff1f Vultr API\u662fVultr\u63d0\u4f9b\u7684\u4e00\u5957RESTful\u63a5\u53e3\uff0c\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u7f16\u7a0b\u65b9\u5f0f\u7ba1\u7406\u5176\u4e91\u8ba1\u7b97\u8d44\u6e90\u3002\u901a\u8fc7Vultr API\uff0c\u7528\u6237\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u5316\u90e8\u7f72\u3001\u8d44\u6e90\u76d1\u63a7\u7b49\u529f\u80fd\uff0c\u4ece\u800c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002 \u4e3a\u4ec0\u4e48\u9009\u62e9PHP\uff1f PHP\u662f\u4e00\u79cd\u6d41\u884c\u7684\u670d\u52a1\u5668\u7aef\u811a\u672c\u8bed\u8a00\uff0c\u5e7f\u6cdb\u7528\u4e8eWeb\u5f00\u53d1\u3002\u9009\u62e9PHP\u6765\u8c03\u7528Vultr API\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf\uff1a \u6613\u4e8e\u5b66\u4e60\uff1a\u5bf9\u4e8e\u5927\u591a\u6570\u5f00\u53d1\u8005\u800c\u8a00\uff0cPHP\u8bed\u8a00\u7b80\u5355\u6613\u5b66\uff0c\u5165\u95e8\u5feb\u3002 \u793e\u533a\u652f\u6301\uff1aPHP\u62e5\u6709\u5e9e\u5927\u7684\u5f00\u53d1\u8005\u793e\u533a\uff0c\u9047\u5230\u95ee\u9898\u65f6\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u89e3\u51b3\u65b9\u6848\u3002 \u517c\u5bb9\u6027\uff1aPHP\u53ef\u4ee5\u4e0e\u591a\u79cd\u6570\u636e\u5e93\u53ca\u670d\u52a1\u517c\u5bb9\uff0c\u9002\u5408\u6784\u5efa\u52a8\u6001\u7f51\u9875\u3002 Vultr API\u7684\u57fa\u672c\u6982\u5ff5 \u5728\u6df1\u5165\u5177\u4f53\u7684PHP\u5b9e\u73b0\u4e4b\u524d\uff0c\u4e86\u89e3\u4e00\u4e9bVultr API\u7684\u57fa\u672c\u6982\u5ff5\u975e\u5e38\u91cd\u8981\uff1a API\u5bc6\u94a5\uff1a\u4f7f\u7528Vultr API\u7684\u7b2c\u4e00\u6b65\u662f\u751f\u6210\u4e00\u4e2aAPI\u5bc6\u94a5\uff0c\u8fd9\u4e2a\u5bc6\u94a5\u7528\u4e8e\u8eab\u4efd\u9a8c\u8bc1\u3002 API\u7aef\u70b9\uff1aVultr API\u7684\u6bcf\u4e2a\u529f\u80fd\u90fd\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684\u7aef\u70b9\uff0c\u901a\u8fc7\u8fd9\u4e2a\u7aef\u70b9\u53ef\u4ee5\u53d1\u8d77\u8bf7\u6c42\u3002 \u8bf7\u6c42\u7c7b\u578b\uff1aVultr API\u4e3b\u8981\u652f\u6301GET\u3001POST\u3001DELETE\u7b49\u8bf7\u6c42\u65b9\u5f0f\uff0c\u7528\u4e8e\u83b7\u53d6\u4fe1\u606f\u6216\u8fdb\u884c\u8d44\u6e90\u64cd\u4f5c\u3002 \u5982\u4f55\u83b7\u53d6Vultr API\u5bc6\u94a5\uff1f \u767b\u5f55\u5230Vultr\u63a7\u5236\u53f0\u3002 \u5728\u4fa7\u8fb9\u680f\u4e2d\u627e\u5230\u201cAPI\u201d\u9009\u9879\u3002 \u751f\u6210\u4e00\u4e2a\u65b0\u7684API\u5bc6\u94a5\uff0c\u5e76\u4fdd\u5b58\u597d\u3002 \u4f7f\u7528PHP\u8c03\u7528Vultr API\u7684\u6b65\u9aa4 1. \u5b89\u88c5PHP\u7684cURL\u6269\u5c55 \u9996\u5148\uff0c\u9700\u8981\u786e\u4fddPHP\u73af\u5883\u4e2d\u5b89\u88c5\u4e86cURL\u6269\u5c55\u3002cURL\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u662f\u4e0eVultr API\u4ea4\u4e92\u7684\u5fc5\u8981\u5de5\u5177\u3002 2. \u521b\u5efa\u57fa\u672c\u7684\u8bf7\u6c42\u51fd\u6570 \u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684PHP\u51fd\u6570\uff0c\u7528\u4e8e\u5411Vultr API\u53d1\u9001GET\u8bf7\u6c42\uff1a php function callVultrAPI($endpoint, $method = \u2018GET\u2019, $data = null) { $apiKey = \u2018\u4f60\u7684API\u5bc6\u94a5\u2019; \/\/ \u66ff\u6362\u4e3a\u4f60\u7684API\u5bc6\u94a5 $url = \u2018https:\/\/api.vultr.com\/v2\/\u2019 . [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-57200","post","type-post","status-publish","format-standard","hentry","category-vpn"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/57200","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=57200"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/57200\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=57200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=57200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=57200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}