简体   繁体   English

PHP CURL有时返回空

[英]PHP CURL sometimes return empty

I want to ask something make me confuse. 我想问些让我感到困惑的事情。 I'am using CURL to get html code from this link 我正在使用CURL从此链接获取html代码

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

And This is my function 这是我的功能

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;
    }

The problem, sometimes i got return empty , i dont know this is from my server or from the site protection with i dont know, please tell me, thank you. 问题是,有时我返回的是空的 ,我不知道这是来自服务器还是我不知道的站点保护,请告诉我,谢谢。

CURLOPT_VERBOSE should reveal what happened. CURLOPT_VERBOSE应该显示发生了什么。 so just check if curl_exec fail, and if it does, throw a RuntimeException, then, next time it happens, check your php error logs. 因此,只需检查curl_exec是否失败,如果失败,则抛出RuntimeException,然后在下次发生时,检查您的php错误日志。 additionally you can check what curl_errno() and curl_error says. 另外,您可以检查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 );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM