2014-02-17 72 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