{"id":48396,"date":"2024-12-02T16:13:08","date_gmt":"2024-12-02T08:13:08","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48396\/"},"modified":"2024-12-02T16:13:08","modified_gmt":"2024-12-02T08:13:08","slug":"php-%e5%87%bd%e6%95%b0%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e4%b8%8e%e9%98%9f%e5%88%97%e5%a4%84%e7%90%86%e7%9a%84%e5%8d%8f%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48396\/","title":{"rendered":"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7684\u534f\u4f5c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7684\u534f\u4f5c<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7684\u534f\u4f5c\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730023171671e0f0393c3c.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7684\u534f\u4f5c\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7684\u534f\u4f5c\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7684\u534f\u4f5c<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u51fd\u6570\u5f02\u5e38\u5904\u7406\u901a\u8fc7 <code>try-catch<\/code> \u673a\u5236\u5904\u7406\u51fd\u6570\u6267\u884c\u671f\u95f4\u53d1\u751f\u7684\u9519\u8bef\uff0c\u800c\u961f\u5217\u5904\u7406\u5141\u8bb8\u5f02\u6b65\u5904\u7406\u5927\u91cf\u4efb\u52a1\u3002\u7ed3\u5408\u8fd9\u4e24\u4e2a\u673a\u5236\uff0c\u53ef\u4ee5\u6253\u9020\u4e00\u4e2a\u5065\u58ee\u4e14\u53ef\u6269\u5c55\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u51fd\u6570\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p><code>try<\/code> \u5757\u7528\u4e8e\u6267\u884c\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u800c <code>catch<\/code> \u5757\u5219\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>try {\n    \/\/ \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n} catch (\\Exception $e) {\n    \/\/ \u5904\u7406\u5f02\u5e38\n}<\/pre>\n<p><strong>\u961f\u5217\u5904\u7406<\/strong><\/p>\n<p>\u961f\u5217\u5904\u7406\u53ef\u4ee5\u5c06\u4efb\u52a1\u5206\u914d\u7ed9\u4e0d\u540c\u7684\u5de5\u4f5c\u8fdb\u7a0b\u5f02\u6b65\u6267\u884c\u3002PHP \u6846\u67b6 Laravel \u63d0\u4f9b\u4e86 <code>Queue<\/code> \u95e8\u9762\uff0c\u7528\u4e8e\u8f7b\u677e\u7ba1\u7406\u961f\u5217\uff1a<\/p>\n<pre>use Illuminate\\Queue\\QueueManager;\n\n$queueManager = new QueueManager();\n$queue = $queueManager-&gt;connection('redis');<\/pre>\n<p><strong>\u534f\u4f5c<\/strong><\/p>\n<p>\u5c06\u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7ed3\u5408\u8d77\u6765\uff0c\u53ef\u4ee5\u521b\u5efa\u80fd\u591f\u5f02\u6b65\u5904\u7406\u5f02\u5e38\u5e76\u786e\u4fdd\u4efb\u52a1\u5b8c\u6210\u7684\u5065\u58ee\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u7684\u51fd\u6570\uff0c\u4f46\u53ef\u80fd\u7531\u4e8e\u7f51\u7edc\u8fde\u63a5\u95ee\u9898\u800c\u5931\u8d25\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u51fd\u6570\u5305\u88f9\u5728 <code>try-catch<\/code> \u5757\u4e2d\uff0c\u5e76\u5728\u5f02\u5e38\u53d1\u751f\u65f6\u5c06\u7535\u5b50\u90ae\u4ef6\u53d1\u9001\u5230\u961f\u5217\u4e2d\uff1a<\/p>\n<pre>try {\n    \/\/ \u53d1\u9001\u7535\u5b50\u90ae\u4ef6\n} catch (\\Exception $e) {\n    \/\/ \u5c06\u7535\u5b50\u90ae\u4ef6\u63a8\u9001\u5230\u961f\u5217\n    $queue-&gt;push(new SendEmailJob($data));\n}<\/pre>\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u5373\u4f7f\u7535\u5b50\u90ae\u4ef6\u65e0\u6cd5\u7acb\u5373\u53d1\u9001\uff0c\u4e5f\u4f1a\u5728\u961f\u5217\u4e2d\u6392\u961f\uff0c\u5e76\u6700\u7ec8\u7531\u5de5\u4f5c\u8fdb\u7a0b\u91cd\u8bd5\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u5c06\u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u534f\u4f5c\u8d77\u6765\uff0cPHP \u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u6784\u5efa\u80fd\u591f\u9ad8\u6548\u5904\u7406\u5f02\u5e38\u3001\u786e\u4fdd\u4efb\u52a1\u5b8c\u6210\u5e76\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u5065\u58ee\u6027\u7684\u7cfb\u7edf\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7684\u534f\u4f5c\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7684\u534f\u4f5c \u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7684\u534f\u4f5c\u300b\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a\u6587\u7ae0\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~ PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7684\u534f\u4f5c \u7b80\u4ecb \u5728 PHP \u4e2d\uff0c\u51fd\u6570\u5f02\u5e38\u5904\u7406\u901a\u8fc7 try-catch \u673a\u5236\u5904\u7406\u51fd\u6570\u6267\u884c\u671f\u95f4\u53d1\u751f\u7684\u9519\u8bef\uff0c\u800c\u961f\u5217\u5904\u7406\u5141\u8bb8\u5f02\u6b65\u5904\u7406\u5927\u91cf\u4efb\u52a1\u3002\u7ed3\u5408\u8fd9\u4e24\u4e2a\u673a\u5236\uff0c\u53ef\u4ee5\u6253\u9020\u4e00\u4e2a\u5065\u58ee\u4e14\u53ef\u6269\u5c55\u7684\u5e94\u7528\u7a0b\u5e8f\u3002 \u51fd\u6570\u5f02\u5e38\u5904\u7406 try \u5757\u7528\u4e8e\u6267\u884c\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u800c catch \u5757\u5219\u7528\u4e8e\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a try { \/\/ \u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801 } catch (\\Exception $e) { \/\/ \u5904\u7406\u5f02\u5e38 } \u961f\u5217\u5904\u7406 \u961f\u5217\u5904\u7406\u53ef\u4ee5\u5c06\u4efb\u52a1\u5206\u914d\u7ed9\u4e0d\u540c\u7684\u5de5\u4f5c\u8fdb\u7a0b\u5f02\u6b65\u6267\u884c\u3002PHP \u6846\u67b6 Laravel \u63d0\u4f9b\u4e86 Queue \u95e8\u9762\uff0c\u7528\u4e8e\u8f7b\u677e\u7ba1\u7406\u961f\u5217\uff1a use Illuminate\\Queue\\QueueManager; $queueManager = new QueueManager(); $queue = $queueManager-&gt;connection(&#8216;redis&#8217;); \u534f\u4f5c \u5c06\u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u961f\u5217\u5904\u7406\u7ed3\u5408\u8d77\u6765\uff0c\u53ef\u4ee5\u521b\u5efa\u80fd\u591f\u5f02\u6b65\u5904\u7406\u5f02\u5e38\u5e76\u786e\u4fdd\u4efb\u52a1\u5b8c\u6210\u7684\u5065\u58ee\u5e94\u7528\u7a0b\u5e8f\u3002 \u5b9e\u6218\u6848\u4f8b \u5047\u8bbe\u6709\u4e00\u4e2a\u53d1\u9001\u7535\u5b50\u90ae\u4ef6\u7684\u51fd\u6570\uff0c\u4f46\u53ef\u80fd\u7531\u4e8e\u7f51\u7edc\u8fde\u63a5\u95ee\u9898\u800c\u5931\u8d25\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u51fd\u6570\u5305\u88f9\u5728 try-catch \u5757\u4e2d\uff0c\u5e76\u5728\u5f02\u5e38\u53d1\u751f\u65f6\u5c06\u7535\u5b50\u90ae\u4ef6\u53d1\u9001\u5230\u961f\u5217\u4e2d\uff1a try { \/\/ \u53d1\u9001\u7535\u5b50\u90ae\u4ef6 [&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-48396","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48396","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=48396"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48396\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}