{"id":46833,"date":"2024-12-02T12:31:43","date_gmt":"2024-12-02T04:31:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46833\/"},"modified":"2024-12-02T12:31:43","modified_gmt":"2024-12-02T04:31:43","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%b8%8e-elm-%e4%ba%a4%e4%ba%92%ef%bc%9a%e6%9e%84%e5%bb%ba%e5%81%a5%e5%a3%ae%e4%b8%94%e5%8f%af%e7%bb%b4%e6%8a%a4%e7%9a%84%e5%89%8d%e7%ab%af%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46833\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u4e0e Elm \u4ea4\u4e92\uff1a\u6784\u5efa\u5065\u58ee\u4e14\u53ef\u7ef4\u62a4\u7684\u524d\u7aef\u5e94\u7528"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u4e0e Elm \u4ea4\u4e92\uff1a\u6784\u5efa\u5065\u58ee\u4e14\u53ef\u7ef4\u62a4\u7684\u524d\u7aef\u5e94\u7528<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e Elm \u4ea4\u4e92\uff1a\u6784\u5efa\u5065\u58ee\u4e14\u53ef\u7ef4\u62a4\u7684\u524d\u7aef\u5e94\u7528\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p>\u5982\u4f55\u4e0e Elm \u4ea4\u4e92\uff1f\u4e86\u89e3 Elm \u7aef\u53e3\uff1a\u4f7f\u7528\u7aef\u53e3\u8fdb\u884c\u901a\u4fe1\uff0c\u63a5\u6536\u5e76\u53d1\u9001\u6570\u636e\u3001\u4e8b\u4ef6\u6216\u547d\u4ee4\u3002\u901a\u8fc7 JSON \u4ea4\u6362\u6570\u636e\uff1a\u4f7f\u7528 PHP \u7684 json_encode() \u548c Elm \u7684 Json.Decode \u6a21\u5757\u5728\u4e24\u8005\u95f4\u4ea4\u6362\u6570\u636e\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u4e0e Elm Counter \u5e94\u7528\u7a0b\u5e8f\u4ea4\u4e92\uff1a\u521b\u5efa\u7aef\u53e3\u3001\u5904\u7406\u4f20\u5165\u6d88\u606f\u3001\u53d1\u9001\u6570\u636e\uff0c\u5e76\u5728 Elm \u4e2d\u4f7f\u7528 Signal.map \u63a5\u6536\u6570\u636e\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729849283671b67c305652.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e Elm \u4ea4\u4e92\uff1a\u6784\u5efa\u5065\u58ee\u4e14\u53ef\u7ef4\u62a4\u7684\u524d\u7aef\u5e94\u7528\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u4e0e Elm \u4ea4\u4e92\uff1a\u6784\u5efa\u5065\u58ee\u4e14\u53ef\u7ef4\u62a4\u7684\u524d\u7aef\u5e94\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\uff1a\u4e0e Elm \u4ea4\u4e92\u7684\u6307\u5357<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHP \u51fd\u6570\u662f\u4e0e Elm \u8fdb\u884c\u4ea4\u4e92\u7684\u91cd\u8981\u9014\u5f84\uff0c\u65e0\u8bba\u662f\u8981\u4f20\u9012\u6570\u636e\u8fd8\u662f\u89e6\u53d1\u64cd\u4f5c\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4e0e Elm \u4ea4\u4e92\uff1a<\/p>\n<ol>\n<li>\u7406\u89e3 Elm \u7aef\u53e3<\/li>\n<li>\u901a\u8fc7 JSON \u8fdb\u884c\u6570\u636e\u4ea4\u6362<\/li>\n<li>\u5b9e\u6218\u6848\u4f8b\uff1a\u4e0e Elm Counter \u5e94\u7528\u7a0b\u5e8f\u4ea4\u4e92<\/li>\n<\/ol>\n<p><strong>\u4e86\u89e3 Elm \u7aef\u53e3<\/strong><\/p>\n<p>Elm \u7aef\u53e3\u662f\u7528\u4e8e\u4e0e\u5916\u90e8\u7cfb\u7edf\uff08\u5982 PHP\uff09\u901a\u4fe1\u7684\u901a\u9053\u3002\u5b83\u4eec\u53ef\u4ee5\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\uff0c\u6d88\u606f\u53ef\u4ee5\u662f\u6570\u636e\u3001\u4e8b\u4ef6\u6216\u547d\u4ee4\u3002<\/p>\n<p>\u8981\u521b\u5efa\u7aef\u53e3\uff0c\u8bf7\u4f7f\u7528 <code>port<\/code> \u5173\u952e\u5b57\uff0c\u5982\uff1a<\/p>\n<pre>port message : Signal a -&gt; b<\/pre>\n<p><strong>\u901a\u8fc7 JSON \u8fdb\u884c\u6570\u636e\u4ea4\u6362<\/strong><\/p>\n<p>JSON\uff08JavaScript Object Notation\uff09\u662f\u4e00\u79cd\u6d41\u884c\u7684\u6570\u636e\u683c\u5f0f\uff0c\u7528\u4e8e\u5728 PHP \u548c Elm \u4e4b\u95f4\u8f7b\u677e\u4ea4\u6362\u6570\u636e\u3002<\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>json_encode()<\/code> \u51fd\u6570\u5c06 PHP \u53d8\u91cf\u8f6c\u6362\u4e3a JSON\uff1a<\/p>\n<pre>$phpArray = ['name' =&gt; 'John', 'age' =&gt; 30];\n$json = json_encode($phpArray);<\/pre>\n<p>\u5728 Elm \u4e2d\uff0c<code>Json.Decode<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u5c06 JSON \u89e3\u6790\u4e3a Elm \u7c7b\u578b\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>type Person = { name : String, age : Int }\n\nJson.Decode.decodeValue Person json<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u4e0e Elm Counter \u5e94\u7528\u7a0b\u5e8f\u4ea4\u4e92<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684 Elm Counter \u5e94\u7528\u7a0b\u5e8f\u4e3a\u4f8b\u6765\u6f14\u793a PHP \u51fd\u6570\u4e0e Elm \u7684\u4ea4\u4e92\u3002<\/p>\n<p><strong>\u6b65\u9aa4 1\uff1a\u521b\u5efa Elm \u5e94\u7528\u7a0b\u5e8f<\/strong><\/p>\n<pre>import Html exposing (text)\n\nmain = text \"Counter: 0\"<\/pre>\n<p><strong>\u6b65\u9aa4 2\uff1a\u5728 PHP \u4e2d\u521b\u5efa\u7aef\u53e3<\/strong><\/p>\n<pre>\/\/ \u8bbe\u7f6e\u7aef\u53e3\n$port = new \\Ratchet\\Wamp\\WampServer('localhost', 8080);\n\n\/\/ \u5904\u7406\u4f20\u5165\u6d88\u606f\n$port-&gt;on('open', function($conn) {\n    $conn-&gt;send(json_encode(['count' =&gt; 0]));\n});<\/pre>\n<p><strong>\u6b65\u9aa4 3\uff1a\u4f7f\u7528 PHP \u51fd\u6570\u4f20\u9012\u6570\u636e<\/strong><\/p>\n<p>\u5728 PHP \u51fd\u6570\u4e2d\uff0c\u4f7f\u7528 <code>send()<\/code> \u65b9\u6cd5\u5411 Elm \u7aef\u53e3\u53d1\u9001\u6570\u636e\uff1a<\/p>\n<pre>\/\/ \u5047\u8bbe\u60a8\u6709\u4e00\u4e2a\u9012\u589e\u8ba1\u6570\u5668\u7684 PHP \u51fd\u6570\nfunction incrementCounter() {\n    \/\/ \u83b7\u53d6\u5f53\u524d\u8ba1\u6570\n    $count = (int) $conn-&gt;recv();\n\n    \/\/ \u9012\u589e\u8ba1\u6570\n    $count++;\n\n    \/\/ \u5c06\u66f4\u65b0\u540e\u7684\u8ba1\u6570\u53d1\u9001\u56de Elm\n    $conn-&gt;send(json_encode(['count' =&gt; $count]));\n}<\/pre>\n<p><strong>\u6b65\u9aa4 4\uff1a\u5728 Elm \u4e2d\u63a5\u6536\u6570\u636e<\/strong><\/p>\n<p>\u5728 Elm \u4e2d\uff0c\u4f7f\u7528 <code>Signal.map<\/code> \u51fd\u6570\u5728\u6bcf\u6b21\u6536\u5230\u6d88\u606f\u65f6\u63a5\u6536\u6570\u636e\uff1a<\/p>\n<pre>signal = Signal.map count portMessage\n\ncount = Signal.foldl countSignal 0 signal<\/pre>\n<p>\u96c6\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 PHP \u51fd\u6570\u4e0e Elm \u5e94\u7528\u7a0b\u5e8f\u4ea4\u4e92\u5e76\u66f4\u65b0\u8ba1\u6570\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u4e0e Elm \u4ea4\u4e92\uff1a\u6784\u5efa\u5065\u58ee\u4e14\u53ef\u7ef4\u62a4\u7684\u524d\u7aef\u5e94\u7528 \u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u7c73\u4e91\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aPHP \u51fd\u6570\u5982\u4f55\u4e0e Elm \u4ea4\u4e92\uff1a\u6784\u5efa\u5065\u58ee\u4e14\u53ef\u7ef4\u62a4\u7684\u524d\u7aef\u5e94\u7528\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01 \u5982\u4f55\u4e0e Elm \u4ea4\u4e92\uff1f\u4e86\u89e3 Elm \u7aef\u53e3\uff1a\u4f7f\u7528\u7aef\u53e3\u8fdb\u884c\u901a\u4fe1\uff0c\u63a5\u6536\u5e76\u53d1\u9001\u6570\u636e\u3001\u4e8b\u4ef6\u6216\u547d\u4ee4\u3002\u901a\u8fc7 JSON \u4ea4\u6362\u6570\u636e\uff1a\u4f7f\u7528 PHP \u7684 json_encode() \u548c Elm \u7684 Json.Decode \u6a21\u5757\u5728\u4e24\u8005\u95f4\u4ea4\u6362\u6570\u636e\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u4e0e Elm Counter \u5e94\u7528\u7a0b\u5e8f\u4ea4\u4e92\uff1a\u521b\u5efa\u7aef\u53e3\u3001\u5904\u7406\u4f20\u5165\u6d88\u606f\u3001\u53d1\u9001\u6570\u636e\uff0c\u5e76\u5728 Elm \u4e2d\u4f7f\u7528 Signal.map \u63a5\u6536\u6570\u636e\u3002 PHP \u51fd\u6570\uff1a\u4e0e Elm \u4ea4\u4e92\u7684\u6307\u5357 \u7b80\u4ecb PHP \u51fd\u6570\u662f\u4e0e Elm \u8fdb\u884c\u4ea4\u4e92\u7684\u91cd\u8981\u9014\u5f84\uff0c\u65e0\u8bba\u662f\u8981\u4f20\u9012\u6570\u636e\u8fd8\u662f\u89e6\u53d1\u64cd\u4f5c\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u4e0e Elm \u4ea4\u4e92\uff1a \u7406\u89e3 Elm \u7aef\u53e3 \u901a\u8fc7 JSON \u8fdb\u884c\u6570\u636e\u4ea4\u6362 \u5b9e\u6218\u6848\u4f8b\uff1a\u4e0e Elm Counter \u5e94\u7528\u7a0b\u5e8f\u4ea4\u4e92 \u4e86\u89e3 Elm \u7aef\u53e3 Elm [&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-46833","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46833","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=46833"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46833\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}