Example #1
0
 /**
  * Search method
  * @param $search_string
  * @param array $options
  * * ! `pn` search post parameter must be in cp1251 encoding
  * @return array with founded items or array with error
  */
 public function search($search_string, $options = array())
 {
     $errors = array();
     $search_string = trim($search_string);
     //post type for first search, get type for subsequent
     $curl = curl_init();
     if (self::$is_search_first_run) {
         curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_FOLLOWLOCATION, 1, CURLOPT_USERAGENT, self::$user_agent, CURLOPT_URL => self::$main_page . "tracker.php", CURLOPT_POST => true, CURLOPT_HEADER => false, CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => 120, CURLOPT_TIMEOUT => 120, CURLOPT_MAXREDIRS => 10, CURLOPT_ENCODING => 'gzip,deflate', CURLOPT_COOKIESESSION, 1));
     } else {
         $params = "?search_id=" . $options[0]['search_id'] . "&start=" . $options[0]['start'] . "&nm=" . $options[0]['nm'];
         curl_setopt_array($curl, array(CURLOPT_URL => self::$main_page . "tracker.php" . $params, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION, true, CURLOPT_AUTOREFERER => true, CURLOPT_CONNECTTIMEOUT => 120, CURLOPT_TIMEOUT => 120, CURLOPT_HEADER => false, CURLOPT_MAXREDIRS => 10, CURLOPT_ENCODING => 'gzip,deflate'));
     }
     curl_setopt($curl, CURLOPT_COOKIEJAR, realpath(self::$cookies));
     curl_setopt($curl, CURLOPT_COOKIEFILE, realpath(self::$cookies));
     //if exist any search options
     if (empty($options)) {
         curl_setopt($curl, CURLOPT_POSTFIELDS, array("nm" => $search_string));
     } elseif (self::$is_search_first_run) {
         curl_setopt($curl, CURLOPT_POSTFIELDS, $options);
     }
     if (!($resp = curl_exec($curl))) {
         trigger_error(curl_error($curl));
         return $errors['error'] = "<br> <h2 class='align-center'>Неправильный запрос</h2> <br>";
     }
     curl_close($curl);
     if (self::$is_search_first_run) {
         $search_result = $this->parse_search_result($resp, $search_string);
         if (isset($search_result['pages']) && isset($search_result['search_id'])) {
             self::$is_search_first_run = false;
             $id = $search_result['search_id'];
             $pages = $search_result['pages'];
             for ($i = 1; $i < $pages; $i++) {
                 $arr = $this->search($search_string, array(array("nm" => $search_string, "start" => $i * 50, "search_id" => $id)));
                 $search_result = array_merge($search_result, $arr);
             }
             self::$is_search_first_run = true;
             return $search_result;
         } else {
             return $search_result;
         }
     } else {
         return $this->parse_search_result($resp, $search_string);
     }
 }