title: How can I find where I will be redirected using cURL in PHP?
body: <p>I'm trying to make curl follow a redirect but I can't quite get it to work right. I have a string that I want to send as a GET param to a server and get the resulting URL.</p>
  
  <p>Example:</p>
  
  <blockquote>
    <p>String = <em>Kobold Vermin</em><br>
    Url = <em>www.wowhead.com/search?q=Kobold+Worker</em></p>
  </blockquote>
  
  <p>If you go to that url it will redirect you to "www.wowhead.com/npc=257". I want curl to return this URL to my PHP code so that i can extract the "npc=257" and use it.</p>
  
  <p>Current code:</p>
  
  <pre><code>function npcID($name) {
      $urltopost = "http://www.wowhead.com/search?q=" . $name;
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
      curl_setopt($ch, CURLOPT_URL, $urltopost);
      curl_setopt($ch, CURLOPT_REFERER, "http://www.wowhead.com");
      curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:application/x-www-form-urlencoded"));
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
      return curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  }
  </code></pre>
  
  <p>This however returns <em>www.wowhead.com/search?q=Kobold+Worker</em> and not <em>www.wowhead.com/npc=257</em>.</p>
  
  <p>I suspect PHP is returning before the external redirect happens. How can I fix this?</p>