{"id":24736,"date":"2024-11-21T11:39:18","date_gmt":"2024-11-21T03:39:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24736\/"},"modified":"2024-11-21T11:39:18","modified_gmt":"2024-11-21T03:39:18","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8hyperf%e6%a1%86%e6%9e%b6%e8%bf%9b%e8%a1%8c%e7%9f%ad%e4%bf%a1%e5%8f%91%e9%80%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24736\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u77ed\u4fe1\u53d1\u9001"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169780061864708.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u77ed\u4fe1\u53d1\u9001\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u77ed\u4fe1\u53d1\u9001\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u77ed\u4fe1\u53d1\u9001<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u5728\u5f53\u4eca\u6570\u5b57\u5316\u65f6\u4ee3\uff0c\u77ed\u4fe1\u5df2\u7ecf\u6210\u4e3a\u4e86\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u7684\u6c9f\u901a\u5de5\u5177\u3002\u65e0\u8bba\u662f\u8fdb\u884c\u9a8c\u8bc1\u7801\u7684\u53d1\u9001\u8fd8\u662f\u6d3b\u52a8\u63a8\u5e7f\uff0c\u77ed\u4fe1\u90fd\u80fd\u8d77\u5230\u91cd\u8981\u7684\u4f5c\u7528\u3002\u800c\u5728\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u5982\u4f55\u65b9\u4fbf\u5730\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u529f\u80fd\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u7684\u95ee\u9898\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u77ed\u4fe1\u53d1\u9001\uff0c\u5e76\u9644\u4e0a\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<ol>\n<li>\u914d\u7f6eSMSService\uff1a<br \/>\u9996\u5148\uff0c\u5728Hyperf\u6846\u67b6\u4e2d\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6e\u4e00\u4e2aSMSService\u3002SMSService\u8d1f\u8d23\u628a\u77ed\u4fe1\u53d1\u9001\u5230\u76ee\u6807\u624b\u673a\u53f7\u7801\uff0c\u5e76\u83b7\u53d6\u53d1\u9001\u7ed3\u679c\u3002<\/li>\n<\/ol>\n<pre>&lt;?php namespace AppService;\n\nuse HyperfGuzzleClientFactory;\n\nclass SMSService\n{\n    protected $client;\n\n    public function __construct(ClientFactory $clientFactory)\n    {\n        $this-&gt;client = $clientFactory-&amp;gt;create();\n    }\n\n    public function sendSMS($mobile, $content)\n    {\n        $response = $this-&amp;gt;client-&amp;gt;post('https:\/\/api.example.com\/sms\/send', [\n            'json' =&amp;gt; [\n                'mobile' =&amp;gt; $mobile,\n                'content' =&amp;gt; $content\n            ]\n        ]);\n\n        $result = json_decode($response-&amp;gt;getBody(), true);\n\n        if ($result['code'] == 200) {\n            return true;\n        } else {\n            return false;\n        }\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7Guzzle HTTP\u5ba2\u6237\u7aef\u53d1\u9001POST\u8bf7\u6c42\u5230\u77ed\u4fe1\u63a5\u53e3\u3002\u63a5\u53e3\u5730\u5740\u4e3ahttps:\/\/api.example.com\/sms\/send\uff0c\u8bf7\u6c42\u53c2\u6570\u5305\u62ec\u624b\u673a\u53f7\u7801$mobile\u548c\u77ed\u4fe1\u5185\u5bb9$content\u3002\u53d1\u9001\u7ed3\u679c\u901a\u8fc7\u5224\u65ad\u63a5\u53e3\u8fd4\u56de\u7684JSON\u7ed3\u679c\u4e2d\u7684code\u5b57\u6bb5\u6765\u786e\u5b9a\u662f\u5426\u53d1\u9001\u6210\u529f\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528SMSService\u53d1\u9001\u77ed\u4fe1\uff1a<br \/>\u5728\u914d\u7f6e\u597dSMSService\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u9700\u8981\u53d1\u9001\u77ed\u4fe1\u7684\u4f4d\u7f6e\u4f7f\u7528\u5b83\u4e86\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8bController\u4ee3\u7801\uff0c\u7528\u6765\u6f14\u793a\u5982\u4f55\u8c03\u7528SMSService\u53d1\u9001\u77ed\u4fe1\u3002<\/li>\n<\/ol>\n<pre>&lt;?php namespace AppController;\n\nuse AppServiceSMSService;\nuse HyperfHttpServerAnnotationAutoController;\n\n\/**\n * @AutoController\n *\/\nclass SMSController extends AbstractController\n{\n    public function send(SMSService $smsService)\n    {\n        $mobile = $this-&gt;request-&amp;gt;input('mobile');\n        $content = $this-&amp;gt;request-&amp;gt;input('content');\n\n        $result = $smsService-&amp;gt;sendSMS($mobile, $content);\n\n        if ($result) {\n            return $this-&amp;gt;response-&amp;gt;success('\u77ed\u4fe1\u53d1\u9001\u6210\u529f');\n        } else {\n            return $this-&amp;gt;response-&amp;gt;error('\u77ed\u4fe1\u53d1\u9001\u5931\u8d25');\n        }\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7use\u5173\u952e\u5b57\u5f15\u5165\u4e86SMSService\uff0c\u5e76\u5728send\u65b9\u6cd5\u4e2d\u8fdb\u884c\u4e86\u5b9e\u4f8b\u5316\u3002\u83b7\u53d6\u8bf7\u6c42\u4e2d\u4f20\u9012\u7684\u624b\u673a\u53f7\u7801\u548c\u77ed\u4fe1\u5185\u5bb9\u540e\uff0c\u8c03\u7528SMSService\u7684sendSMS\u65b9\u6cd5\u8fdb\u884c\u77ed\u4fe1\u53d1\u9001\u3002\u6839\u636e\u53d1\u9001\u7ed3\u679c\u8fd4\u56de\u4e0d\u540c\u7684\u54cd\u5e94\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<br \/>\u901a\u8fc7\u4ee5\u4e0a\u7b80\u5355\u7684\u914d\u7f6e\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5728Hyperf\u6846\u67b6\u4e2d\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u529f\u80fd\u3002\u4f7f\u7528Hyperf\u6846\u67b6\u7684SMSService\u548cGuzzle HTTP\u5ba2\u6237\u7aef\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8c03\u7528\u77ed\u4fe1\u63a5\u53e3\u53d1\u9001\u77ed\u4fe1\uff0c\u63d0\u5347\u4e86\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9Hyperf\u6846\u67b6\u5f00\u53d1\u8005\u5728\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u529f\u80fd\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u77ed\u4fe1\u53d1\u9001\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u77ed\u4fe1\u53d1\u9001 \u5f15\u8a00\uff1a\u5728\u5f53\u4eca\u6570\u5b57\u5316\u65f6\u4ee3\uff0c\u77ed\u4fe1\u5df2\u7ecf\u6210\u4e3a\u4e86\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u7684\u6c9f\u901a\u5de5\u5177\u3002\u65e0\u8bba\u662f\u8fdb\u884c\u9a8c\u8bc1\u7801\u7684\u53d1\u9001\u8fd8\u662f\u6d3b\u52a8\u63a8\u5e7f\uff0c\u77ed\u4fe1\u90fd\u80fd\u8d77\u5230\u91cd\u8981\u7684\u4f5c\u7528\u3002\u800c\u5728\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5f00\u53d1\u65f6\uff0c\u5982\u4f55\u65b9\u4fbf\u5730\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u529f\u80fd\u662f\u4e00\u4e2a\u9700\u8981\u8003\u8651\u7684\u95ee\u9898\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u77ed\u4fe1\u53d1\u9001\uff0c\u5e76\u9644\u4e0a\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u914d\u7f6eSMSService\uff1a\u9996\u5148\uff0c\u5728Hyperf\u6846\u67b6\u4e2d\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u529f\u80fd\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6e\u4e00\u4e2aSMSService\u3002SMSService\u8d1f\u8d23\u628a\u77ed\u4fe1\u53d1\u9001\u5230\u76ee\u6807\u624b\u673a\u53f7\u7801\uff0c\u5e76\u83b7\u53d6\u53d1\u9001\u7ed3\u679c\u3002 &lt;?php namespace AppService; use HyperfGuzzleClientFactory; class SMSService { protected $client; public function __construct(ClientFactory $clientFactory) { $this-&gt;client = $clientFactory-&amp;gt;create(); } public function sendSMS($mobile, $content) { $response = $this-&amp;gt;client-&amp;gt;post(&#8216;https:\/\/api.example.com\/sms\/send&#8217;, [ &#8216;json&#8217; =&amp;gt; [ &#8216;mobile&#8217; =&amp;gt; $mobile, &#8216;content&#8217; =&amp;gt; $content ] ]); $result = json_decode($response-&amp;gt;getBody(), true); if ($result[&#8216;code&#8217;] == 200) { return true; } else [&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-24736","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24736","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=24736"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24736\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}