Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include "phpQuery/phpQuery.php";
- function crawl($url, $baseUrl='', $config = array())
- {
- $cookiePath="";
- $tempname = "cookie";
- if (!isset($config['get']))
- {
- $config['get'] = true;
- }
- if($cookiePath!='')
- if (file_exists($cookiePath))
- {
- $cookie_file = $cookiePath;
- }
- else
- {
- ($cookie_file_fd = fopen($cookiePath, 'w')) or dbgd('The cookie file could not be opened. Make sure this directory has the correct permissions');
- $cookie_file = $cookiePath;
- fclose($cookie_file_fd);
- chmod($cookiePath, 0755);
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_TIMEOUT, 200);
- if (isset($config['headers']))
- curl_setopt($ch, CURLOPT_HTTPHEADER, $config['headers']);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1');
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_REFERER, $baseUrl);
- //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- if (!isset($config['returntransfer']))
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- else
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, $config['returntransfer']);
- if ($cookiePath != "")
- {
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiePath);
- }
- if (isset($config['postdata']))
- {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $config['postdata']);
- }
- if ($config['get'] == false)
- {
- curl_setopt($ch, CURLOPT_HTTPGET, true);
- }
- if (isset($config['getInfo']))
- {
- print_r(curl_getinfo($ch));
- exit;
- }
- $data = curl_exec($ch);
- if ($data)
- return $data;
- else
- throw new CHttpException('404', 'Halaman tidak ditemukan');
- }
- $Link = "http://www.antaranews.com/rss/nasional";
- $data=crawl($Link);
- $doc=phpQuery::newDocumentHTML($data);
- $item=$doc->find("item");
- foreach($item as $itm){
- $itm=pq($itm);
- $title=$itm->find("title")->text();
- echo "<br>".$title."\n\n";
- //masukin fungsi save ke database disini
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement