Using PHP how do I get the final url destination from Google search result? For example
I want to get
http://www.windowsphone.com/en-us/how-to/wp8/start/whats-new-in-windows-phone
from
http://www.google.com.my/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CC4QFjAA&url=http%3A%2F%2Fwww.windowsphone.com%2Fen-us%2Fhow-to%2Fwp8%2Fstart%2Fwhats-new-in-windows-phone&ei=U0-vUa7HDseOrQenzIGIAg&usg=AFQjCNE7USUy5FcCHAIPreZ7UXBCQ8JUcg&sig2=NwHO-_b51du7XRiknzniXA&bvm=bv.47380653,d.bmk
UPDATE
So far I've tried the following but it does not work for Google search result links
<?php
$ch = curl_init("http://www.google.com.my/url?sa=t&etc...");
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);
$header = "Location: ";
$pos = strpos($response, $header);
$pos += strlen($header);
$redirect_url = substr($response, $pos, strpos($response, "\r\n", $pos)-$pos);
echo $redirect_url;
?>
How about this. Little less code. No foreach needed.
$url = "http://www.google.com.my/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CC4QFjAA&url=http%3A%2F%2Fwww.windowsphone.com%2Fen-us%2Fhow-to%2Fwp8%2Fstart%2Fwhats-new-in-windows-phone&ei=U0-vUa7HDseOrQenzIGIAg&usg=AFQjCNE7USUy5FcCHAIPreZ7UXBCQ8JUcg&sig2=NwHO-_b51du7XRiknzniXA&bvm=bv.47380653,d.bmk";
$query = parse_url($url, PHP_URL_QUERY );
parse_str($query, $link);
echo $link["url"];