Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.44 KB | None | 0 0
  1. <?php
  2.     include("../simple_html_dom/simple_html_dom.php");
  3.     include("config.php");
  4.     include("mysql.php");
  5.     include("parser.php");
  6.     $perPage = 3;
  7.     function GetFeedJSON($url, $type, $action, $p) {
  8.         $headers = [
  9.             "Accept: application/json, text/javascript, */*; q=0.01",
  10.             "Accept-Encoding: gzip, deflate",
  11.             "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
  12.             "Host: www.eip.ru",
  13.             "Origin: http://www.eip.ru",
  14.             "Referer: $url",
  15.             "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
  16.         ];
  17.         $file = tempnam("", "yparse_");
  18.        
  19.         $ch = curl_init("$url?type=$type&action=$action&page=$p");
  20.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
  21.         //curl_setopt($ch, CURLOPT_VERBOSE, true);
  22.         curl_setopt($ch, CURLOPT_HEADER, 1);
  23.         curl_setopt($ch, CURLOPT_COOKIEJAR, $file);
  24.         $data = curl_exec($ch);
  25.        
  26.         preg_match_all('/name\=\"\_csrf\"\s*value\=\"(.+\=\=)\"/ium', $data, $res);
  27.         $_csrf = $res[1][0];
  28.        
  29.         $fields =
  30.             'address=&'.
  31.             'price_from=&'.
  32.             'price_to=&'.
  33.             'num=&'.
  34.             'square_from=&'.
  35.             'square_to=&'.
  36.             'square_living_from=&'.
  37.             'square_living_to=&'.
  38.             'square_kitchen_from=&'.
  39.             'square_kitchen_to=&'.
  40.             'subject=&'.
  41.             'house_type=&'.
  42.             'san_uzel=&'.
  43.             'floor_from=&'.
  44.             'floor_to=&'.
  45.             'view=&'.
  46.             'region_id=0&'.
  47.             "page=$p&".
  48.             'order_by=&'.
  49.             'user_id=&'.
  50.             'order_dir=&'.
  51.             "_csrf=$_csrf&".
  52.             'json=1'
  53.         ;
  54.         rtrim($fields, '&');
  55.        
  56.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
  57.         //curl_setopt($ch, CURLOPT_VERBOSE, true);
  58.         curl_setopt($ch, CURLOPT_HEADER, 0);
  59.         curl_setopt($ch, CURLOPT_URL, $url);
  60.         curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  61.         curl_setopt($ch, CURLOPT_COOKIEJAR, $file);
  62.        
  63.         $data = curl_exec($ch);
  64.         print_r(json_decode($data)->data);
  65.         exit();
  66.        
  67.        
  68.     }
  69.    
  70.     function ParseFeed($url){
  71.         $html = file_get_html($url);       
  72.         $urls = [];
  73.        
  74.         foreach ($html->find("tr td[class=address_table] a") as $a) {
  75.             array_push($urls, "http://www.eip.ru" . $a->href);
  76.         }
  77.         return $urls;
  78.     }
  79.     function ParseAid($url, $DBtype){
  80.         $html = file_get_html($url);
  81.        
  82.         return [
  83.             'phone'         => $html->find("i[class=phone]", 0)->plaintext,
  84.             'description'   => $html->find("div[class=description]", 0)->plaintext,
  85.             'price'         => $html->find("div[class=price_block] div div", 0)->plaintext,
  86.             'street'        => $html->find("h1", 0)->plaintext,
  87.             'type'          => $DBtype,
  88.             'source'        => $url
  89.         ];
  90.     }
  91.     function GetAidFromJSON($json, $DBtype){
  92.         return [
  93.             'phone'             => $json->phone,
  94.             'description'       => $json->description,
  95.             'price'             => $json->price,
  96.             'street'            => $json->address,
  97.             'type'              => $DBtype,
  98.             'source'            => "http://www.eip.ru/moskva/detail/" . $json->id
  99.         ];
  100.     }
  101.     function ParserDriver($table, $DBtype, $feedUrl){
  102.         global $perPage;
  103.         $urls = ParseFeed($feedUrl);
  104.        
  105.         $count = 0;
  106.         foreach ($urls as $url) {
  107.             $count++;
  108.             if ($count > $perPage) break;
  109.             $aid = ParseAid($url, $DBtype);
  110.            
  111.             InsertToPTable($table, $aid);
  112.         }
  113.     }
  114.     /*ParserDriver('aidp', 2, "http://www.eip.ru/moskva/flats/sale");
  115.     ParserDriver('aidp', 1, "http://www.eip.ru/moskva/flats/rent");
  116.     ParserDriver('commercep', 2, "http://www.eip.ru/moskva/offices/sale");
  117.     ParserDriver('commercep', 1, "http://www.eip.ru/moskva/offices/rent");
  118.     ParseFeedCurl('http://www.eip.ru/moskva/flats/rent');*/
  119.     print_r(GetFeedJSON('http://www.eip.ru/moskva/flats/rent', 'flats', 'rent', 8));
  120. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement