Advertisement
zero50x

Дорг 3

Oct 29th, 2017
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.26 KB | None | 0 0
  1. $query_ru = 'средства от простуды';
  2. $qu = urlencode($query_ru);
  3.  
  4. // логин изменён на AAAAAAAAAAAAAAAA!!!
  5. $url_link = trim('https://yandex.com/search/xml?user=AAAAAAAAAAAAAAAA&key=03.305237842:69c8d10033b113c29e6e4969c5ea0559&query='.$qu.'&l10n=en&sortby=tm.order%3Dascending&filter=strict&maxpassages=1&groupby=attr%3D%22%22.mode%3Dflat.groups-on-page%3D15.docs-in-group%3D1&page=1');
  6. if ($url_link) {
  7. // echo "Fetching {$url_link}...<br/>\r\n";
  8. $main = curl_init($url_link);
  9. //шлем заголовки
  10. curl_setopt($main, CURLOPT_RETURNTRANSFER, 1);
  11. curl_setopt($main, CURLOPT_USERAGENT, 'Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.7.62 Version/11.01');
  12.  
  13.  
  14.  
  15. // Выполняем запрос и приводим к одной кодировке
  16. $html = curl_exec($main);
  17.  
  18. ######################### Определение кодировки #############################################################
  19. @$str_cp1251 = iconv('UTF-8', 'Windows-1251', $html);
  20. $kod1 = preg_match('#.#u', $html);
  21. $kod2 = preg_match('#.#u', $str_cp1251);
  22.  
  23.     if ($kod1 == 0) { // если $kod1 равен нулю то это 1251, конвертируем её
  24.         $html = iconv("WINDOWS-1251", "UTF-8", $html);
  25.     }
  26.     else {
  27.         /* если $kod1 не равен нулю то это UTF-8 подробнее о кодировках тут: http://habrahabr.ru/post/107945/#comment_3411725 */
  28.     }
  29. }
  30.  
  31. //echo "<pre>"; var_dump($html); echo "</pre>";
  32.  
  33. // Внутри title и decription там ещё есть странные теги, их надо удалить
  34. $html = str_replace('<hlword>', ' ', $html);
  35. $html = str_replace('</hlword>', ' ', $html);
  36. $html = str_replace('<passage>', ' ', $html);
  37. $html = str_replace('</passage>', ' ', $html);
  38.  
  39. preg_match_all('/<doc[^>]+>(.*?)<\/doc>/ism', $html, $top);
  40.  
  41. echo "<pre>"; var_dump($top[0]); echo "</pre>";
  42.  
  43. for($i = 0; $i < count($top[0]); $i++){
  44.     preg_match_all('/<url>(.*?)<\/url>/ism', $top[0][$i], $url);
  45.     echo $url[1][0]."<br>";
  46.  
  47.     preg_match_all('/<title>(.*?)<\/title>/ism', $top[0][$i], $title);
  48.     echo $title[1][0]."<br>";
  49.  
  50.     preg_match_all('/<passages>(.*?)<\/passages>/ism', $top[0][$i], $passages);
  51.     echo $passages[1][0]."<br>";
  52.    
  53.     echo "<br><br>";
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement