title: How to get the final url from google search result in PHP
body: <p>Using PHP how do I get the final url destination from Google search result? For example</p>
  
  <p>I want to get</p>
  
  <pre><code>http://www.windowsphone.com/en-us/how-to/wp8/start/whats-new-in-windows-phone
  </code></pre>
  
  <p>from</p>
  
  <pre><code>http://www.google.com.my/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;ved=0CC4QFjAA&amp;url=http%3A%2F%2Fwww.windowsphone.com%2Fen-us%2Fhow-to%2Fwp8%2Fstart%2Fwhats-new-in-windows-phone&amp;ei=U0-vUa7HDseOrQenzIGIAg&amp;usg=AFQjCNE7USUy5FcCHAIPreZ7UXBCQ8JUcg&amp;sig2=NwHO-_b51du7XRiknzniXA&amp;bvm=bv.47380653,d.bmk
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>So far I've tried the following but it does not work for Google search result links</p>
  
  <pre><code>&lt;?php
  $ch = curl_init("http://www.google.com.my/url?sa=t&amp;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;
  ?&gt;
  </code></pre>