在前一天的案例整合中,將之前的案例有關於擷取與取得學校最新消息網站爬蟲跟工作排程整合,讓每個指定時段會擷取最新消息出來,後續則可以加入資料庫當作更新最新消息的資料。
在本日案例整合中,是要將案例1-1與發送信件整合,讓學校最新消息可以自動發信給指定的收信對象。
首先,先將爬蟲環境跑起來,用下面的指令:
docker run --name=php_crawler -d -it php_crawler bash
接著,將lab1-1.php
程式用個人偏好程式編輯器打開,並將下面內容放入下面程式碼:
<?php
define('MAILGUN_URL', 'https://api.mailgun.net/v3/DOMAIN_NAME');
define('MAILGUN_KEY', 'key-MAILGUN_KEY');
require_once __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
$latestNews = 'https://www.nttu.edu.tw/p/503-1000-1009.php';
$client = new Client();
$response = $client->request('GET', $latestNews);
$latestNewsString = (string)$response->getBody();
$titles = [];
$descriptions = [];
$pubDates = [];
$links = [];
$authors = [];
$crawler = new Crawler($latestNewsString);
$crawler
->filter('title')
->reduce(function (Crawler $node, $i) {
global $titles;
$titles[] = $node->text();
});
$crawler
->filter('description')
->reduce(function (Crawler $node, $i) {
global $descriptions;
$descriptions[] = str_replace([" ", "\n", "\r", "\t"], "", strip_tags($node->text()));
});
$crawler
->filter('pubDate')
->reduce(function (Crawler $node, $i) {
global $pubDates;
$pubDates[] = $node->text();
});
$crawler
->filter('link')
->reduce(function (Crawler $node, $i) {
global $links;
$links[] = $node->text();
});
$crawler
->filter('author')
->reduce(function (Crawler $node, $i) {
global $authors;
$authors[] = $node->text();
});
// var_dump($descriptions);
// var_dump($pubDates);
// var_dump($links);
// var_dump($authors);
// var_dump($titles);
$text = implode(',', $descriptions) . "\n" . implode(',', $pubDates) . "\n" . implode(',', $links) . "\n";
$text .= implode(',', $authors) . "\n" . implode(',', $titles) . "\n";
$result = sendMailByMailGun('TO_EMAIL_ADDRESS', 'Peter', 'admin', 'admin@DOMAIN_NAME', 'test', '', $text, '', '');
var_dump($result);
function sendMailByMailGun($to, $toName, $mailFromName, $mailFrom, $subject, $html, $text, $tag, $replyTo) {
$array_data = [
'from'=> $mailFromName .'<'.$mailFrom.'>',
'to'=>$toName.'<'.$to.'>',
'subject'=>$subject,
'html'=>$html,
'text'=>$text,
'o:tracking'=>'yes',
'o:tracking-clicks'=>'yes',
'o:tracking-opens'=>'yes',
'o:tag'=>$tag,
'h:Reply-To'=>$replyTo
];
$session = curl_init(MAILGUN_URL.'/messages');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_USERPWD, 'api:'.MAILGUN_KEY);
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $array_data);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($session);
curl_close($session);
$results = json_decode($response, true);
return $results;
}
上面程式碼指的是下面做法:
MAILGUN_URL
與MAILGUN_KEY
。一個是請求網址,另一個是傳送過去識別金鑰。DOMAIN_NAME
與key-MAILGUN_KEY
要替換掉,key
指的是一開始有前綴key-
的金鑰。\n
進行換行,接著存成$text
變數sendMailByMailGun
函式,其參數有$to, $toName, $mailFromName, $mailFrom, $subject, $html, $text, $tag, $replyTo
。放到正在運行爬蟲環境Container
容器中。
docker cp lab1-1.php php_crawler:/root/
接著執行它,用下面指令:
docker exec php_crawler php lab1-1.php
最後,會得到如下的結果:
array(2) {
["id"]=>
string(87) "<20191014150419.1.8068997DB0F9DDA8@sandbox5099c0f44ddb4ce0883b7ed9d2a87499.mailgun.org>"
["message"]=>
string(18) "Queued. Thank you."
}
接著去檢查收信人信箱:就會看到類似如下的內容(信件內容就這樣,是測試用的XD):
到這裡,基本上就完成本日的寄送信件的案例整合了。
透過本日的案例整合,我們可以知道,幾件事情: