2014-02-17 79 views
0

我正在嘗試從網站獲取鏈接。當我嘗試通過終端進行連接時,我收到此消息,您必須打開瀏覽器中的JavaScript和Cookie支持才能訪問此網站。我已經嘗試了所有不同的代碼在這裏在stackoverflow和所有谷歌周圍。沒有人按我希望的方式工作。他們中沒有人從我的網站獲取任何數據,我試圖從中獲取數據。其他網站的工作。無法使用cURL獲取html鏈接

<?php 

function get_url($url, $javascript_loop = 0, $timeout = 5) 
{ 
    $url = str_replace("&amp;", "&", urldecode(trim($url))); 

    $cookie = tempnam ("/tmp", "CURLCOOKIE"); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1"); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_ENCODING, ""); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); # required for https urls 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10); 
    $content = curl_exec($ch); 
    $response = curl_getinfo($ch); 
    if(curl_exec($ch) === false) 
    { 
     echo 'Curl error: ' . curl_error($ch); 
    } 
    curl_close ($ch); 

    if ($response['http_code'] == 301 || $response['http_code'] == 302) 
    { 
     ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1"); 

     if ($headers = get_headers($response['url'])) 
     { 
      foreach($headers as $value) 
      { 
       if (substr(strtolower($value), 0, 9) == "location:") 
        return get_url(trim(substr($value, 9, strlen($value)))); 
      } 
     } 
    } 

    if ( (preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value)) && 
      $javascript_loop < 5 
    ) 
    { 
     return get_url($value[1], $javascript_loop+1); 
    } 
    else 
    { 
     return array($content, $response); 
    } 
} 
$test = get_url('http://livefootball.ws'); 

print_r($test); 

?> 

如果我切換到其他網站的網址,我得到的結果,但與這個網站,它不工作。任何幫助,將不勝感激。

回答

0

嘗試設置CURLOPT_COOKIEFILE也指向您的$cookie並確保您具有服務器能夠寫入該文件的權限。這可能會照顧cookie問題。但就Javascript問題而言,我認爲你運氣不好。

How to simulate that JavaScript is enabled with PHP Curl?

+0

我已經設法從我的mac的終端cURL得到輸出。所以這意味着它應該在沒有Javascript問題的情況下工作。但仍然不能使用cURL的PHP​​版本,即使我放了一個CURLOPT_COOKIEFILE。 – user3321206

+0

好吧,我只是嘗試用cURL連接到網站'livefootball.ws'並得到這個錯誤信息'CURL Error(http://livefootball.ws):失敗連接到livefootball.ws:80;沒有錯誤'。然後我嘗試在瀏覽器中連接到它,並且得到了「無法連接--Firefox無法建立與livefootball.ws.服務器的連接」。檢查以確保URL正確,如果可以,請訪問它。 – Quixrick

+0

該URL在我的瀏覽器中正常工作,奇怪它不適合你? – user3321206