Jump to content

Edit a PHP script to show Google search results totals


malone

Recommended Posts

Ok, so I have a bit of code but it's not working as it should, would one of you guys have a look at it and let me know where I'm going wrong?

 

Basically I need to be able to input several words or terms into the text area, each on a separate line and have the results displayed something like:

 

hello: 23,000.000

hello world: 19,000,000

world: 278,000,000

 

I also see that the script is not searching the words / terms inside quotes (EG. "hello world")

 

TIA

 

<html>
<body>
<?php
function my_fetch($url,$user_agent='Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)')
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}

$s = $_GET['s'];
if (isset($s))
{
echo "<p><i>Search for $s</i></p>";
    $s = urlencode($s);
    $data = my_fetch("http://www.google.com/search?hl=en&q=" . $s . "&btnG=Google+Search");

    $data = strip_tags($data);

    $find = 'Results 1 - 10 of about ';
    $find2 = ' for';

    $data = strstr($data, $find);

    $pos = strpos($data, $find2);


$search_number=substr($data,strlen($find), $pos-strlen($find));

echo "Total Results: $search_number";
}
else
{
?>

<form name="form1" id="form1" method="get" action="">
<div align="left">
<p>  <textarea name="s" type="text" id="s" rows="8" style="width:60%" /></textarea><br />
<input type="submit" name="Submit" value="Results" /></p>
Put "" around the string: <input type="checkbox" checked name="apos" value="true" /><br />
</div>
</form>
<p>
<?php
}
?>
</p>
</body>
</html>

Link to comment
Share on other sites

<html>
<body>
<?php
function my_fetch($url,$user_agent='Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)')
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}

$s = array();
$s = $_GET['s'];
if (isset($s) && $_GET['s'] != ""){
$exploded_text = explode("\n",$s);
foreach($exploded_text as $search_term){
$search_term = trim($search_term);
echo "<p><i>Search for $search_term</i></p>";
    $search_term = urlencode($search_term);
    $data = my_fetch("http://www.google.com/search?hl=en&q=" . $search_term . "&btnG=Google+Search");

    $data = strip_tags($data);

    $find = 'Results 1 - 10 of about ';
    $find2 = ' for';

    $data = strstr($data, $find);

    $pos = strpos($data, $find2);


$search_number=substr($data,strlen($find), $pos-strlen($find));

echo "Total Results: $search_number";
}
}
else
{
?>

<form name="form1" id="form1" method="get" action="">
<div align="left">
<p>  <textarea name="s" type="text" id="s" rows="8" style="width:60%" /></textarea><br />
<input type="submit" name="Submit" value="Results" /></p>
Put "" around the string: <input type="checkbox" checked name="apos" value="true" /><br />
</div>
</form>
<p>
<?php
}
?>
</p>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.