GSuggest PHP CLI Script

Joined
Sep 30, 2009
Messages
31
Reaction score
7
I munged together a php program to rotate through a list of proxy servers and hit Google Suggest for results based on a keyword.

For example:

Code:
# php gs.php tools
tools
tools of the trade
tools plus
tools for sale
tools of the mind
tools and toys
tools4cheap
tools independence mo
toolsnow
tools for working wood

The reason why I'm posting it here is because I read someone else has a script here that didn't work very well and the suggestions were to also rotate through proxy severs. Anyway, I though I would share what I munged together:

PHP:
<?php
/*
 * gsuggest - Will use a proxy file to randomize connections for google suggestions results.
 *
 * Create a proxy.list file in the formation of
 *  USERNAME:PASSWORD@IP:PORT
 *  USERNAME:PASSWORD@IP:PORT
 *
 *   Usage:  gsuggest <keyword>
 *
 * Captain Midnight - 03/24/14
 *   
 */

$proxyuse="true";

function gsuggest(&$keyword)
{
  if (isset($keyword) && $keyword !== "") {    

        $getcontents=file_get_contents("proxy.list");
        $proxydetails=explode("\n",$getcontents);

                $n=-1;

        for($gets=0;$gets<=count($proxydetails);$gets++)
        {

                        $exp=explode(":",$proxydetails[$gets]);
                        $expa=explode("@",$exp[1]);

                        $n++;
                        $authentication_keywords[$n]["username"] = $exp[0];
                        $authentication_keywords[$n]["password"] = $expa[0];
                        $authentication_keywords[$n]["ip"] = $expa[1];
                        $authentication_keywords[$n]["port"] = $exp[2];
        }

                $get_auth_keys = $authentication_keywords[array_rand($authentication_keywords)];
                $user = $get_auth_keys[username];
                $pass = $get_auth_keys[password];
                $ips = $get_auth_keys[ip];
                $port = trim($get_auth_keys[port]);
                $encode=$user.":".$pass;
                /*
                echo "++++++++++++++++\nDEBUG\n++++++++++++++++\nUSER = $user\nPASS = $pass\nIP = $ips\nPORT = $port\nENCODE = $encode\n";
                */

                /* $url = "http://suggestqueries.google.com/complete/search?output=toolbar&hl=en&q=$keyword"; */
                $url = "http://clients1.google.com/complete/search?output=toolbar&hl=en&q=$keyword";
                $str = str_replace(" ", "+",$url);

        /* if($_REQUEST['proxyuseset']) */
        if (isset($proxyuse) && $proxyuse == "true") {
                        $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $str);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_PROXYPORT, $port);
                curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
                curl_setopt($ch, CURLOPT_PROXY, $ips);
                curl_setopt($ch, CURLOPT_PROXYUSERPWD, $encode);
                 $data = (curl_exec($ch));
                 curl_close($ch);
        } /* proxyset */
        else {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $str);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                 $data = curl_exec($ch);
                 curl_close($ch);
                 $ips= " ";
        }/* noproxyset */

        $xml = simplexml_load_string("$data");

    foreach ($xml->children() as $child) {                               
      foreach ($child->suggestion->attributes() as $dta) {                                        
      echo $dta."\n";                    
      }
    }
  } /* keyword set */
} /* function */

gsuggest($argv[1]);

?>
 
Gotta love CLI's. 80% of the code I write is command-line interface.

Remember if you want to run this as a shell script in linux/unix systems you need to include the path to your PHP CLI binary at the top of the file. This path may differ depending on your system.
Code:
#!/usr/bin/php
 
Last edited:
Back
Top