Forum Moderators: coopster

Message Too Old, No Replies

Get google ads redirected real URL

redirected url

         

coredeveloper2013

9:04 am on Sep 9, 2014 (gmt 0)

10+ Year Member



Hi,
When we search in google there comes ads. I want to grab those URL of ads. I mean the real url when we click on those ads.

Thanks.

aakk9999

9:32 am on Sep 9, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld, coredeveloper2013.

What have you tried in order to achive what you ask?

coredeveloper2013

9:37 am on Sep 9, 2014 (gmt 0)

10+ Year Member



<?php
function follow_redirect($url){
$redirect_url = null;

if(function_exists("curl_init")){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
}
else{
$url_parts = parse_url($url);
$sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80));
$request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
$request .= 'Host: ' . $url_parts['host'] . "\r\n";
$request .= "Connection: Close\r\n\r\n";
fwrite($sock, $request);
$response = fread($sock, 2048);
fclose($sock);
}

$header = "Location: ";
$pos = strpos($response, $header);
if($pos === false){
return false;
}
else{
$pos += strlen($header);
$redirect_url = substr($response, $pos, strpos($response, "\r\n", $pos)-$pos);
return $redirect_url;
}
}

$url = 'http://www.google.com/aclk?sa=L&ai=CrCejFqkOVOKWOI-kugTN6oLYAvnJlJQFmZ-zhcEBsICmBhABKAVQtvqyowVgM8gBAakC66asdGZRgT6qBCZP0CBGn079-YW7-A7S8GWCAhS00aM1g5em2pp3jm0DQzu_bEXDIYAH19CNEogHAZAHAqgHpr4b&num=4&sig=AOD64_22epSqlycp5HsAdE2QbJccaNwJzw&rct=j&q=&ved=0CKIBENEM&adurl=http://example.net/accounting-software.html';

echo '<ol>';
while(($newurl = follow_redirect($url)) !== false){
echo '<li>', $url, '</li>';
$url = $newurl;
}
echo '</ol>';

echo '<a href="', $url, '">', $url, '</a>';
?>

[edited by: phranque at 5:51 pm (utc) on Sep 9, 2014]
[edit reason] exemplified domain [/edit]

aakk9999

8:53 pm on Sep 10, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you need to explain your problem rather than just posting the code.

What does not work? What is the result you are getting and what do you expect to get?

phranque

10:12 pm on Sep 10, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i would add some error checking/reporting to your code.

coredeveloper2013

2:42 am on Sep 11, 2014 (gmt 0)

10+ Year Member



More explanation of my query -
* Paste this URL to browser - [google.com...]
* You will see results with ads.
* Need to develop such solution which will return those URL of search results.

Special case - Click on any ads. They will be redirected to different url rather than the visible url.

See this image - [mediusware.com ]