<?php
//Объявляем функцию парсинга выдачи
function parse_serp($engine, $query)
{
switch (strtolower($engine))
{
case 'google':
$request = 'http://www.google.com/search?hl=en&q='.urlencode(trim($query));
$pattern = '/cC[itIT]*eE/';
$result = file_get_contents($request);
if(preg_match_all($pattern, $result, $matches))
{
for ($i=0; $i<count($matches[0]); $i++)
{
$link = $matches[2][$i];
$serp[] = $link;
}
}
break;
case 'yahoo':
$request = 'http://search.yahoo.com/search?ei=UTF-8&n=100&b=0&vl=&p='.urlencode(trim($query));
$pattern = '/<a class=yschttl.*(http%3a\/\/.*)">(.*)<\/a>.*<div class=yschabstr>(.*)<\/div>/isU';
$result = file_get_contents($request);
if(preg_match_all($pattern, $result, $matches))
{
for ($i=0; $i<count($matches[0]); $i++)
{
$link = urldecode($matches[1][$i]);
$serp[] = $link;
}
}
break;
case 'msn':
$request = 'http://search.msn.com/results.aspx?count=50&first=0&mkt=&q='.urlencode(trim($query));
$pattern = '/<li.*><h3><a href=".*" gping="\/GLinkPing.aspx\?\/_1_9SE\/1\?(.*)&&.*">(.*)<\/a><\/h3><p>(.*)<\/p>/isU';
$result = file_get_contents($request);
if(preg_match_all($pattern, $result, $matches))
{
for ($i=1; $i<count($matches[0]); $i++)
{
$link = urldecode($matches[1][$i]);
$serp[] = $link;
}
}
break;
}
return $serp;
}
//Стартуем функцию парсинга
parse_serp("google", "cars");
//Выводим полученные результаты
print_r($result);
?>