<?
if(!$_GET['q']) {
die("You did not enter a query in the url.");
}
// Setup query, replace spaces with +'s
$query = str_replace(" ", "+", $_GET['q']);
// Grab the SERP's, if page 1 is not a full 100 links, don't grab page 2
$serp1 = getpage("http://www.google.com/search?q=$query&num=100");
preg_match_all("/<a href=\"([^\"]*?)\" class=l/", $serp1, $links1);
if(count($links1[1])==100) {
$serp2 = getpage("http://www.google.com/search?q=$query&num=100&start=100");
preg_match_all("/<a href=\"([^\"]*?)\" class=l/", $serp2, $links2);
$links = array_merge($links1[1], $links2[1]);
} else {
$links = $links1[1];
}
// Implode link array, save to text file named after the query (Including + signs)
$linklist = implode("\n", $links);
$txt = fopen("$query.txt", "w");
fwrite($txt, $linklist);
fclose($txt);
// Output a link to the text file
echo "<a href='$query.txt'>$query.txt</a>";
// Curl Function
function getpage($url) {
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.5";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
curl_close($ch);
return $page;
}
?>