为什么不卷曲获取此网页源代码?

时间:2019-03-30 17:43:00

标签: php http curl

我已经编写了一个PHP脚本来使用curl获取网站(http://primaire.recitus.qc.ca/sujets/13/personnages-marquants/3972)源代码。最初,一切似乎都像是一种魅力,但是一段时间后,我开始偶然发现一些无法获取的网站。通常返回的错误是

之类的东西
    Failed connect to primaire.recitus.qc.ca:80; Connection timed out

但是网站本身加载速度非常快。我将超时设置提高到一分钟,但是没有用。因此我认为问题出在我的脚本之外。然后,我检查了this website,以查看他们是否可以获取它,但两者均不能。这是我使用的脚本:

<?php
    header("Access-Control-Allow-Origin: *");
    header('Content-type: text/plain');
    $input = "http://primaire.recitus.qc.ca/sujets/13/personnages-marquants/3972";
    $method = $_SERVER['REQUEST_METHOD'];
    $ch = curl_init($input);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15');
    curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com');
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPGET, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 5);
    curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
    curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 2);
    if(curl_exec($ch) === false) {
        echo 'Curl error: ' . curl_error($ch);
    }
    $output = curl_exec($ch);
    curl_close($ch);
    echo $output;
?>

我不知道我是否缺少某些内容,但是在我看来,我已经用setopt涵盖了大多数异常,但是请告诉我我是否错了。任何帮助表示赞赏。

0 个答案:

没有答案
相关问题