PHP CURL有时会返回空

时间:2017-12-07 01:00:01

标签: php curl

我想问一些令我困惑的事情。 我正在使用CURL从此链接获取HTML代码

echo set_user_agent_grab("https://www.bandros.co.id/produk/dress-atasan-baju-rajut-wanita-sad-500");

这是我的功能

function set_user_agent_grab($link){
    $headers = ["text/html; charset=UTF-8"];
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $link);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2');
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
            $result = curl_exec($ch);
            curl_close($ch);
            return $result;
    }

问题,有时我得到返回空,我不知道这是来自我的服务器或网站保护,我不知道,请告诉我,谢谢。

1 个答案:

答案 0 :(得分:1)

CURLOPT_VERBOSE应该揭示发生了什么。所以只检查curl_exec是否失败,如果是,则抛出RuntimeException,然后,下次发生时,检查你的php错误日志。另外你可以查看curl_errno()和curl_error的内容。

function set_user_agent_grab($link) {
    $headers = [ 
            "text/html; charset=UTF-8" 
    ];
    $ch = curl_init ();
    $debugfileh = tmpfile ();
    $debugfile = stream_get_meta_data ( $debugfileh ) ['uri'];
    try {
        curl_setopt ( $ch, CURLOPT_VERBOSE, 1);
        curl_setopt ( $ch, CURLOPT_STDERR, $debugfileh);

        curl_setopt ( $ch, CURLOPT_URL, $link );
        curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2' );
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $ch, CURLOPT_MAXREDIRS, 10 );
        curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 30 );
        curl_setopt ( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
        curl_setopt ( $ch, CURLOPT_ENCODING, 'gzip' );
        $result = curl_exec ( $ch );
        if (! is_string ( $result )) {
            $errstr = "curl_exec failed: " . curl_errno ( $ch ) . ": " . curl_error ( $ch ) . ". debuginfo: " . file_get_contents ( $debugfile );
            throw new RuntimeException ( $errstr );
        }
        return $result;
    } finally{
        fclose ( $debugfileh );
        curl_close ( $ch );
    }
}