2012-10-14 46 views
0

好吧更新了所有內容。我用這個功能:檢索該頁面的重定向

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; 
} 

我得到這個當附和道:

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 

所以我做一個遞歸..並嘗試刮定位字後再次讀取該網頁...

它應該帶我到遞歸http://example.com/mature?我對嗎?但我沒有刮掉位置的單詞..爲什麼?

+0

看看[Make curl follow redirects?](http://stackoverflow.com/questions/3519939/make-curl-follow-redirects) – Killrawr

回答

2

您需要使用CURLOPT_FOLLOWLOCATION或設置cURL來檢索完整標題(CURLOPT_HEADER)和解析Location標題。

+0

請看更新 –

+0

@DmitryMakovetskiyd你確定頁面發送任何重定向?因爲基於我所看到的不是。 – Vyktor

+0

好吧修好了,謝謝 –