监控阮1峰老师的blog
监控阮一峰老师的blog
引言
阮一峰大家基本都认识,很厉害的一个人,经济学博士,文章写得很棒,知识面很广泛,计算机、算法、英语、文采,这是能想到的他的一些标签,他的博客应该算是最受欢迎的博客之一了。
我经常回去看他的博客,但有时候时间长了,再次去看,发现他已经有好几篇新文章了,我就在想,能不能自己写个程序,监控的他博客,当他有新文章的时候,第一时间推送给我。
思路
他的博客中有一个feed,是一个返回xml格式文档的接口,这个接口是最新的文章列表,总共15篇,可以通过监控这个接口中前几篇文章列表的变化来间接的检测他的文章更新,如果有新的文章马上给我的邮箱发送邮件,这样我就可以尽可能早的get到阮老师的最新文章了。
这是设计思路图:
首先就是rss解析了,这个使用php的原生函数simplexml_load_string得到一个解析之后的SimpleXMLElement对象,通过该对象可以很轻松的获取到阮老师更新的前几篇文章。然后和redis中的已发送文章列表集合作对比,如果有新的更新,将更新的信息发送给UDPserver,让UDPserver去发送邮件给用户列表。检测程序循环执行,每10分钟跑一次。
开始编码
有了思路编码就很快了(这里其实优化了好几次,刚开始的时候思路也没有这么明了,边做边改,当然了,还是要慢慢锻炼,开始之前尽可能多的完善思路)。
使用PHP依赖管理利器——Composer,经常使用PHP的开发者对这个工具应该很熟悉,不熟悉的同学可以点击前面的链接进行脑补,文档是中文的,很好懂。这个小系统将会使用到三个类库:phpmailer
,predis
,workerman
。第一个是一个发送邮件的类库,可以点击这里查看他的相关信息,第二个类库是php对redis接口的封装,这里是他的源码地址,第三个是一个创建UDPserver的类库,这里是他的官方网站。
安装依赖的类库
首先新建一个项目目录,然后进入项目目录安装依赖
<code>mkdir blog-observercd blog-observercomposer require phpmailer/phpmailercomposer require predis/prediscomposer require workerman/workerman</code>
执行完上面的命令之后,会在项目目录blog-observer目录下面看到下面几个文件composer.json
,composer.lock
和文件夹vender
,composer.json中的内容如下,至此依赖的类库安装好了。
<code class="sourceCode json"><span class="fu">{</span> <span class="dt">"require"</span><span class="fu">:</span> <span class="fu">{</span> <span class="dt">"phpmailer/phpmailer"</span><span class="fu">:</span> <span class="st">"^5.2"</span><span class="fu">,</span> <span class="dt">"predis/predis"</span><span class="fu">:</span> <span class="st">"^1.0"</span><span class="fu">,</span> <span class="dt">"workerman/workerman"</span><span class="fu">:</span> <span class="st">"^3.3"</span> <span class="fu">}</span><span class="fu">}</span></code>
监控博客更新&推送更新消息给UDP
下面是主要代码,由于是服务端程序,所以这里设置为daemon进程,我这里UDPserver为udp://127.0.0.1:1234
<code class="sourceCode php">daemonize<span class="ot">();</span><span class="kw">while</span><span class="ot">(</span><span class="dv">1</span><span class="ot">)</span>{ <span class="co">//获取最新的几篇文章,看看是否需要推送</span> <span class="kw">$c</span> = <span class="fu">file_get_contents</span><span class="ot">(</span><span class="kw">XML_URL</span><span class="ot">);</span> <span class="kw">$parse</span> = <span class="er">@</span><span class="fu">simplexml_load_string</span><span class="ot">(</span><span class="kw">$c</span><span class="ot">);</span> <span class="kw">if</span><span class="ot">(</span><span class="kw">$parse</span><span class="ot">)</span> { <span class="kw">$count</span> = <span class="fu">count</span><span class="ot">(</span><span class="kw">$parse</span>->entry<span class="ot">);</span> <span class="kw">$count</span> = <span class="kw">$count</span> > <span class="kw">RECENT_NUM</span> <span class="ot">?</span> <span class="kw">RECENT_NUM</span> <span class="ot">:</span> <span class="kw">$count</span><span class="ot">;</span> <span class="kw">$maynew</span> = <span class="ot">[];</span> <span class="kw">for</span><span class="ot">(</span><span class="kw">$i</span> = <span class="dv">0</span><span class="ot">;</span> <span class="kw">$i</span> < <span class="kw">$count</span><span class="ot">;</span> <span class="kw">$i</span>++<span class="ot">)</span> { <span class="kw">$maynew</span><span class="ot">[</span><span class="kw">$parse</span>->entry<span class="ot">[</span><span class="kw">$i</span><span class="ot">]</span>-><span class="fu">link</span>->attributes<span class="ot">()</span>->href-><span class="fu">__toString</span><span class="ot">()]</span> = <span class="kw">$parse</span>->entry<span class="ot">[</span><span class="kw">$i</span><span class="ot">]</span>->title-><span class="fu">__toString</span><span class="ot">();</span> } <span class="kw">$body</span> = <span class="st">""</span><span class="ot">;</span> <span class="co">//是否推送</span> <span class="kw">foreach</span><span class="ot">(</span><span class="kw">$maynew</span> <span class="kw">as</span> <span class="kw">$url</span> => <span class="kw">$title</span><span class="ot">)</span> { <span class="kw">if</span><span class="ot">(</span><span class="kw">$client</span>->sadd<span class="ot">(</span><span class="kw">SENDED_SET_KEY</span><span class="ot">,</span> <span class="kw">$url</span><span class="ot">))</span> { <span class="co">//send EMAIL</span> <span class="kw">$body</span> .= <span class="st">"<a href='"</span>.<span class="kw">$url</span>.<span class="st">"'>"</span>.<span class="kw">$title</span>.<span class="st">"</a><br>"</span><span class="ot">;</span> } } <span class="kw">if</span><span class="ot">(</span><span class="kw">$body</span><span class="ot">)</span> { <span class="kw">$msg</span> = <span class="ot">[];</span> <span class="kw">$msg</span><span class="ot">[</span><span class="st">'type'</span><span class="ot">]</span> = <span class="dv">1</span><span class="ot">;</span> <span class="kw">$msg</span><span class="ot">[</span><span class="st">'mailbody'</span><span class="ot">]</span> = <span class="kw">$body</span><span class="ot">;</span> <span class="kw">$start</span> = <span class="dv">0</span><span class="ot">;</span> <span class="kw">while</span><span class="ot">(</span><span class="kw">$mailaddrs</span> = <span class="kw">$client</span>->lrange<span class="ot">(</span><span class="kw">EMAIL_LIST_KEY</span> <span class="ot">,</span><span class="kw">$start</span><span class="ot">,</span> <span class="ot">(</span><span class="kw">$start</span> + <span class="kw">EVERY_SEND_NUM</span> -<span class="dv">1</span> <span class="ot">)))</span> { <span class="kw">$msg</span><span class="ot">[</span><span class="st">'mailaddrs'</span><span class="ot">]</span> = <span class="kw">$mailaddrs</span><span class="ot">;</span> <span class="kw">$send_msg</span> = <span class="fu">json_encode</span><span class="ot">(</span><span class="kw">$msg</span><span class="ot">);</span> <span class="fu">socket_sendto</span><span class="ot">(</span><span class="kw">$sock</span><span class="ot">,</span> <span class="kw">$send_msg</span><span class="ot">,</span> <span class="fu">strlen</span><span class="ot">(</span><span class="kw">$send_msg</span><span class="ot">),</span> <span class="dv">0</span><span class="ot">,</span> <span class="st">'127.0.0.1'</span><span class="ot">,</span> <span class="dv">1234</span><span class="ot">);</span> <span class="kw">$start</span> += <span class="kw">EVERY_SEND_NUM</span><span class="ot">;</span> } } } <span class="fu">sleep</span><span class="ot">(</span><span class="kw">GAP_SECONDS</span><span class="ot">);</span>}</code>
UDPserver程序
有了workerman,可以很方便的实现UDPserver,比自己写来的快得多。
<code class="sourceCode php"><span class="kw">$udp_worker</span> = <span class="kw">new</span> Workerman\Worker<span class="ot">(</span><span class="st">"udp://0.0.0.0:"</span>.<span class="kw">MAIL_UDP_PORT</span><span class="ot">);</span><span class="kw">$udp_worker</span>-><span class="fu">count</span> = <span class="dv">2</span><span class="ot">;</span><span class="kw">$udp_worker</span>->onMessage = <span class="kw">function</span><span class="ot">(</span><span class="kw">$connection</span><span class="ot">,</span> <span class="kw">$data</span><span class="ot">)</span> <span class="kw">use</span> <span class="ot">(</span><span class="kw">$mail</span><span class="ot">)</span>{ <span class="kw">$arr</span> = <span class="fu">json_decode</span><span class="ot">(</span><span class="kw">$data</span><span class="ot">,</span> <span class="kw">true</span><span class="ot">);</span> <span class="kw">switch</span><span class="ot">(</span><span class="kw">$arr</span><span class="ot">[</span><span class="st">'type'</span><span class="ot">])</span> { <span class="co">//发送邮件</span> <span class="kw">case </span><span class="st">'1'</span><span class="ot">:</span> { <span class="kw">$mailaddrs</span> = <span class="kw">$arr</span><span class="ot">[</span><span class="st">'mailaddrs'</span><span class="ot">];</span> <span class="kw">if</span><span class="ot">(</span>!<span class="fu">empty</span><span class="ot">(</span><span class="kw">$mailaddrs</span><span class="ot">)</span> && <span class="kw">$arr</span><span class="ot">[</span><span class="st">'mailbody'</span><span class="ot">])</span> { <span class="kw">foreach</span><span class="ot">(</span><span class="kw">$mailaddrs</span> <span class="kw">as</span> <span class="kw">$to</span><span class="ot">)</span> { <span class="kw">$mail</span>->clearAddresses<span class="ot">();</span> <span class="kw">$mail</span>->AddAddress<span class="ot">(</span><span class="kw">$to</span><span class="ot">);</span> <span class="kw">$mail</span>->Body = <span class="kw">$arr</span><span class="ot">[</span><span class="st">'mailbody'</span><span class="ot">];</span> <span class="kw">if</span><span class="ot">(</span>!<span class="kw">$mail</span>->Send<span class="ot">())</span> { <span class="fu">echo</span> <span class="st">"发送邮件失败:</span><span class="kw">\n</span><span class="st">"</span>.<span class="st">"address:"</span>.<span class="kw">$to</span>.<span class="st">"</span><span class="kw">\n</span><span class="st">"</span><span class="ot">;</span> } } } <span class="kw">break</span><span class="ot">;</span> } <span class="kw">default:</span> <span class="kw">break</span><span class="ot">;</span> }}<span class="ot">;</span>Workerman\Worker::runAll<span class="ot">();</span></code>
启动监控程序
好了,至此所有的设计编码工作就完成了,现在启动程序,进程启动之后会议daemon的形式运行,不会随着终端的关闭而停止。
<code>php xmldup.php startphp xmlmail.php</code>
总结
这是一个小系统,当然了还有很多不规范的地方,比如daemon进程一般都会以字母d
结尾,还有就是启动很不方便,要启动两次脚本,哈哈,当然了,这只是自己先来无事玩玩了,要真设计一个完成的系统估计会考虑很多很多的东西,加油吧,继续前进。
这里是github地址:blog-observer,自己试用的时候记得修改邮箱名称和密码。
如果谁也想第一时间获取到最新的阮老师的文章可以给我发邮件,我把你们的邮件地添加到邮件list中,但不保证会发送到,有时候关了电脑程序就停止了~
- 7楼飞凡123
- 时间间隔设定的是多少
- Re: 奔跑的Man
- @飞凡123,测试的时候是600s,这个时间不能太短,太短了没多大意义,还浪费阮老师的服务器资源
- 6楼Liez
- 马克
- 5楼白丸
- 真是好学生
- 4楼旭宝爱吃鱼
- 可以,赞
- Re: 奔跑的Man
- @旭宝爱吃鱼,THX
- 3楼wzx_xle
- 我用的foxmail的rss订阅功能
- Re: 奔跑的Man
- @wzx_xle,@星夜落尘,RSS订阅?行,你们可以啊,我又没说不可以,你要用RSS订阅我啥也不说了,我就喜欢DIV,得劲了吧~~
- 2楼永远的麦子
- 写得很好,也很喜欢阮老师的博客,上次了解到阮老师的博客是学习git的时候了解到阮老师的。
- Re: 奔跑的Man
- @永远的麦子,谢谢了。多像阮老师学习,哈哈,多涉猎技术之外的知识充实自己
- 1楼星夜落尘
- 不是有rss订阅吗。。。

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen



Viele Benutzer werden sich bei der Auswahl von Smartwatches für die Marke Huawei entscheiden. Viele Benutzer sind neugierig auf den Unterschied zwischen Huawei GT3pro und GT4. Was sind die Unterschiede zwischen Huawei GT3pro und GT4? 1. Aussehen GT4: 46 mm und 41 mm, das Material ist Glasspiegel + Edelstahlgehäuse + hochauflösende Faserrückschale. GT3pro: 46,6 mm und 42,9 mm, das Material ist Saphirglas + Titangehäuse/Keramikgehäuse + Keramikrückschale 2. Gesundes GT4: Mit dem neuesten Huawei Truseen5.5+-Algorithmus werden die Ergebnisse genauer. GT3pro: EKG-Elektrokardiogramm sowie Blutgefäß und Sicherheit hinzugefügt

Um das Hochladen und Herunterladen von Dateien in Workerman-Dokumenten zu implementieren, sind spezifische Codebeispiele erforderlich. Einführung: Workerman ist ein leistungsstarkes PHP-Framework für die asynchrone Netzwerkkommunikation, das einfach, effizient und benutzerfreundlich ist. In der tatsächlichen Entwicklung sind das Hochladen und Herunterladen von Dateien häufige Funktionsanforderungen. In diesem Artikel wird erläutert, wie das Workerman-Framework zum Implementieren des Hochladens und Herunterladens von Dateien verwendet wird, und es werden spezifische Codebeispiele aufgeführt. 1. Datei-Upload: Unter Datei-Upload versteht man die Übertragung von Dateien vom lokalen Computer auf den Server. Folgendes wird verwendet

Einführung in die Implementierung der grundlegenden Verwendung von Workerman-Dokumenten: Workerman ist ein leistungsstarkes PHP-Entwicklungsframework, mit dem Entwickler problemlos Netzwerkanwendungen mit hoher Parallelität erstellen können. In diesem Artikel wird die grundlegende Verwendung von Workerman vorgestellt, einschließlich Installation und Konfiguration, Erstellung von Diensten und Überwachungsports, Bearbeitung von Clientanfragen usw. Und geben Sie entsprechende Codebeispiele an. 1. Installieren und konfigurieren Sie Workerman. Geben Sie in der Befehlszeile den folgenden Befehl ein, um Workerman zu installieren: c

Swoole und Workerman sind beide leistungsstarke PHP-Server-Frameworks. Swoole ist für seine asynchrone Verarbeitung, hervorragende Leistung und Skalierbarkeit bekannt und eignet sich für Projekte, die eine große Anzahl gleichzeitiger Anfragen und einen hohen Durchsatz verarbeiten müssen. Workerman bietet die Flexibilität sowohl des asynchronen als auch des synchronen Modus mit einer intuitiven API, die sich besser für Benutzerfreundlichkeit und Projekte eignet, die ein geringeres Parallelitätsvolumen bewältigen.

Workerman-Entwicklung: Echtzeit-Videoanrufe basierend auf dem UDP-Protokoll Zusammenfassung: In diesem Artikel wird erläutert, wie Sie mit dem Workerman-Framework eine Echtzeit-Videoanruffunktion basierend auf dem UDP-Protokoll implementieren. Wir werden ein tiefgreifendes Verständnis der Eigenschaften des UDP-Protokolls erlangen und anhand von Codebeispielen zeigen, wie man eine einfache, aber vollständige Echtzeit-Videoanrufanwendung erstellt. Einführung: In der Netzwerkkommunikation sind Echtzeit-Videoanrufe eine sehr wichtige Funktion. Beim herkömmlichen TCP-Protokoll kann es bei der Implementierung von Videoanrufen mit hoher Echtzeitgeschwindigkeit zu Problemen wie Übertragungsverzögerungen kommen. Und UDP

Für die Verwendung von Workerman zum Aufbau eines hochverfügbaren Lastausgleichssystems sind spezifische Codebeispiele erforderlich. Im Bereich der modernen Technologie müssen mit der rasanten Entwicklung des Internets immer mehr Websites und Anwendungen eine große Anzahl gleichzeitiger Anforderungen verarbeiten. Um eine hohe Verfügbarkeit und Leistung zu erreichen, ist das Lastausgleichssystem zu einer der wesentlichen Komponenten geworden. In diesem Artikel wird erläutert, wie Sie mit dem PHP-Open-Source-Framework Workerman ein Hochverfügbarkeits-Lastausgleichssystem erstellen und spezifische Codebeispiele bereitstellen. 1. Einführung in WorkermanWorke

Für die Implementierung der Reverse-Proxy-Funktion im Workerman-Dokument sind spezifische Codebeispiele erforderlich. Einführung: Workerman ist ein leistungsstarkes PHP-Framework für die Netzwerkkommunikation mit mehreren Prozessen, das umfangreiche Funktionen und leistungsstarke Leistung bietet und in der Web-Echtzeitkommunikation weit verbreitet ist Service-Szenarien. Unter anderem unterstützt Workerman auch die Reverse-Proxy-Funktion, mit der Lastausgleich und statisches Ressourcen-Caching realisiert werden können, wenn der Server externe Dienste bereitstellt. In diesem Artikel wird erläutert, wie Sie Workerman zum Implementieren der Reverse-Proxy-Funktion verwenden.

So implementieren Sie die Timer-Funktion im Workerman-Dokument Workerman ist ein leistungsstarkes PHP-Framework für die asynchrone Netzwerkkommunikation, das eine Fülle von Funktionen bereitstellt, einschließlich der Timer-Funktion. Verwenden Sie Timer, um Code innerhalb bestimmter Zeitintervalle auszuführen. Dies eignet sich sehr gut für Anwendungsszenarien wie geplante Aufgaben und Abfragen. Als nächstes werde ich detailliert vorstellen, wie die Timer-Funktion in Workerman implementiert wird, und spezifische Codebeispiele bereitstellen. Schritt 1: Workerman installieren Zuerst müssen wir Worker installieren
