Dmitry Makovetskiyd
Dmitry Makovetskiyd

Reputation: 7053

Retrieving a page of that has a redirect

Okay updated it all. I use this function:

    private function get_follow_url($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $a = curl_exec($ch);


    if(preg_match('Location:(.*)', $a, $r)){
        $url=trim($r[1]);
        $this->get_follow_url($url);
    }
  return $a;
}

I get this when it is echoed:

HTTP/1.1 301 Moved Permanently Server: nginx/0.7.67 Date: Sun, 14 Oct 2012 10:03:21 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.2.17 X-Pingback: http://thesexguy.com/xmlrpc.php Location: http://example.com/mature Content-Length: 0

So I do a recursion.. and try to fetch the page again after scraping the Location word...

It should take me to http://example.com/mature on the recursion? am I right? But I fail to scrape the location word..why?

Upvotes: 0

Views: 59

Answers (1)

Vyktor
Vyktor

Reputation: 20997

You need to either use CURLOPT_FOLLOWLOCATION or set cURL to retrieve full headers (CURLOPT_HEADER) and parse Location headers.

Upvotes: 2

Related Questions