NEWS

ดึงข่าวสารการศึกษาจาก Obec.go.th มาแสดงในเว็บของเราได้

admin  18/พ.ย./2569  A- A A+   

ดูตัวอย่างได้ที่  https://atomymaxsite.com/obec.php

นำไปใช้งานจริงได้ตามนี้ครับ  https://demo.atomymaxsite.com/

นำ code ด้านล่างนี้ไปใช้งานได้เลยครับ


  1. <?php  
  2. header('Content-Type: text/html; charset=utf-8');  
  3. $target_url = "https://www.obec.go.th";  
  4.   
  5. $ch = curl_init();  
  6. curl_setopt_array($ch, [  
  7.     CURLOPT_URL => $target_url,  
  8.     CURLOPT_RETURNTRANSFER => true,  
  9.     CURLOPT_FOLLOWLOCATION => true,  
  10.     CURLOPT_TIMEOUT => 15,  
  11.     CURLOPT_SSL_VERIFYPEER => false, // ป้องกันปัญหา SSL ในเครื่อง Local (เช่น XAMPP/Laragon)  
  12.     CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",  
  13.     CURLOPT_HTTPHEADER => ["Accept-Language: th-TH,th;q=0.9"],  
  14. ]);  
  15. $html = curl_exec($ch);  
  16. curl_close($ch);  
  17.   
  18. // ฟังก์ชันแปลง Relative URL เป็น Full URL  
  19. function fixUrl($url, $base = "https://www.obec.go.th") {  
  20.     if (!$url) return "";  
  21.     if (strpos($url, "http") === 0) return $url;  
  22.     if (strpos($url, "//") === 0) return "https:" . $url;  
  23.     if (strpos($url, "/") === 0) return $base . $url;  
  24.     return $base . "/" . $url;  
  25. }  
  26.   
  27. $news_list = [];  
  28.   
  29. if ($html) {  
  30.     $dom = new DOMDocument();  
  31.     @$dom->loadHTML('<?xml encoding="utf-8" ?>' . $html);  
  32.     $xpath = new DOMXPath($dom);  
  33.   
  34.     // ค้นหากล่องข่าว (รองรับทั้งโครงสร้าง article และ card ทั่วไปของ WordPress/OBEC)  
  35.     $articles = $xpath->query("//article | //div[contains(@class, 'post')] | //div[contains(@class, 'item')]");  
  36.     $seen = [];  
  37.   
  38.     foreach ($articles as $article) {  
  39.         // 1. ดึงลิงก์และหัวข้อข่าว  
  40.         $linkNode = $xpath->query(".//h2//a | .//h3//a | .//a[string-length(normalize-space(.)) > 15]", $article)->item(0);  
  41.         if (!$linkNode) continue;  
  42.   
  43.         $href = fixUrl($linkNode->getAttribute("href"));  
  44.         $title = trim(preg_replace('/\s+/', ' ', $linkNode->textContent));  
  45.   
  46.         if (mb_strlen($title) < 15 || isset($seen[$href])) continue;  
  47.         $seen[$href] = true;  
  48.   
  49.         // 2. ดึงรูปภาพ (รองรับทั้ง src ปกติ และ data-src สำหรับ Lazy Load)  
  50.         $imgNode = $xpath->query(".//img", $article)->item(0);  
  51.         $img = "";  
  52.         if ($imgNode) {  
  53.             $img = $imgNode->getAttribute("data-src") ?: $imgNode->getAttribute("src");  
  54.             $img = fixUrl($img);  
  55.         }  
  56.         // รูปสำรองกรณีข่าวนั้นไม่มีภาพปก  
  57.         if (!$img || strpos($img, 'data:image') === 0) {  
  58.             $img = "https://via.placeholder.com/400x260/f1f5f9/64748b?text=OBEC+NEWS";  
  59.         }  
  60.   
  61.         // 3. ดึงเนื้อหาย่อ (Excerpt)  
  62.         $descNode = $xpath->query(".//p | .//div[contains(@class, 'excerpt') or contains(@class, 'desc') or contains(@class, 'content')]", $article)->item(0);  
  63.         $desc = $descNode ? trim(preg_replace('/\s+/', ' ', $descNode->textContent)) : "";  
  64.         if ($desc === $title) $desc = ""; // กันข้อความซ้ำกับหัวข้อ  
  65.   
  66.         // 4. ดึงวันที่ (หรือใช้วันที่ปัจจุบันเป็นค่าเริ่มต้นหากหน้าแรกไม่แสดงวันที่)  
  67.         $dateNode = $xpath->query(".//time | .//*[contains(@class, 'date') or contains(@class, 'meta')]", $article)->item(0);  
  68.         $date = $dateNode ? trim(preg_replace('/\s+/', ' ', $dateNode->textContent)) : "";  
  69.   
  70.         $news_list[] = [  
  71.             'title' => $title,  
  72.             'href'  => $href,  
  73.             'img'   => $img,  
  74.             'desc'  => $desc,  
  75.             'date'  => $date  
  76.         ];  
  77.   
  78.         if (count($news_list) >= 10) break; // แสดง 10 ข่าว (2 แถว แถวละ 5 การ์ด)  
  79.     }  
  80. }  
  81. ?>  
  82. <!DOCTYPE html>  
  83. <html lang="th">  
  84. <head>  
  85. <meta charset="UTF-8">  
  86. <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  87. <title>ข่าวการศึกษา สพฐ.</title>  
  88. <link href="https://fonts.googleapis.com/css2?family=Prompt:wght@300;400;500;600;700&display=swap" rel="stylesheet">  
  89. <style>  
  90.     * { box-sizing: border-box; margin: 0; padding: 0; }  
  91.     body {  
  92.         font-family: 'Prompt', sans-serif;  
  93.         background-color: #f8fafc;  
  94.         color: #1e293b;  
  95.         padding: 40px 20px;  
  96.     }  
  97.     .obec-section {  
  98.         max-width: 1360px;  
  99.         margin: 0 auto;  
  100.     }  
  101.     /* ส่วนหัวข้อด้านบน */  
  102.     .obec-header {  
  103.         display: flex;  
  104.         justify-content: space-between;  
  105.         align-items: flex-end;  
  106.         margin-bottom: 24px;  
  107.         flex-wrap: wrap;  
  108.         gap: 12px;  
  109.     }  
  110.     .obec-kicker {  
  111.         display: inline-block;  
  112.         background-color: #fff7ed;  
  113.         color: #ea580c;  
  114.         font-size: 11px;  
  115.         font-weight: 700;  
  116.         padding: 4px 10px;  
  117.         border-radius: 6px;  
  118.         letter-spacing: 0.5px;  
  119.         margin-bottom: 8px;  
  120.     }  
  121.     .obec-title {  
  122.         font-size: 28px;  
  123.         font-weight: 700;  
  124.         color: #0f172a;  
  125.         line-height: 1.2;  
  126.     }  
  127.     .obec-subtitle {  
  128.         font-size: 13px;  
  129.         color: #64748b;  
  130.         margin-top: 4px;  
  131.     }  
  132.     .obec-subtitle a {  
  133.         color: #2563eb;  
  134.         text-decoration: none;  
  135.     }  
  136.     .obec-view-all {  
  137.         color: #ea580c;  
  138.         text-decoration: none;  
  139.         font-size: 14px;  
  140.         font-weight: 600;  
  141.         display: inline-flex;  
  142.         align-items: center;  
  143.         gap: 6px;  
  144.         transition: opacity 0.2s;  
  145.     }  
  146.     .obec-view-all:hover { opacity: 0.8; }  
  147.   
  148.     /* ตารางการ์ด 5 คอลัมน์ */  
  149.     .obec-grid {  
  150.         display: grid;  
  151.         grid-template-columns: repeat(5, 1fr);  
  152.         gap: 20px;  
  153.     }  
  154.     @media (max-width: 1200px) { .obec-grid { grid-template-columns: repeat(3, 1fr); } }  
  155.     @media (max-width: 768px)  { .obec-grid { grid-template-columns: repeat(2, 1fr); } }  
  156.     @media (max-width: 480px)  { .obec-grid { grid-template-columns: 1fr; } }  
  157.   
  158.     /* ดีไซน์การ์ดแต่ละใบ */  
  159.     .obec-card {  
  160.         background: #ffffff;  
  161.         border-radius: 16px;  
  162.         overflow: hidden;  
  163.         box-shadow: 0 4px 15px rgba(0, 0, 0, 0.04);  
  164.         border: 1px solid #f1f5f9;  
  165.         display: flex;  
  166.         flex-direction: column;  
  167.         text-decoration: none;  
  168.         color: inherit;  
  169.         transition: transform 0.2s ease, box-shadow 0.2s ease;  
  170.     }  
  171.     .obec-card:hover {  
  172.         transform: translateY(-4px);  
  173.         box-shadow: 0 12px 24px rgba(0, 0, 0, 0.08);  
  174.     }  
  175.     .obec-thumb {  
  176.         position: relative;  
  177.         width: 100%;  
  178.         height: 165px;  
  179.         background-color: #e2e8f0;  
  180.         overflow: hidden;  
  181.     }  
  182.     .obec-thumb img {  
  183.         width: 100%;  
  184.         height: 100%;  
  185.         object-fit: cover;  
  186.         transition: transform 0.3s ease;  
  187.     }  
  188.     .obec-card:hover .obec-thumb img {  
  189.         transform: scale(1.05);  
  190.     }  
  191.     .obec-badge {  
  192.         position: absolute;  
  193.         top: 10px;  
  194.         right: 10px;  
  195.         background-color: #2563eb;  
  196.         color: #ffffff;  
  197.         font-size: 11px;  
  198.         font-weight: 600;  
  199.         padding: 3px 10px;  
  200.         border-radius: 6px;  
  201.         box-shadow: 0 2px 6px rgba(37, 99, 235, 0.3);  
  202.     }  
  203.     .obec-body {  
  204.         padding: 16px;  
  205.         display: flex;  
  206.         flex-direction: column;  
  207.         flex-grow: 1;  
  208.     }  
  209.     .obec-card-title {  
  210.         font-size: 14.5px;  
  211.         font-weight: 600;  
  212.         color: #d97706; /* สีส้มทองตามภาพ */  
  213.         line-height: 1.45;  
  214.         margin-bottom: 8px;  
  215.         display: -webkit-box;  
  216.         -webkit-line-clamp: 3;  
  217.         -webkit-box-orient: vertical;  
  218.         overflow: hidden;  
  219.     }  
  220.     .obec-card-desc {  
  221.         font-size: 12.5px;  
  222.         color: #64748b;  
  223.         line-height: 1.5;  
  224.         margin-bottom: 14px;  
  225.         display: -webkit-box;  
  226.         -webkit-line-clamp: 2;  
  227.         -webkit-box-orient: vertical;  
  228.         overflow: hidden;  
  229.     }  
  230.     .obec-footer {  
  231.         margin-top: auto;  
  232.         padding-top: 12px;  
  233.         border-top: 1px solid #f1f5f9;  
  234.         display: flex;  
  235.         justify-content: space-between;  
  236.         align-items: center;  
  237.         font-size: 11.5px;  
  238.         color: #94a3b8;  
  239.     }  
  240.     .obec-footer-tag {  
  241.         color: #2563eb;  
  242.         font-weight: 600;  
  243.         display: inline-flex;  
  244.         align-items: center;  
  245.         gap: 4px;  
  246.     }  
  247. </style>  
  248. </head>  
  249. <body>  
  250.   
  251. <section class="obec-section">  
  252.     <div class="obec-header">  
  253.         <div>  
  254.             <span class="obec-kicker">OBEC NEWS</span>  
  255.             <h2 class="obec-title">ข่าวการศึกษา สพฐ.</h2>  
  256.             <p class="obec-subtitle">อัปเดตอัตโนมัติจาก <a href="https://www.obec.go.th" target="_blank">www.obec.go.th</a></p>  
  257.         </div>  
  258.         <a href="https://www.obec.go.th" target="_blank" class="obec-view-all">  
  259.             ข่าว สพฐ. ทั้งหมด →  
  260.         </a>  
  261.     </div>  
  262.   
  263.     <?php if (!emptyempty($news_list)): ?>  
  264.         <div class="obec-grid">  
  265.             <?php foreach ($news_list as $news): ?>  
  266.                 <a href="<?= htmlspecialchars($news['href']) ?>" target="_blank" class="obec-card">  
  267.                     <div class="obec-thumb">  
  268.                         <img src="<?= htmlspecialchars($news['img']) ?>" alt="<?= htmlspecialchars($news['title']) ?>" loading="lazy">  
  269.                         <span class="obec-badge">สพฐ.</span>  
  270.                     </div>  
  271.                     <div class="obec-body">  
  272.                         <h3 class="obec-card-title"><?= htmlspecialchars($news['title']) ?></h3>  
  273.                         <?php if ($news['desc']): ?>  
  274.                             <p class="obec-card-desc"><?= htmlspecialchars($news['desc']) ?></p>  
  275.                         <?php endif; ?>  
  276.                         <div class="obec-footer">  
  277.                             <span>📅 <?= htmlspecialchars($news['date'] ?: 'ข่าวประชาสัมพันธ์') ?></span>  
  278.                             <span class="obec-footer-tag">☑️ สพฐ.</span>  
  279.                         </div>  
  280.                     </div>  
  281.                 </a>  
  282.             <?php endforeach; ?>  
  283.         </div>  
  284.     <?php else: ?>  
  285.         <p style="color: #ef4444; background: #fee2e2; padding: 15px; border-radius: 8px;">  
  286.             ไม่พบรายการข่าว หรือเว็บไซต์ต้นทางมีการป้องกันการดึงข้อมูล (ตรวจสอบโครงสร้าง HTML ของหน้าเว็บต้นทาง)  
  287.         </p>  
  288.     <?php endif; ?>  
  289. </section>  
  290.   
  291. </body>  
  292. </html>  


Not Rated stars เฉลี่ย : Not Rated จาก 0 ครั้ง.            


comments




Leave a comments

ชื่อ/Email :
CAPTCHA ใส่รหัสที่ท่านเห็นลงในช่องนี้
กรุณาใช้คำพูดที่สุภาพ และอย่าใช้คำพูดที่พาดพิงถึงบุคคลอื่นให้เสียหาย ขอขอบคุณที่ให้ความร่วมมือ




ข้อความที่ท่านได้อ่าน เกิดจากการเขียนโดยสาธารณชน และส่งขึ้นมาแบบอัตโนมัติ เจ้าของระบบไม่รับผิดชอบต่อข้อความใดๆทั้งสิ้น เพราะไม่สามารถระบุได้ว่าเป็นความจริงหรือ ชื่อผู้เขียนที่ได้เห็นคือชื่อจริง ผู้อ่านจึงควรใช้วิจารณญาณในการกลั่นกรอง และถ้าท่านพบเห็นข้อความใดที่ขัดต่อกฎหมายและศีลธรรม กรุณาแจ้งที่ suriyaphop@gmail.com เพื่อให้ผู้ควบคุมระบบทราบและทำการลบข้อความนั้น ออกจากระบบต่อไป