Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $query_ru = 'средства от простуды';
- $qu = urlencode($query_ru);
- // логин изменён на AAAAAAAAAAAAAAAA!!!
- $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');
- if ($url_link) {
- // echo "Fetching {$url_link}...<br/>\r\n";
- $main = curl_init($url_link);
- //шлем заголовки
- curl_setopt($main, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($main, CURLOPT_USERAGENT, 'Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.7.62 Version/11.01');
- // Выполняем запрос и приводим к одной кодировке
- $html = curl_exec($main);
- ######################### Определение кодировки #############################################################
- @$str_cp1251 = iconv('UTF-8', 'Windows-1251', $html);
- $kod1 = preg_match('#.#u', $html);
- $kod2 = preg_match('#.#u', $str_cp1251);
- if ($kod1 == 0) { // если $kod1 равен нулю то это 1251, конвертируем её
- $html = iconv("WINDOWS-1251", "UTF-8", $html);
- }
- else {
- /* если $kod1 не равен нулю то это UTF-8 подробнее о кодировках тут: http://habrahabr.ru/post/107945/#comment_3411725 */
- }
- }
- //echo "<pre>"; var_dump($html); echo "</pre>";
- // Внутри title и decription там ещё есть странные теги, их надо удалить
- $html = str_replace('<hlword>', ' ', $html);
- $html = str_replace('</hlword>', ' ', $html);
- $html = str_replace('<passage>', ' ', $html);
- $html = str_replace('</passage>', ' ', $html);
- preg_match_all('/<doc[^>]+>(.*?)<\/doc>/ism', $html, $top);
- echo "<pre>"; var_dump($top[0]); echo "</pre>";
- for($i = 0; $i < count($top[0]); $i++){
- preg_match_all('/<url>(.*?)<\/url>/ism', $top[0][$i], $url);
- echo $url[1][0]."<br>";
- preg_match_all('/<title>(.*?)<\/title>/ism', $top[0][$i], $title);
- echo $title[1][0]."<br>";
- preg_match_all('/<passages>(.*?)<\/passages>/ism', $top[0][$i], $passages);
- echo $passages[1][0]."<br>";
- echo "<br><br>";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement