Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

Google API - Get URL of search results

I'm trying to write a web application where I enter in a search query into a form, and get a list of just the url's of the search results printed on the page.

Could you please provide code examples of how to do this?
0
Breadstick
Asked:
Breadstick
  • 4
  • 2
  • 2
  • +2
1 Solution
 
dougdayCommented:
Your best bet is to do an HTTP query to google and parse the results, probably using regular expressions.
0
 
dougdayCommented:
At least that's your best *FREE* bet.
0
 
BreadstickAuthor Commented:
Hmm... no, I'm pretty sure that wouldn't be my best bet.  That would be slower, and I'm pretty sure illegal.

I believe with Google API and your api key, you're allowed a certain number of thousands of queries per day, for free.  I'm just not sure how to do it...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ShelfieldCollegeCommented:
Here's a few examples of using PHP with your google key:

http://www.googleduel.com/apiexample.php

http://www.soft-spot.co.uk/searchscript.htm

http://www.wwwsearchsolutions.com/google.php

I've not looked too closely at them, however they seem to cover what you're looking for.  Take a look and see how it's done and either use their code (if their policy allows it) or write your own.

Hope thats helped, I found these using a simple google search for "google API PHP"

Cheers

/Matt
0
 
keteracelCommented:
or look at the PAQ, I've done a pretty good one here:

http://www.experts-exchange.com/Web/Web_Languages/Q_21031637.html

The result of this question was the following script:

<?php
    function doGoogleSearch($searchString) {
     $fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
     $file = "";
     
     if (!$fp) {
        echo "$errstr ($errno)<br />\n";
     } else {
        $out = "GET /search?hl=en&ie=UTF-8&q=$searchString HTTP/1.1\r\n";
        $out .= "Host: www.google.com\r\n";
        $out .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225\r\n";
        $out .= "Connection: Close\r\n\r\n";
       
        fwrite($fp, $out);
       
        while (!feof($fp)) {
             $file .= fgets($fp, 128);
        }
           fclose($fp);
     }

     $sections = array();
     $start = $end = 0;
     $count = 0;

     while (($start = strpos($file, '<!--m-->', $end)) > $end && ($end = strpos($file, '<!--n-->', $start)) > $start) {
         $sections[$count] = substr($file, $start+8, $end - ($start+8));
         $count++;
     }

     $returnval = array();

     for ($i = 0; $i < $count; $i++) {
         $start = strpos($sections[$i], '=', 0);
         $end = strpos($sections[$i], '>', $start);
         $returnval[$i][0] = substr($sections[$i], $start+1, $end - ($start+1));
         
         $start = $end;
         $end = strpos($sections[$i], '</a>', $start);
         $returnval[$i][1] = substr($sections[$i], $start+1, $end - ($start+1));
         
         $start = strpos($sections[$i], '-1>', $end);
         $end = strpos($sections[$i], '<font', $start);
         $returnval[$i][2] = substr($sections[$i], $start+3, $end - ($start+3));

         //echo "link= " . $returnval[$i][0] . "<br>";
         //echo "title= " . $returnval[$i][1] . "<br>";
       
         if (strlen($returnval[$i][2]) <= 0 || strpos($returnval[$i][2], '<nobr>') > 0) {
             $returnval[$i][2] = "NO DESCRIPTION!";
            //echo "desc= NO DESCRIPTION<br><br>\n\n";
         }
         else {
          $returnval[$i][2] = preg_replace('/(<br>)/', ' ', $returnval[$i][2]);
             //echo "desc= " . $returnval[$i][2] . "<br><br>\n\n";
         }
     }
     return $returnval;
    }

    print_r(doGoogleSearch('jolene+blalock'));
?>

Hope this helps,

keteracel
0
 
dougdayCommented:
Hm... that's exactly what he said he did NOT want to do.
ShelfieldCollege has already provided some excellent resources for how to do this with SOAP.
-Doug
0
 
dougdayCommented:
I was unaware that google provided this service.  I guess you learn something new every day :)
0
 
BreadstickAuthor Commented:
Also keteracel, allow_url_fopen in the PHP configuration on my server has been turned off for security reasons, so I don't think I can use fsockopen.

Thanks for the links ShelfieldCollege, I'll take a look.
0
 
ShelfieldCollegeCommented:
Dougday I'm in a similar boat, I'd spotted something about API's when looking round google's site but never really looked into it, can see now how it could be quite useful if you wish to provide search features on your site.

Cheers

Matt
0
 
hujiCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: ShelfieldCollege {http:#13856896}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now