Targeted Adword Ads - something I whipped up

Status
Not open for further replies.

emp

New member
Jun 29, 2006
7,467
211
0
There was another thread talking about how to target adword ads to specific sites.

Now here is a bit of code.
I know it is an ugly bitch, but I have no time to play with it any more right now.

You need: PHP and curl.

Code:
<h2>Google Ad Crawl</h2>

<?php

    $GooglePrefix = "http://www.google.com/search?q=";
    $GoogleCountSuffix ="&start=";
    $query ="car repair";
    $loop = 0;
    echo "Looking for ".$query."<br>";
    while ($loop<= 50)
    {
        $CompleteUrl = $GooglePrefix.$query.$GoogleCountSuffix.$loop;
        $res = $res.webFetcher($CompleteUrl);
        $loop = $loop+10;
    }
    echo "<hr>";
    $regx = do_reg($res, "/h2.class=r.*(http.*)\"/U");
    /* Now we want to fetch all those URLs*/
        for ($i = 0; $i < count($regx); $i++) 
        {
            $text = $regx[$i];
            $comp = webFetcher($text);
            if (preg_match("/google_ad/", $comp, $matches)) 
            {
                echo "Google ad code found! <a href=".$text.">".$text."</a><br>";
            }
        }
    
    function do_reg($text, $regex)
    {
        preg_match_all($regex, $text, $regxresult, PREG_PATTERN_ORDER);
        return $regresult = $regxresult[1];
    }
    
    function webFetcher($url)
    {
        /* This does exactly what it is named after - it fetches a page from the web, just give it the URL */
        $crawl = curl_init();
        curl_setopt ($crawl, CURLOPT_URL, $url);
        curl_setopt($crawl, CURLOPT_RETURNTRANSFER, 1);
        $resulting = $resulting.curl_exec($crawl); 
        curl_close($crawl);    
        return $result = $resulting;
    }
?>
What it does? Well, it searches for the string in $query and gets the first 5 pages from Google. Then it crawls those results to see if adsense is installed. It then gives you the links for those sites.

I know it needs an input box, the variable names are shitty, etc, etc... blablabla...

As I said, no time right now.
Take it and play with it.

::emp::
 
  • Like
Reactions: fm1234 and elime


Thanks.

No idea how to work with the too long response times, so I am off to lab again.
If you have updates to this, feel free to post them here.

::emp::
 
Hansonso doing something for the community.

So was contacted by a guy called Hansonso about needing the code explained.

What was a first, is that he offered to donate for the help.
(No money has changed hands yet)

I was pleasantly surprised and offered the commented code for free, also here on the thread.

So here we go, not every single line, but in easy to digest blocks. I have put the comment into the code.

Code:
Code:
      <h2>Google Ad Crawl</h2>
<?php
    /* Setting the variables 
    A google Query looks like this:    
    so here are the variables I need for my  query:
    http://www.google.com/search?q=MYQUERY&start=MYSTART
    ¦-------------------GooglePrefix------¦-----query--¦suffix¦-counter--¦
    */
    $GooglePrefix = "http://www.google.com/search?q=";
    $query ="link building";
    $GoogleCountSuffix ="&start=";
    //-----------------------------
    echo "Looking for ".$query."<br>";
    /* Loop to get the Google result pages 
    While going through the loop, we build the query URL out of the parts and the loop counter 
    The results are stored in the $res variable.
    Basically, we get the complete source code for each result page, and store ALL of them in one looong string.
    */
    while ($loop<= 30)
    {
        $CompleteUrl = $GooglePrefix.$query.$GoogleCountSuffix.$loop;
        $res = $res.webFetcher($CompleteUrl); // we use the function webFetcher to get the page
        $loop = $loop+10;
    }
    echo "<hr>";
    /* Now we use regular expressions to filter the URLs out of the result pages
    For this, the function "do_reg" is called, giving it the complete resultstring and the regular expression.
    The returned value (an array of matches) is stored in $regx
    */
    $resultURLs = do_reg($res, "/h2.class=r.*(http.*)\"/U");
    /* Now we want to fetch all those URLs
    Again, we use a loop for this. Some more explanations in the loop itself.
    */
        for ($i = 0; $i < count($resultURLs); $i++) //we use the length of the returned array to count.
        {
            $text = $resultURLs[$i]; //$text is set to the item in the result we are at
            $comp = webFetcher($text); //we get the page at the URL
            if (preg_match("/google_ad/", $comp, $matches))
            /* again, we use aregular expression function.
            This time, we are looking for "google_ad", a code snippet that tells us that google ads are used in the page.
            If found, this is true.
            */
            {
                echo "Google ad code found! <a href=".$text.">".$text."</a><br>";
            }
        }
    
    function do_reg($text, $regex) //returns all the found matches in an array
    {
        preg_match_all($regex, $text, $regxresult, PREG_PATTERN_ORDER);
        return $regresult = $regxresult[1];
    }
    
    function webFetcher($url)
    {
        /* This does exactly what it is named after - it fetches a page from the web, just give it the URL */
        $crawl = curl_init(); //the curl library is initiated, the following lines set the curl variables
        curl_setopt ($crawl, CURLOPT_URL, $url); //The URL is set
        curl_setopt($crawl, CURLOPT_RETURNTRANSFER, 1); //Tells it to return the results in a variable
        $resulting = $resulting.curl_exec($crawl);  //curl is executed and the results stored in $resulting
        curl_close($crawl);     // closes the curl procedure.
        return $result = $resulting;
    }
?>
As a prerequisite, you need to have Apache, PHP and curl installed.
Search for "xampp" and you should have a windows machine set up in a matter of minutes.

For curl, you need to find the php.ini file and remove the comment in the line with curl in it.
(Just use Strg+F to find the line)

Of course, you can still donate :D

::emp::
 
This can probably be done using the Google API I would think also right ?


I tried uploading this to my server and running and I get: Look for link building, an <hr> and that is it.
 
On PHP5, Apache2 I am getting the following error back from Google:

Bad Request

Your client has issued a malformed or illegal request.
 
Hi all.

The post I am referring to is here:
http://www.wickedfire.com/affiliate-marketing/29209-800-roi-using-google-site-placement.html

@Lots of zeros.
You have no curl. exe to install. The libcurl is part of the PHP / XAMPP Distribution. The only thing you have to do is uncomment (remove the ') the line in the php.ini.

@ Patrick
Yes, I use the output to target my ads. See the referenced post.
No, this can not be done via the Google API as far as I know, but I might be mistaken.
As for the bad request, I have no idea what you are doing there. I am using this exact same script on my machine right now. What does your query string look like?

::emp::
 
You have no curl. exe to install. The libcurl is part of the PHP / XAMPP Distribution. The only thing you have to do is uncomment (remove the ') the line in the php.ini.

hmmm.. would it be the same with wamp?
I uncommented this line:
extension=php_curl.dll

and still get:
Fatal error: Call to undefined function curl_init() in C:\wamp\www\query.php on line 57

 
emp: I put some trace/echo code in there so you can see what is going on. Here is where I uploaded your script:

400 Bad Request


Keep in mind:
1. My server defaults to PHP5
2. I have apache2 installed.
 
@LotsofZeros

I did the same thing at first, you need to find the correct php.ini

Just type in "localhost" if you have a fresh install, you get a page with a menu to the left, etc..

One menu point is called "phpinfo" click that and find the path to your php.ini
Look for "Loaded Configuration File"

normally it should be at:
C:\xampp\apache\bin\php.ini

There is a duplicate one in the php directory, that one is NOT used.


@patrick.

Sorry, can't help you there. Anyone?
The script works fine at my end, but I use it locally.

::emp::
 
@LotsofZeros

I did the same thing at first, you need to find the correct php.ini

Just type in "localhost" if you have a fresh install, you get a page with a menu to the left, etc..

One menu point is called "phpinfo" click that and find the path to your php.ini
Look for "Loaded Configuration File"

normally it should be at:
C:\xampp\apache\bin\php.ini

There is a duplicate one in the php directory, that one is NOT used.

I discovered with wamp you can even select php_curl from the menu in the system tray as well.

Thanks for the great script!
 
emp: Someone on a PHP forum pointed out that my query string was not URLencoded. So Google was not happy with "link building" it had to be "link%20building". Don't know how it is working on your end. I added one line at the top which fixed it for me:

$query ="link building";
$query = urlencode($query);
$GoogleCountSuffix ="&start=";

Thanks for the great script.
 
Wow. I love it. A direct reseller competitor of mine is running adsense on his site. Bwahahaha. Guess I have a first good target.
 
Status
Not open for further replies.