Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <meta charset="utf-8">
  2. <?php
  3.     class Curl {
  4.         public function pfs($url){
  5.             $result = $this->parse($url);
  6.             if(!$result)echo "error parse";
  7.             else{
  8.                 $result = $this->find($result);
  9.                 if(!$result)echo "error find";
  10.                 else{
  11.                     $result = $this->save($result);
  12.                     if(!$result)echo "error save<br>";
  13.                     else echo "Успешное сохранение<br>";
  14.                 }
  15.             }
  16.         }
  17.        
  18.         private function parse($url){
  19.             $curl = curl_init();
  20.            
  21.             curl_setopt($curl, CURLOPT_URL, $url);
  22.             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  23.             curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  24.  
  25.             $result = curl_exec($curl);
  26.             if ($result === false) {
  27.             return false;
  28.             } else {
  29.                 return $result;
  30.             }
  31.         }
  32.        
  33.         private function find($res){
  34.             preg_match_all("#<title>(.*?)</title>#su", "$res", $title);
  35.             $title = $title[1][0];
  36.            
  37.             preg_match_all("#<h1.*?>(.*?)</h1>#su", "$res", $h1);
  38.             $h1 = $h1[1][0];
  39.            
  40.             preg_match_all('#</h1>(.*?)<div\s+id="footer"\s*>#su', "$res", $text);
  41.             $text = $text[1][0];
  42.             $text = preg_replace("#<span.*?>(.*?)</span>#su", "$1", $text);    
  43.             $text = preg_replace('#<p\s+class=".*?">(.*?)</p>#su', "<p>$1</p>", $text);
  44.             $text = preg_replace('#<a.+?href=".*?".*?>(.*?)</a>#su', "$1", $text);     
  45.             $text = preg_replace('#<div.*?>#su', "", $text);   
  46.             $text = preg_replace('#</div>#su', "", $text); 
  47.            
  48.             $result = ['title' => $title, 'h1' => $h1, 'text' => $text];
  49.             return $result;
  50.         }
  51.        
  52.         private function save($res){
  53.             $host = 'localhost';
  54.             $database = 'pars';
  55.             $user = 'root';
  56.             $password = '';
  57.            
  58.             mysql_set_charset("utf-8");
  59.             $link = mysqli_connect($host, $user, $password, $database)
  60.                 or die("Ошибка " . mysqli_error($link));
  61.  
  62.             $query = "INSERT INTO parse (title, h1, text)
  63.             VALUES ('".$res['title']."', '".$res['h1']."', '".$res['text']."')";
  64.             $result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));    
  65.            
  66.             mysqli_close($link);
  67.             return true;
  68.         }
  69.     }
  70.    
  71.     $links = [
  72.     'http://theory.phphtml.net/exercises/advanced/php/parsing/rabota-s-bibliotekoj-curl-v-php/1/1.php',
  73.     'http://theory.phphtml.net/exercises/advanced/php/parsing/rabota-s-bibliotekoj-curl-v-php/1/2.php',
  74.     'http://theory.phphtml.net/exercises/advanced/php/parsing/rabota-s-bibliotekoj-curl-v-php/1/3.php',
  75.     'http://theory.phphtml.net/exercises/advanced/php/parsing/rabota-s-bibliotekoj-curl-v-php/1/4.php',
  76.     'http://theory.phphtml.net/exercises/advanced/php/parsing/rabota-s-bibliotekoj-curl-v-php/1/5.php'
  77.     ];
  78.    
  79.     foreach($links as $el){
  80.         $parse = new Curl;
  81.         $parse->pfs($el);
  82.     }
  83. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement