{"id":24891,"date":"2024-11-21T08:30:25","date_gmt":"2024-11-21T00:30:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24891\/"},"modified":"2024-11-21T08:30:25","modified_gmt":"2024-11-21T00:30:25","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8laravel%e5%ae%9e%e7%8e%b0%e7%9f%ad%e4%bf%a1%e5%8f%91%e9%80%81%e5%92%8c%e6%8e%a5%e6%94%b6%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24891\/","title":{"rendered":"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u548c\u63a5\u6536\u529f\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169900085550839.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u548c\u63a5\u6536\u529f\u80fd\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u548c\u63a5\u6536\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u548c\u63a5\u6536\u529f\u80fd\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>Laravel\u662f\u4e00\u4e2a\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5404\u79cd\u529f\u80fd\uff0c\u5305\u62ec\u77ed\u4fe1\u7684\u53d1\u9001\u548c\u63a5\u6536\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Laravel\u6846\u67b6\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u548c\u63a5\u6536\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u77ed\u4fe1\u53d1\u9001\u529f\u80fd\u7684\u5b9e\u73b0<\/p>\n<ol>\n<li>\u914d\u7f6e\u77ed\u4fe1\u670d\u52a1\u5546<\/li>\n<\/ol>\n<p>\u8981\u53d1\u9001\u77ed\u4fe1\uff0c\u9996\u5148\u9700\u8981\u914d\u7f6e\u77ed\u4fe1\u670d\u52a1\u5546\u3002\u5e38\u89c1\u7684\u77ed\u4fe1\u670d\u52a1\u5546\u6709\u963f\u91cc\u4e91\u3001\u817e\u8baf\u4e91\u7b49\uff0c\u8fd9\u91cc\u4ee5\u963f\u91cc\u4e91\u77ed\u4fe1\u4e3a\u4f8b\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<p>\u5728.env\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre>ALIYUN_ACCESS_KEY_ID=your_access_key_id\nALIYUN_ACCESS_KEY_SECRET=your_access_key_secret\nALIYUN_SMS_SIGN_NAME=your_sms_sign_name\nALIYUN_SMS_TEMPLATE_CODE=your_sms_template_code<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5c06your_access_key_id\u548cyour_access_key_secret\u66ff\u6362\u4e3a\u4f60\u7684\u963f\u91cc\u4e91\u7684AccessKey ID\u548cAccessKey Secret\uff1b\u5c06your_sms_sign_name\u66ff\u6362\u4e3a\u4f60\u7684\u77ed\u4fe1\u7b7e\u540d\u540d\u79f0\uff1b\u5c06your_sms_template_code\u66ff\u6362\u4e3a\u4f60\u7684\u77ed\u4fe1\u6a21\u677f\u4ee3\u7801\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u53d1\u9001\u77ed\u4fe1\u7684\u65b9\u6cd5<\/li>\n<\/ol>\n<p>\u5728app\/Http\/Controllers\u76ee\u5f55\u4e0b\u521b\u5efaSmsController.php\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php namespace AppHttpControllers;\n \nuse IlluminateHttpRequest;\nuse AlibabaCloudClientAlibabaCloud;\nuse AlibabaCloudClientExceptionClientException;\nuse AlibabaCloudClientExceptionServerException;\n \nclass SmsController extends Controller\n{\n    public function sendSms(Request $request)\n    {\n        $phoneNumber = $request-&gt;input('phone_number');\n        $code = $request-&amp;gt;input('code');\n \n        AlibabaCloud::accessKeyClient(\n            config('app.aliyun_access_key_id'),\n            config('app.aliyun_access_key_secret')\n        )\n        -&amp;gt;regionId('cn-hangzhou')\n        -&amp;gt;asDefaultClient();\n \n        try {\n            $result = AlibabaCloud::rpc()\n                                -&amp;gt;product('Dysmsapi')\n                                -&amp;gt;host('dysmsapi.aliyuncs.com')\n                                -&amp;gt;version('2017-05-25')\n                                -&amp;gt;action('SendSms')\n                                -&amp;gt;method('POST')\n                                -&amp;gt;options([\n                                    'query' =&amp;gt; [\n                                        'PhoneNumbers' =&amp;gt; $phoneNumber,\n                                        'SignName' =&amp;gt; config('app.aliyun_sms_sign_name'),\n                                        'TemplateCode' =&amp;gt; config('app.aliyun_sms_template_code'),\n                                        'TemplateParam' =&amp;gt; json_encode([\n                                            'code' =&amp;gt; $code,\n                                        ]),\n                                    ],\n                                ])\n                                -&amp;gt;request();\n \n            return response()-&amp;gt;json([\n                'message' =&amp;gt; 'SMS sent successfully',\n                'result' =&amp;gt; $result-&amp;gt;toArray(),\n            ]);\n        } catch (ClientException $e) {\n            return response()-&amp;gt;json([\n                'message' =&amp;gt; 'Client exception occurred',\n                'error' =&amp;gt; $e-&amp;gt;getErrorMessage(),\n            ], 500);\n        } catch (ServerException $e) {\n            return response()-&amp;gt;json([\n                'message' =&amp;gt; 'Server exception occurred',\n                'error' =&amp;gt; $e-&amp;gt;getErrorMessage(),\n            ], 500);\n        }\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u914d\u7f6e\u8def\u7531<\/li>\n<\/ol>\n<p>\u5728routes\/web.php\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>Route::post('\/sms\/send', 'SmsController@sendSms');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u53d1\u9001\u77ed\u4fe1<\/li>\n<\/ol>\n<p>\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001POST\u8bf7\u6c42\u5230\/sms\/send\u8def\u7531\u6765\u53d1\u9001\u77ed\u4fe1\u3002\u8bf7\u6c42\u53c2\u6570\u4e2d\u9700\u8981\u5305\u542bphone_number\u548ccode\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Postman\u5de5\u5177\u53d1\u9001\u4ee5\u4e0b\u8bf7\u6c42\uff1a<\/p>\n<pre>POST \/sms\/send HTTP\/1.1\nHost: your-domain.com\nContent-Type: application\/json\nAuthorization: Bearer your-token\nContent-Length: 68\n \n{\n    \"phone_number\": \"your-phone-number\",\n    \"code\": \"123456\"\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u793a\u4f8b\u4e2d\uff0c\u5c06your-domain.com\u66ff\u6362\u4e3a\u4f60\u7684\u57df\u540d\uff0cyour-token\u66ff\u6362\u4e3a\u4f60\u7684\u8ba4\u8bc1\u4ee4\u724c\uff0cyour-phone-number\u66ff\u6362\u4e3a\u8981\u63a5\u6536\u77ed\u4fe1\u7684\u624b\u673a\u53f7\u3002<\/p>\n<p>\u4e8c\u3001\u77ed\u4fe1\u63a5\u6536\u529f\u80fd\u7684\u5b9e\u73b0<\/p>\n<p>\u8981\u5b9e\u73b0\u77ed\u4fe1\u63a5\u6536\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u77ed\u4fe1\u5e73\u53f0\u63d0\u4f9b\u7684API\u63a5\u53e3\u3002\u8fd9\u91cc\u4ee5\u4e91\u7247\u7f51\u4e3a\u4f8b\u8fdb\u884c\u8bb2\u89e3\u3002<\/p>\n<ol>\n<li>\u6ce8\u518c\u4e91\u7247\u7f51\u8d26\u53f7<\/li>\n<\/ol>\n<p>\u9996\u5148\u9700\u8981\u5728\u4e91\u7247\u7f51\u4e0a\u6ce8\u518c\u8d26\u53f7\uff0c\u7136\u540e\u767b\u5f55\u5e76\u83b7\u53d6API key\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u63a5\u6536\u77ed\u4fe1\u7684\u65b9\u6cd5<\/li>\n<\/ol>\n<p>\u5728app\/Http\/Controllers\u76ee\u5f55\u4e0b\u521b\u5efaSmsController.php\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php namespace AppHttpControllers;\n \nuse IlluminateHttpRequest;\nuse IlluminateSupportFacadesHttp;\n \nclass SmsController extends Controller\n{\n    public function receiveSms(Request $request)\n    {\n        $content = $request-&gt;input('content');\n        $phoneNumber = $request-&amp;gt;input('phone_number');\n \n        \/\/ \u5904\u7406\u77ed\u4fe1\u5185\u5bb9\u7684\u903b\u8f91\n \n        \/\/ \u8fd4\u56de\u54cd\u5e94\n        return response('success');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u914d\u7f6e\u8def\u7531<\/li>\n<\/ol>\n<p>\u5728routes\/web.php\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>Route::post('\/sms\/receive', 'SmsController@receiveSms');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u63a5\u6536\u77ed\u4fe1<\/li>\n<\/ol>\n<p>\u53ef\u4ee5\u901a\u8fc7\u53d1\u9001POST\u8bf7\u6c42\u5230\/sms\/receive\u8def\u7531\u6765\u63a5\u6536\u77ed\u4fe1\u3002\u8bf7\u6c42\u53c2\u6570\u4e2d\u9700\u8981\u5305\u542bcontent\u548cphone_number\u53c2\u6570\u3002\u5177\u4f53\u7684\u77ed\u4fe1\u5185\u5bb9\u5904\u7406\u903b\u8f91\u9700\u8981\u6839\u636e\u63a5\u53e3\u6587\u6863\u6765\u8fdb\u884c\u7f16\u5199\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u63a5\u6536\u5230\u7684\u77ed\u4fe1\u5185\u5bb9\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7b2c\u4e09\u65b9API\u63a5\u53e3\u6765\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5229\u7528Laravel\u6846\u67b6\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u548c\u63a5\u6536\u529f\u80fd\u7684\u5177\u4f53\u65b9\u6cd5\u548c\u4ee3\u7801\u793a\u4f8b\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u4fee\u6539\u548c\u6269\u5c55\u3002\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5229\u7528Laravel\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u548c\u63a5\u6536\u529f\u80fd\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\u5229\u7528Laravel\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u548c\u63a5\u6536\u529f\u80fd\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b Laravel\u662f\u4e00\u4e2a\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u901a\u8fc7\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5404\u79cd\u529f\u80fd\uff0c\u5305\u62ec\u77ed\u4fe1\u7684\u53d1\u9001\u548c\u63a5\u6536\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Laravel\u6846\u67b6\u5b9e\u73b0\u77ed\u4fe1\u53d1\u9001\u548c\u63a5\u6536\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u77ed\u4fe1\u53d1\u9001\u529f\u80fd\u7684\u5b9e\u73b0 \u914d\u7f6e\u77ed\u4fe1\u670d\u52a1\u5546 \u8981\u53d1\u9001\u77ed\u4fe1\uff0c\u9996\u5148\u9700\u8981\u914d\u7f6e\u77ed\u4fe1\u670d\u52a1\u5546\u3002\u5e38\u89c1\u7684\u77ed\u4fe1\u670d\u52a1\u5546\u6709\u963f\u91cc\u4e91\u3001\u817e\u8baf\u4e91\u7b49\uff0c\u8fd9\u91cc\u4ee5\u963f\u91cc\u4e91\u77ed\u4fe1\u4e3a\u4f8b\u8fdb\u884c\u8bf4\u660e\u3002 \u5728.env\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a ALIYUN_ACCESS_KEY_ID=your_access_key_id ALIYUN_ACCESS_KEY_SECRET=your_access_key_secret ALIYUN_SMS_SIGN_NAME=your_sms_sign_name ALIYUN_SMS_TEMPLATE_CODE=your_sms_template_code \u767b\u5f55\u540e\u590d\u5236 \u5c06your_access_key_id\u548cyour_access_key_secret\u66ff\u6362\u4e3a\u4f60\u7684\u963f\u91cc\u4e91\u7684AccessKey ID\u548cAccessKey Secret\uff1b\u5c06your_sms_sign_name\u66ff\u6362\u4e3a\u4f60\u7684\u77ed\u4fe1\u7b7e\u540d\u540d\u79f0\uff1b\u5c06your_sms_template_code\u66ff\u6362\u4e3a\u4f60\u7684\u77ed\u4fe1\u6a21\u677f\u4ee3\u7801\u3002 \u521b\u5efa\u53d1\u9001\u77ed\u4fe1\u7684\u65b9\u6cd5 \u5728app\/Http\/Controllers\u76ee\u5f55\u4e0b\u521b\u5efaSmsController.php\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a &lt;?php namespace AppHttpControllers; use IlluminateHttpRequest; use AlibabaCloudClientAlibabaCloud; use AlibabaCloudClientExceptionClientException; use AlibabaCloudClientExceptionServerException; class SmsController extends Controller { public function sendSms(Request $request) { $phoneNumber = $request-&gt;input(&#8216;phone_number&#8217;); $code = $request-&amp;gt;input(&#8216;code&#8217;); AlibabaCloud::accessKeyClient( config(&#8216;app.aliyun_access_key_id&#8217;), config(&#8216;app.aliyun_access_key_secret&#8217;) ) -&amp;gt;regionId(&#8216;cn-hangzhou&#8217;) -&amp;gt;asDefaultClient(); try { $result = AlibabaCloud::rpc() -&amp;gt;product(&#8216;Dysmsapi&#8217;) [&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-24891","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24891","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=24891"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24891\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}