{"id":47629,"date":"2024-12-02T12:57:47","date_gmt":"2024-12-02T04:57:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47629\/"},"modified":"2024-12-02T12:57:47","modified_gmt":"2024-12-02T04:57:47","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%b8%8e%e5%a4%96%e9%83%a8-api-%e6%89%a9%e5%b1%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47629\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u4e0e\u5916\u90e8 API \u6269\u5c55\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u4e0e\u5916\u90e8 API \u6269\u5c55\uff1f<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e\u5916\u90e8 API \u6269\u5c55\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p>\u901a\u8fc7\u8c03\u7528\u5916\u90e8 API\uff0cPHP \u51fd\u6570\u53ef\u4ee5\u6269\u5c55\u5176\u529f\u80fd\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5 cURL \u5e93\uff1asudo apt-get install php-curl\u521b\u5efa PHP \u51fd\u6570\uff1a\u4f7f\u7528 curl_init() \u51fd\u6570\u521d\u59cb\u5316 cURL \u8fde\u63a5\u8c03\u7528 PHP \u51fd\u6570\uff1a\u4f7f\u7528 download(&#8216;url&#8217;, &#8216;filepath&#8217;) \u8bed\u6cd5\u8c03\u7528\u51fd\u6570<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729926628671c95e4ad636.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e\u5916\u90e8 API \u6269\u5c55\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e\u5916\u90e8 API \u6269\u5c55\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 PHP \u51fd\u6570\u4e0e\u5916\u90e8 API \u6269\u5c55<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHP \u51fd\u6570\u901a\u8fc7\u8c03\u7528\u5916\u90e8 API \u6765\u6269\u5c55\u5176\u529f\u80fd\uff0c\u4ece\u800c\u5141\u8bb8\u5f00\u53d1\u8005\u65e0\u7f1d\u5730\u96c6\u6210\u7b2c\u4e09\u65b9\u670d\u52a1\u548c\u6570\u636e\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u4e0e\u5916\u90e8 API \u4ee5\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\u529f\u80fd\u7684\u6b65\u9aa4\u3002<\/p>\n<p><strong>\u7b2c\u4e00\u6b65\uff1a\u5b89\u88c5 cURL \u5e93<\/strong><\/p>\n<p>cURL \u662f\u4e00\u4e2a\u7528\u4e8e\u4ece URL \u83b7\u53d6\u6570\u636e\u548c\u63d0\u4ea4\u8868\u683c\u6570\u636e\u7684\u5e93\u3002\u5b83\u652f\u6301\u591a\u79cd\u534f\u8bae\uff0c\u5305\u62ec HTTP\u3001HTTPS\u3001FTP \u548c FTPS\u3002\u8981\u5b89\u88c5 cURL \u5e93\uff0c\u8bf7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>sudo apt-get install php-curl<\/pre>\n<p><strong>\u7b2c\u4e8c\u6b65\uff1a\u521b\u5efa PHP \u51fd\u6570<\/strong><\/p>\n<p>\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <strong>curl_init()<\/strong> \u51fd\u6570\u521b\u5efa PHP \u51fd\u6570\u4ee5\u521d\u59cb\u5316 cURL \u8fde\u63a5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 cURL \u4e0b\u8f7d\u8fdc\u7a0b\u6587\u4ef6\u7684\u793a\u4f8b\u51fd\u6570\uff1a<\/p>\n<pre>function download($url, $filepath)\n{\n    $curl = curl_init();\n    curl_setopt($curl, CURLOPT_URL, $url);\n    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);\n    $data = curl_exec($curl);\n    curl_close($curl);\n    file_put_contents($filepath, $data);\n}<\/pre>\n<p><strong>\u7b2c\u4e09\u6b65\uff1a\u8c03\u7528 PHP \u51fd\u6570<\/strong><\/p>\n<p>\u8981\u8c03\u7528\u60a8\u521b\u5efa\u7684 PHP \u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>download('https:\/\/example.com\/file.txt', '\/tmp\/file.txt');<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 <strong>download()<\/strong> \u51fd\u6570\u4ece\u8fdc\u7a0b URL \u4e0b\u8f7d\u6587\u4ef6\u7684\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<pre>$url = 'https:\/\/example.com\/image.jpg';\n$filepath = '\/tmp\/image.jpg';\ndownload($url, $filepath);<\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u4e0b\u8f7d\u8fdc\u7a0b\u56fe\u50cf\u6587\u4ef6\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u670d\u52a1\u5668\u4e0a\u7684 <strong>\/tmp\/image.jpg<\/strong> \u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u7ed3\u675f<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 PHP \u51fd\u6570\u4e0e\u5916\u90e8 API \u6269\u5c55\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u96c6\u6210\u7b2c\u4e09\u65b9\u670d\u52a1\u548c\u6570\u636e\u5230\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002\u901a\u8fc7\u9075\u5faa\u672c\u6587\u4e2d\u7684\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684 PHP \u51fd\u6570\u6765\u4e0e\u5916\u90e8 API \u4ea4\u4e92\uff0c\u4ece\u800c\u6269\u5c55\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u529f\u80fd\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e\u5916\u90e8 API \u6269\u5c55\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8ephp,\u5916\u90e8API\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u4e0e\u5916\u90e8 API \u6269\u5c55\uff1f \u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e\u5916\u90e8 API \u6269\u5c55\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002 \u901a\u8fc7\u8c03\u7528\u5916\u90e8 API\uff0cPHP \u51fd\u6570\u53ef\u4ee5\u6269\u5c55\u5176\u529f\u80fd\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5b89\u88c5 cURL \u5e93\uff1asudo apt-get install php-curl\u521b\u5efa PHP \u51fd\u6570\uff1a\u4f7f\u7528 curl_init() \u51fd\u6570\u521d\u59cb\u5316 cURL \u8fde\u63a5\u8c03\u7528 PHP \u51fd\u6570\uff1a\u4f7f\u7528 download(&#8216;url&#8217;, &#8216;filepath&#8217;) \u8bed\u6cd5\u8c03\u7528\u51fd\u6570 \u4f7f\u7528 PHP \u51fd\u6570\u4e0e\u5916\u90e8 API \u6269\u5c55 \u7b80\u4ecb PHP \u51fd\u6570\u901a\u8fc7\u8c03\u7528\u5916\u90e8 API \u6765\u6269\u5c55\u5176\u529f\u80fd\uff0c\u4ece\u800c\u5141\u8bb8\u5f00\u53d1\u8005\u65e0\u7f1d\u5730\u96c6\u6210\u7b2c\u4e09\u65b9\u670d\u52a1\u548c\u6570\u636e\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u4e0e\u5916\u90e8 API \u4ee5\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\u529f\u80fd\u7684\u6b65\u9aa4\u3002 \u7b2c\u4e00\u6b65\uff1a\u5b89\u88c5 cURL \u5e93 cURL \u662f\u4e00\u4e2a\u7528\u4e8e\u4ece URL \u83b7\u53d6\u6570\u636e\u548c\u63d0\u4ea4\u8868\u683c\u6570\u636e\u7684\u5e93\u3002\u5b83\u652f\u6301\u591a\u79cd\u534f\u8bae\uff0c\u5305\u62ec HTTP\u3001HTTPS\u3001FTP \u548c FTPS\u3002\u8981\u5b89\u88c5 cURL \u5e93\uff0c\u8bf7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a sudo apt-get [&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-47629","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47629","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=47629"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47629\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}