<?php
function getData() {
$file = '';
// if form has been submitted
if (isset($_GET['file'])) {
// return GET
return array($_GET['file'], $_GET['numResults'], $_GET['displayType']);
}
// if form has not yet been submitted
else {
printForm();
}
}
function printForm() {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
Enter file <input type="text" name="file"><br>
Number of results per keyword <input type="text" name="numResults"><br>
Display type: <select name='displayType'>
<option value="1">HTML Table</option>
<option value="2">Semicolon-delimited, HTML view</option>
<option value="3">Semicolon-delimited, source view</option>
<input type="submit" name="submit" value="Go">
</form>
<p>
<?php
}
function getLinks($keyword, $num)
{
//$data = file_get_contents('http://www.google.com/search?hl=en&as_q='.trim($keyword).'&as_epq=&as_oq=&as_eq=&num='.$num.'&lr=&as_filetype=&ft=i&as_sitesearch=&as_qdr=all&as_rights=&as_occt=any&cr=&as_nlo=&as_nhi=&safe=off');
$data = file_get_contents('http://www.google.com/search?hl=en&as_q='.trim($keyword).'&as_epq=&as_oq=&as_eq=&num='.$num);
$regex = '/\<h3 class=r\>\<a href=\"(.+?)\"/';
preg_match_all($regex,$data,$match);
return $match[1];
}
$inputs = getData();
/////////////////////////
if (isset($_GET['file'])) {
//SECOND RUN START HERE//
$keywords = file($inputs[0]);
$numResults = $inputs[1];
$display = $inputs[2];
$results = array();
if ($display == 1)
{
echo '<table border="1">';
print "\n";
}
foreach($keywords as $currentWord)
{
$results = getLinks($currentWord, $numResults);
foreach($results as $url)
{
if ($display > 1)
{
echo trim($currentWord).';'.$url;
if ($display == 2)
{
echo '<br />';
}
print "\n";
}
else if ($display == 1)
{
echo '<tr><td>'.trim($currentWord).'</td><td>'.$url.'</td></tr>';
print "\n";
}
}
}
if ($display == 1)
{
echo '</table>';
print "\n";
}
}
?>