Last active
December 9, 2016 06:41
-
-
Save sxidsvit/229df28095e03a008cf049326ed16a63 to your computer and use it in GitHub Desktop.
Класс для работы с БД на основе PDO. + CURL + парсинг сайтов
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
| ================================ SQL.php -класс для работы с БД. Bспользутся PDO ======================================== | |
| ==== PHP Data Objects. Данное расширение предоставляет доступ к Базам Данных используя объекты ======= | |
| <?php | |
| class SQL { | |
| private static $instance = null; | |
| private $db ; | |
| public static function Instance() { | |
| if(self::$instance == null) { | |
| self::$instance = new SQL(); | |
| } | |
| return self::$instance; | |
| } | |
| private function __construct() { | |
| setlocale(LC_ALL, 'ru_RU.UTF8'); | |
| $this->db = new PDO('mysql:host=localhost;dbname=parser-free', 'root', ''); | |
| $this->db->exec('SET NAMES UTF8'); | |
| $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
| } | |
| public function Select($query) { | |
| $q = $this->db->prepare($query); | |
| $q->execute(); | |
| if($q->errorCode() != PDO::ERR_NONE) { | |
| $info = $q->errorInfo(); | |
| die($info[2]); | |
| } | |
| return $q->fetchAll(); | |
| } | |
| public function Insert($table, $object) { | |
| $columns = array(); | |
| foreach($object as $key => $value) { | |
| $columns[] = $key; | |
| $masks[] = ":$key"; | |
| if($value === null) { | |
| $object[$key] = 'NULL'; | |
| } | |
| } | |
| $columns_s = implode(',', $columns); | |
| $masks_s = implode(',', $masks); | |
| $query = "INSERT INTO $table ($columns_s) VALUES ($masks_s)"; | |
| // echo($query); | |
| $q = $this->db->prepare($query); | |
| $q->execute($object); | |
| if($q->errorCode() != PDO::ERR_NONE) { | |
| $info = $q->errorInfo(); | |
| die($info[2]); | |
| } | |
| return $this->db->lastInsertId(); | |
| } | |
| public function Update($table, $object, $where) { | |
| $sets = array(); | |
| foreach($object as $key => $value) { | |
| $sets[] = "$key=:$key"; | |
| if($value === null) { | |
| $object[$key] = 'NULL'; | |
| } | |
| } | |
| $sets_s = implode(',' , $sets); | |
| $query = "UPDATE $table SET sets_s WHERE $where"; | |
| $q = $this->db->prepare($query); | |
| $q->execute($object); | |
| if($q->errorCode() != PDO::ERR_NONE) { | |
| $info = $q->errorInfo(); | |
| die($info[2]); | |
| } | |
| return $q->rowCount(); | |
| } | |
| public function Delete($table, $where) { | |
| $query = "DELETE FROM $table WHERE $where"; | |
| $q = $this->db->prepare($query); | |
| $q->execute($object); | |
| if($q->errorCode() != PDO::ERR_NONE) { | |
| $info = $q->errorInfo(); | |
| die($info[2]); | |
| } | |
| return $q->rowCount(); | |
| } | |
| } | |
| ?> | |
| ==================================================== конец SQL-класс ==================================================== | |
| ==================================================== curl_querry.php ==================================================== | |
| <?php | |
| // https://htmlweb.ru/php/php_curl.php - как работать с curl | |
| function curl_get($url, $referer = 'http://www.google.com') { | |
| $cookie = "_pw=123abc"; | |
| $ch = curl_init(); | |
| curl_setopt($ch, CURLOPT_URL, $url); | |
| curl_setopt($ch, CURLOPT_HEADER, 1); // Установите эту опцию в ненулевое значение, если вы хотите, чтобы шапка/header включалась в вывод | |
| curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.1 (Windows NT 7.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"); | |
| curl_setopt($ch, CURLOPT_REFERER, $referer); | |
| curl_setopt($ch, CURLOPT_COOKIE, $cookie); //Передаёт строку с содержимым куки/cookie, установленным в HTTP header"е | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| $data = curl_exec($ch); | |
| curl_close($ch); | |
| return $data; | |
| } | |
| ?> | |
| ==================================================== END curl_querry.php ==================================================== | |
| ==================================================== index.php ==================================================== | |
| <?php | |
| include_once('lib/SQL.php'); | |
| include_once('lib/curl_querry.php'); | |
| include_once('lib/simple_html_dom.php'); | |
| $sql = SQL::Instance(); // Подключаемся к БД | |
| // echo gettype($sql)."- в файле index.php переменная sql имеет такой тип <br>"; | |
| $html = curl_get('http://ntschool.ru/courses'); | |
| $dom = str_get_html($html); | |
| $courses = $dom->find('.one_title'); | |
| foreach($courses as $course) { | |
| $tobd = array('id_school' => 1); | |
| $a = $course->find('a', 0); | |
| $tobd['name'] = $a->plaintext; | |
| $page = curl_get('http://ntschool.ru'.$a->href); | |
| $dom_cost = str_get_html($page); | |
| $cost = $dom_cost->find('.cost',0); | |
| $tobd['cost'] = (int)$cost->plaintext; | |
| //$sql->Insert('courses', $tobd); | |
| echo $a->plaintext.' - '.$a->href.' - '.$cost->plaintext."<br>"; | |
| } | |
| // Отладка | |
| // $html = file_get_contents('http://ntschool.ru/courses'); | |
| // file_put_contents('result.html', $html); | |
| // file_put_contents('ref_page.html', $page); | |
| // echo $html; | |
| // file_put_contents('ref_page_'.$i.'.html', $page); | |
| // break; | |
| ==================================================== END index.php ==================================================== | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment