{"id":23998,"date":"2024-11-21T15:25:32","date_gmt":"2024-11-21T07:25:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23998\/"},"modified":"2024-11-21T15:25:32","modified_gmt":"2024-11-21T07:25:32","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8swoole%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%8d%8f%e7%a8%8b%e5%ae%9e%e7%8e%b0%e9%ab%98%e5%b9%b6%e5%8f%91%e7%9a%84swoole_imap_fetch%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23998\/","title":{"rendered":"\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_imap_fetch\u51fd\u6570"},"content":{"rendered":"<p>\u662f\u4e00\u6b3e\u57fa\u4e8ephp\u7684\u5f02\u6b65\u3001\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u5730\u5b9e\u73b0\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u5e94\u7528\u3002\u800c\u534f\u7a0b\u5219\u662fswoole\u4e2d\u7684\u4e00\u79cd\u91cd\u8981\u6280\u672f\uff0c\u5728\u7f51\u7edc\u901a\u4fe1\u4e2d\u8d77\u5230\u4e86\u6781\u4e3a\u91cd\u8981\u7684\u4f5c\u7528\u3002\u672c\u6587\u5c06\u4e3b\u8981\u4ecb\u7ecd\u5982\u4f55\u5728swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_imap_fetch\u51fd\u6570\u3002<\/p>\n<p>Swoole_imap_fetch\u51fd\u6570\u662fSwoole\u4e2d\u7684\u4e00\u79cdIMAP\u7f51\u7edc\u534f\u8bae\uff0c\u5b9e\u73b0\u4e86\u5bf9\u8fdc\u7a0bIMAP\u670d\u52a1\u5668\u7684\u8bbf\u95ee\u548c\u901a\u4fe1\u3002\u4f7f\u7528swoole_imap_fetch\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u4ece\u90ae\u4ef6\u670d\u52a1\u5668\u4e0a\u83b7\u53d6\u90ae\u4ef6\uff0c\u4ee5\u53ca\u5bf9\u90ae\u4ef6\u7684\u89e3\u6790\u3001\u5206\u7c7b\u3001\u5b58\u50a8\u7b49\u64cd\u4f5c\u3002\u4f46\u662f\uff0c\u7531\u4e8e\u90ae\u4ef6\u670d\u52a1\u5668\u4e2d\u5b58\u5728\u5927\u91cf\u7684\u90ae\u4ef6\u6570\u636e\uff0c\u5982\u679c\u4f7f\u7528\u4f20\u7edf\u7684\u65b9\u5f0f\u5bf9\u90ae\u4ef6\u8fdb\u884c\u83b7\u53d6\u3001\u89e3\u6790\u7b49\u64cd\u4f5c\uff0c\u5bb9\u6613\u51fa\u73b0\u6027\u80fd\u74f6\u9888\uff0c\u5bfc\u81f4\u5e94\u7528\u7684\u54cd\u5e94\u901f\u5ea6\u53d8\u6162\uff0c\u7ed9\u7528\u6237\u5e26\u6765\u4e0d\u597d\u7684\u4f53\u9a8c\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Swoole\u4e2d\u7684\u534f\u7a0b\u6765\u63d0\u5347swoole_imap_fetch\u51fd\u6570\u7684\u6027\u80fd\uff0c\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u5728Swoole\u4e2d\u5f15\u5165\u534f\u7a0b\u5e93\uff0c\u5e76\u542f\u7528\u534f\u7a0b\u652f\u6301\u3002<\/li>\n<\/ol>\n<pre>co::set(['hook_flags' =&amp;gt; SWOOLE_HOOK_ALL]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u7136\u540e\uff0c\u5728\u8c03\u7528swoole_imap_fetch\u51fd\u6570\u4e4b\u524d\uff0c\u9700\u8981\u5bf9\u8be5\u51fd\u6570\u8fdb\u884c\u534f\u7a0b\u5316\u6539\u9020\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre>function swoole_imap_fetch_async($imap_stream, $msg_number, $options = 0) \n{ \n    return new AsyncImapFetch($imap_stream, $msg_number, $options); \n} \n\nclass AsyncImapFetch \n{ \n    private $imap_stream; \n    private $msg_number; \n    private $options; \n    private $deferred; \n\n    public function __construct($imap_stream, $msg_number, $options = 0) \n    { \n        $this-&amp;gt;imap_stream = $imap_stream; \n        $this-&amp;gt;msg_number = $msg_number; \n        $this-&amp;gt;options = $options; \n        $this-&amp;gt;deferred = new SwooleCoroutineChannel(1); \n        SwooleCoroutine::create([$this, 'execute']); \n    } \n\n    public function execute() \n    { \n        $result = swoole_coroutine::sleep(1); \/\/ \u6a21\u62df\u7f51\u7edcIO\u7b49\u5f85 \n        $ret = swoole_imap_fetch($this-&amp;gt;imap_stream, $this-&amp;gt;msg_number, $this-&amp;gt;options); \n        $this-&amp;gt;deferred-&amp;gt;push($ret); \n    } \n\n    public function getResult() \n    { \n        return $this-&amp;gt;deferred-&amp;gt;pop(); \n    } \n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u6700\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u8c03\u7528swoole_imap_fetch_async\u51fd\u6570\uff0c\u5176\u4e2d\u8c03\u7528execute\u51fd\u6570\u7684\u5730\u65b9\u4f1a\u81ea\u52a8\u5f00\u542f\u534f\u7a0b\u6267\u884c\uff0c\u5b8c\u6210imap_fetch\u7684\u5f02\u6b65\u5904\u7406\u3002<\/li>\n<\/ol>\n<pre>$imap_stream = imap_open('{imap.xxx.com:993\/imap\/ssl}INBOX', 'user', 'pass'); \n\n\/\/ \u5f02\u6b65\u83b7\u53d6\u90ae\u4ef6\u4fe1\u606f \n$async_fetch = swoole_imap_fetch_async($imap_stream, 1, FT_UID); \n\n\/\/ \u5176\u4ed6\u64cd\u4f5c \n\/\/ ... \n\n$ret = $async_fetch-&amp;gt;getResult(); \/\/ \u83b7\u53d6\u83b7\u53d6\u90ae\u4ef6\u7ed3\u679c \n\nimap_close($imap_stream); \n\nprint_r($ret); \/\/ \u8f93\u51fa\u83b7\u53d6\u7684\u7ed3\u679c <\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cswoole_imap_fetch_async\u51fd\u6570\u5bf9swoole_imap_fetch\u51fd\u6570\u8fdb\u884c\u4e86\u534f\u7a0b\u5316\u6539\u9020\uff0c\u5e76\u4f7f\u7528Swoole\u4e2d\u7684\u534f\u7a0b\u6280\u672f\u5b9e\u73b0\u4e86\u5176\u5f02\u6b65\u5904\u7406\u3002\u5728\u5b9e\u9645\u8fd0\u884c\u4e2d\uff0c\u7531\u4e8eSwoole\u7684\u534f\u7a0b\u8c03\u5ea6\u673a\u5236\uff0c\u5f02\u6b65\u5904\u7406\u4e0d\u4f1a\u963b\u585e\u5176\u4ed6\u7684\u534f\u7a0b\uff0c\u4ece\u800c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684\u83b7\u53d6\u90ae\u4ef6\u6570\u636e\u64cd\u4f5c\u3002<\/p>\n<p>\u603b\u4e4b\uff0cSwoole\u4e2d\u534f\u7a0b\u7684\u4f7f\u7528\u662f\u63d0\u5347\u5e94\u7528\u6027\u80fd\u548c\u5e76\u53d1\u8bbf\u95ee\u7684\u4e00\u79cd\u6781\u4e3a\u91cd\u8981\u7684\u6280\u672f\uff0c\u901a\u8fc7\u4f7f\u7528\u534f\u7a0b\u53ef\u4ee5\u5b9e\u73b0\u5bf9I\/O\u64cd\u4f5c\u7684\u5f02\u6b65\u5904\u7406\uff0c\u907f\u514d\u4e86\u963b\u585e\u5f0f\u7684I\/O\u64cd\u4f5c\u5bf9\u5e94\u7528\u5e26\u6765\u7684\u6027\u80fd\u74f6\u9888\u3002\u5229\u7528Swoole\u4e2d\u7684\u534f\u7a0b\u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_imap_fetch\u51fd\u6570\uff0c\u4f7f\u5f97\u90ae\u4ef6\u7684\u83b7\u53d6\u3001\u89e3\u6790\u3001\u5206\u7c7b\u548c\u5b58\u50a8\u7b49\u64cd\u4f5c\u66f4\u52a0\u9ad8\u6548\u3001\u7a33\u5b9a\u548c\u53ef\u9760\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684_imap_fetch\u51fd\u6570\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>\u662f\u4e00\u6b3e\u57fa\u4e8ephp\u7684\u5f02\u6b65\u3001\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u5730\u5b9e\u73b0\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u901a\u4fe1\u5e94\u7528\u3002\u800c\u534f\u7a0b\u5219\u662fswoole\u4e2d\u7684\u4e00\u79cd\u91cd\u8981\u6280\u672f\uff0c\u5728\u7f51\u7edc\u901a\u4fe1\u4e2d\u8d77\u5230\u4e86\u6781\u4e3a\u91cd\u8981\u7684\u4f5c\u7528\u3002\u672c\u6587\u5c06\u4e3b\u8981\u4ecb\u7ecd\u5982\u4f55\u5728swoole\u4e2d\u4f7f\u7528\u534f\u7a0b\u5b9e\u73b0\u9ad8\u5e76\u53d1\u7684swoole_imap_fetch\u51fd\u6570\u3002 Swoole_imap_fetch\u51fd\u6570\u662fSwoole\u4e2d\u7684\u4e00\u79cdIMAP\u7f51\u7edc\u534f\u8bae\uff0c\u5b9e\u73b0\u4e86\u5bf9\u8fdc\u7a0bIMAP\u670d\u52a1\u5668\u7684\u8bbf\u95ee\u548c\u901a\u4fe1\u3002\u4f7f\u7528swoole_imap_fetch\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u4ece\u90ae\u4ef6\u670d\u52a1\u5668\u4e0a\u83b7\u53d6\u90ae\u4ef6\uff0c\u4ee5\u53ca\u5bf9\u90ae\u4ef6\u7684\u89e3\u6790\u3001\u5206\u7c7b\u3001\u5b58\u50a8\u7b49\u64cd\u4f5c\u3002\u4f46\u662f\uff0c\u7531\u4e8e\u90ae\u4ef6\u670d\u52a1\u5668\u4e2d\u5b58\u5728\u5927\u91cf\u7684\u90ae\u4ef6\u6570\u636e\uff0c\u5982\u679c\u4f7f\u7528\u4f20\u7edf\u7684\u65b9\u5f0f\u5bf9\u90ae\u4ef6\u8fdb\u884c\u83b7\u53d6\u3001\u89e3\u6790\u7b49\u64cd\u4f5c\uff0c\u5bb9\u6613\u51fa\u73b0\u6027\u80fd\u74f6\u9888\uff0c\u5bfc\u81f4\u5e94\u7528\u7684\u54cd\u5e94\u901f\u5ea6\u53d8\u6162\uff0c\u7ed9\u7528\u6237\u5e26\u6765\u4e0d\u597d\u7684\u4f53\u9a8c\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Swoole\u4e2d\u7684\u534f\u7a0b\u6765\u63d0\u5347swoole_imap_fetch\u51fd\u6570\u7684\u6027\u80fd\uff0c\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\u5982\u4e0b\uff1a \u9996\u5148\uff0c\u5728Swoole\u4e2d\u5f15\u5165\u534f\u7a0b\u5e93\uff0c\u5e76\u542f\u7528\u534f\u7a0b\u652f\u6301\u3002 co::set([&#8216;hook_flags&#8217; =&amp;gt; SWOOLE_HOOK_ALL]); \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u5728\u8c03\u7528swoole_imap_fetch\u51fd\u6570\u4e4b\u524d\uff0c\u9700\u8981\u5bf9\u8be5\u51fd\u6570\u8fdb\u884c\u534f\u7a0b\u5316\u6539\u9020\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a function swoole_imap_fetch_async($imap_stream, $msg_number, $options = 0) { return new AsyncImapFetch($imap_stream, $msg_number, $options); } class AsyncImapFetch { private $imap_stream; private $msg_number; private $options; private $deferred; public function __construct($imap_stream, $msg_number, $options = 0) { $this-&amp;gt;imap_stream = $imap_stream; $this-&amp;gt;msg_number = $msg_number; $this-&amp;gt;options = $options; $this-&amp;gt;deferred = new SwooleCoroutineChannel(1); SwooleCoroutine::create([$this, [&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-23998","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23998","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=23998"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23998\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}