Created
June 12, 2019 05:44
-
-
Save ahmadbasarudin/ba17e0a6eb5f3b18ac45e25c45b44274 to your computer and use it in GitHub Desktop.
grabbing berita pertanian
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function renderRSS($a_item) | |
| { | |
| header("Content-Type: application/rss+xml; charset=UTF-8"); | |
| $xml = new SimpleXMLElement('<rss/>'); | |
| $xml->addAttribute("version", "2.0"); | |
| $channel = $xml->addChild("channel"); | |
| $channel->addChild("title", "Your feed title"); | |
| $channel->addChild("link", "Your website's uri"); | |
| $channel->addChild("description", "Describe your feed"); | |
| $channel->addChild("language", "en-us"); | |
| if(isset($a_item[0])) | |
| { | |
| foreach ($a_item as $entry) | |
| { | |
| $item = $channel->addChild("item"); | |
| $item->addChild("title", $entry['text']); | |
| $item->addChild("link", $entry['href']); | |
| $item->addChild("description", $entry['text']); | |
| } | |
| } | |
| echo $xml->asXML(); | |
| } | |
| function readHTML($url){ | |
| // inisialisasi CURL | |
| $data = curl_init(); | |
| // setting CURL | |
| curl_setopt($data, CURLOPT_RETURNTRANSFER, 1); | |
| curl_setopt($data, CURLOPT_URL, $url); | |
| // menjalankan CURL untuk membaca isi file | |
| $hasil = curl_exec($data); | |
| curl_close($data); | |
| return $hasil; | |
| } | |
| $url_target = "http://www.pertanian.go.id/home/?show=news&act=view_all&cat=5"; | |
| $html_code = readHTML($url_target); | |
| //echo $html_code; | |
| $links = array(); | |
| $dom = new DOMDocument; | |
| libxml_use_internal_errors(true); | |
| $content_top = explode("<div class=\"media-body\">", $html_code); | |
| if(isset($content_top[1])) | |
| { | |
| for($i=1;$i<count($content_top);$i++) | |
| { | |
| $a_content = explode("</div>", $content_top[$i]); | |
| $dom->loadHTML($a_content[0]); | |
| $arr = $dom->getElementsByTagName("a"); | |
| foreach($arr as $item) { | |
| $href = $item->getAttribute("href"); | |
| $text = trim(preg_replace("/[\r\n]+/", " ", $item->nodeValue)); | |
| $links[] = [ | |
| 'href' => $href, | |
| 'text' => $text | |
| ]; | |
| } | |
| } | |
| libxml_clear_errors(); | |
| } | |
| renderRSS($links); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment