【问题标题】:Unable to get html links with cURL无法使用 cURL 获取 html 链接
【发布时间】:2014-02-17 23:53:37
【问题描述】:

我正在尝试从网站获取链接。当我尝试通过终端连接时,我收到以下消息:“您必须在浏览器中打开 javascript 和 cookie 支持才能访问此站点”。我在 stackoverflow 和谷歌周围尝试了许多不同的代码。没有人按照我想要的方式工作。他们都没有从我试图从中获取数据的该网站获取任何数据。其他网站工作。

    <?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);

?>

如果我将 URL 切换到其他网站,我会得到结果,但在这个网站上它不起作用。任何帮助将不胜感激。

【问题讨论】:

    标签: javascript php html curl


    【解决方案1】:

    尝试将CURLOPT_COOKIEFILE 设置为也指向您的$cookie,并确保您拥有服务器能够写入该文件的权限。这可能会解决 cookie 问题。但就Javascript问题而言,我认为你不走运。

    How to simulate that JavaScript is enabled with PHP Curl?

    【讨论】:

    • 我已经设法从我的 mac 终端通过 cURL 获取输出。所以这意味着它应该在没有 Javascript 问题的情况下工作。但仍然无法使用 php 版本的 cURL,即使我设置了 CURLOPT_COOKIEFILE。
    • 好的,我刚刚尝试使用 cURL 连接到网站 livefootball.ws 并收到此错误消息 CURL Error (http://livefootball.ws): Failed connect to livefootball.ws:80; No error。因此,我尝试在浏览器中连接它并得到Unable to connect - Firefox can't establish a connection to the server at livefootball.ws. 检查以确保 URL 正确,如果正确,则可以访问它。
    • 该网址在我的浏览器中有效,奇怪的是它不适合您?
    猜你喜欢
    • 2022-01-02
    • 1970-01-01
    • 2015-11-24
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 2015-04-30
    相关资源
    最近更新 更多