
ดูตัวอย่างได้ที่ https://atomymaxsite.com/obec.php
นำไปใช้งานจริงได้ตามนี้ครับ https://demo.atomymaxsite.com/
นำ code ด้านล่างนี้ไปใช้งานได้เลยครับ
- <?php
- header('Content-Type: text/html; charset=utf-8');
- $target_url = "https://www.obec.go.th";
- $ch = curl_init();
- curl_setopt_array($ch, [
- CURLOPT_URL => $target_url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_FOLLOWLOCATION => true,
- CURLOPT_TIMEOUT => 15,
- CURLOPT_SSL_VERIFYPEER => false, // ป้องกันปัญหา SSL ในเครื่อง Local (เช่น XAMPP/Laragon)
- CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
- CURLOPT_HTTPHEADER => ["Accept-Language: th-TH,th;q=0.9"],
- ]);
- $html = curl_exec($ch);
- curl_close($ch);
- // ฟังก์ชันแปลง Relative URL เป็น Full URL
- function fixUrl($url, $base = "https://www.obec.go.th") {
- if (!$url) return "";
- if (strpos($url, "http") === 0) return $url;
- if (strpos($url, "//") === 0) return "https:" . $url;
- if (strpos($url, "/") === 0) return $base . $url;
- return $base . "/" . $url;
- }
- $news_list = [];
- if ($html) {
- $dom = new DOMDocument();
- @$dom->loadHTML('<?xml encoding="utf-8" ?>' . $html);
- $xpath = new DOMXPath($dom);
- // ค้นหากล่องข่าว (รองรับทั้งโครงสร้าง article และ card ทั่วไปของ WordPress/OBEC)
- $articles = $xpath->query("//article | //div[contains(@class, 'post')] | //div[contains(@class, 'item')]");
- $seen = [];
- foreach ($articles as $article) {
- // 1. ดึงลิงก์และหัวข้อข่าว
- $linkNode = $xpath->query(".//h2//a | .//h3//a | .//a[string-length(normalize-space(.)) > 15]", $article)->item(0);
- if (!$linkNode) continue;
- $href = fixUrl($linkNode->getAttribute("href"));
- $title = trim(preg_replace('/\s+/', ' ', $linkNode->textContent));
- if (mb_strlen($title) < 15 || isset($seen[$href])) continue;
- $seen[$href] = true;
- // 2. ดึงรูปภาพ (รองรับทั้ง src ปกติ และ data-src สำหรับ Lazy Load)
- $imgNode = $xpath->query(".//img", $article)->item(0);
- $img = "";
- if ($imgNode) {
- $img = $imgNode->getAttribute("data-src") ?: $imgNode->getAttribute("src");
- $img = fixUrl($img);
- }
- // รูปสำรองกรณีข่าวนั้นไม่มีภาพปก
- if (!$img || strpos($img, 'data:image') === 0) {
- $img = "https://via.placeholder.com/400x260/f1f5f9/64748b?text=OBEC+NEWS";
- }
- // 3. ดึงเนื้อหาย่อ (Excerpt)
- $descNode = $xpath->query(".//p | .//div[contains(@class, 'excerpt') or contains(@class, 'desc') or contains(@class, 'content')]", $article)->item(0);
- $desc = $descNode ? trim(preg_replace('/\s+/', ' ', $descNode->textContent)) : "";
- if ($desc === $title) $desc = ""; // กันข้อความซ้ำกับหัวข้อ
- // 4. ดึงวันที่ (หรือใช้วันที่ปัจจุบันเป็นค่าเริ่มต้นหากหน้าแรกไม่แสดงวันที่)
- $dateNode = $xpath->query(".//time | .//*[contains(@class, 'date') or contains(@class, 'meta')]", $article)->item(0);
- $date = $dateNode ? trim(preg_replace('/\s+/', ' ', $dateNode->textContent)) : "";
- $news_list[] = [
- 'title' => $title,
- 'href' => $href,
- 'img' => $img,
- 'desc' => $desc,
- 'date' => $date
- ];
- if (count($news_list) >= 10) break; // แสดง 10 ข่าว (2 แถว แถวละ 5 การ์ด)
- }
- }
- ?>
- <!DOCTYPE html>
- <html lang="th">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>ข่าวการศึกษา สพฐ.</title>
- <link href="https://fonts.googleapis.com/css2?family=Prompt:wght@300;400;500;600;700&display=swap" rel="stylesheet">
- <style>
- * { box-sizing: border-box; margin: 0; padding: 0; }
- body {
- font-family: 'Prompt', sans-serif;
- background-color: #f8fafc;
- color: #1e293b;
- padding: 40px 20px;
- }
- .obec-section {
- max-width: 1360px;
- margin: 0 auto;
- }
- /* ส่วนหัวข้อด้านบน */
- .obec-header {
- display: flex;
- justify-content: space-between;
- align-items: flex-end;
- margin-bottom: 24px;
- flex-wrap: wrap;
- gap: 12px;
- }
- .obec-kicker {
- display: inline-block;
- background-color: #fff7ed;
- color: #ea580c;
- font-size: 11px;
- font-weight: 700;
- padding: 4px 10px;
- border-radius: 6px;
- letter-spacing: 0.5px;
- margin-bottom: 8px;
- }
- .obec-title {
- font-size: 28px;
- font-weight: 700;
- color: #0f172a;
- line-height: 1.2;
- }
- .obec-subtitle {
- font-size: 13px;
- color: #64748b;
- margin-top: 4px;
- }
- .obec-subtitle a {
- color: #2563eb;
- text-decoration: none;
- }
- .obec-view-all {
- color: #ea580c;
- text-decoration: none;
- font-size: 14px;
- font-weight: 600;
- display: inline-flex;
- align-items: center;
- gap: 6px;
- transition: opacity 0.2s;
- }
- .obec-view-all:hover { opacity: 0.8; }
- /* ตารางการ์ด 5 คอลัมน์ */
- .obec-grid {
- display: grid;
- grid-template-columns: repeat(5, 1fr);
- gap: 20px;
- }
- @media (max-width: 1200px) { .obec-grid { grid-template-columns: repeat(3, 1fr); } }
- @media (max-width: 768px) { .obec-grid { grid-template-columns: repeat(2, 1fr); } }
- @media (max-width: 480px) { .obec-grid { grid-template-columns: 1fr; } }
- /* ดีไซน์การ์ดแต่ละใบ */
- .obec-card {
- background: #ffffff;
- border-radius: 16px;
- overflow: hidden;
- box-shadow: 0 4px 15px rgba(0, 0, 0, 0.04);
- border: 1px solid #f1f5f9;
- display: flex;
- flex-direction: column;
- text-decoration: none;
- color: inherit;
- transition: transform 0.2s ease, box-shadow 0.2s ease;
- }
- .obec-card:hover {
- transform: translateY(-4px);
- box-shadow: 0 12px 24px rgba(0, 0, 0, 0.08);
- }
- .obec-thumb {
- position: relative;
- width: 100%;
- height: 165px;
- background-color: #e2e8f0;
- overflow: hidden;
- }
- .obec-thumb img {
- width: 100%;
- height: 100%;
- object-fit: cover;
- transition: transform 0.3s ease;
- }
- .obec-card:hover .obec-thumb img {
- transform: scale(1.05);
- }
- .obec-badge {
- position: absolute;
- top: 10px;
- right: 10px;
- background-color: #2563eb;
- color: #ffffff;
- font-size: 11px;
- font-weight: 600;
- padding: 3px 10px;
- border-radius: 6px;
- box-shadow: 0 2px 6px rgba(37, 99, 235, 0.3);
- }
- .obec-body {
- padding: 16px;
- display: flex;
- flex-direction: column;
- flex-grow: 1;
- }
- .obec-card-title {
- font-size: 14.5px;
- font-weight: 600;
- color: #d97706; /* สีส้มทองตามภาพ */
- line-height: 1.45;
- margin-bottom: 8px;
- display: -webkit-box;
- -webkit-line-clamp: 3;
- -webkit-box-orient: vertical;
- overflow: hidden;
- }
- .obec-card-desc {
- font-size: 12.5px;
- color: #64748b;
- line-height: 1.5;
- margin-bottom: 14px;
- display: -webkit-box;
- -webkit-line-clamp: 2;
- -webkit-box-orient: vertical;
- overflow: hidden;
- }
- .obec-footer {
- margin-top: auto;
- padding-top: 12px;
- border-top: 1px solid #f1f5f9;
- display: flex;
- justify-content: space-between;
- align-items: center;
- font-size: 11.5px;
- color: #94a3b8;
- }
- .obec-footer-tag {
- color: #2563eb;
- font-weight: 600;
- display: inline-flex;
- align-items: center;
- gap: 4px;
- }
- </style>
- </head>
- <body>
- <section class="obec-section">
- <div class="obec-header">
- <div>
- <span class="obec-kicker">OBEC NEWS</span>
- <h2 class="obec-title">ข่าวการศึกษา สพฐ.</h2>
- <p class="obec-subtitle">อัปเดตอัตโนมัติจาก <a href="https://www.obec.go.th" target="_blank">www.obec.go.th</a></p>
- </div>
- <a href="https://www.obec.go.th" target="_blank" class="obec-view-all">
- ข่าว สพฐ. ทั้งหมด →
- </a>
- </div>
- <?php if (!emptyempty($news_list)): ?>
- <div class="obec-grid">
- <?php foreach ($news_list as $news): ?>
- <a href="<?= htmlspecialchars($news['href']) ?>" target="_blank" class="obec-card">
- <div class="obec-thumb">
- <img src="<?= htmlspecialchars($news['img']) ?>" alt="<?= htmlspecialchars($news['title']) ?>" loading="lazy">
- <span class="obec-badge">สพฐ.</span>
- </div>
- <div class="obec-body">
- <h3 class="obec-card-title"><?= htmlspecialchars($news['title']) ?></h3>
- <?php if ($news['desc']): ?>
- <p class="obec-card-desc"><?= htmlspecialchars($news['desc']) ?></p>
- <?php endif; ?>
- <div class="obec-footer">
- <span>📅 <?= htmlspecialchars($news['date'] ?: 'ข่าวประชาสัมพันธ์') ?></span>
- <span class="obec-footer-tag">☑️ สพฐ.</span>
- </div>
- </div>
- </a>
- <?php endforeach; ?>
- </div>
- <?php else: ?>
- <p style="color: #ef4444; background: #fee2e2; padding: 15px; border-radius: 8px;">
- ไม่พบรายการข่าว หรือเว็บไซต์ต้นทางมีการป้องกันการดึงข้อมูล (ตรวจสอบโครงสร้าง HTML ของหน้าเว็บต้นทาง)
- </p>
- <?php endif; ?>
- </section>
- </body>
- </html>

