简体   繁体   English

如何在Linux服务器上使用PHP CURL获取图像的有效响应代码?

[英]How do I get a valid response code for an image using PHP CURL on a Linux server?

I am trying to detect whether a URL to an image is valid, behind a firewall or behind an authenticated area. 我正在尝试检测图像的URL在防火墙之后或在经过身份验证的区域后是否有效。 Below is the function that I have written: 下面是我编写的函数:

private function pingImg($img){
    $found = FALSE;
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $img);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 7); 
    $result = curl_exec($curl);
    if($result !== false){
        if(curl_getinfo($curl, CURLINFO_HTTP_CODE) == "401"){
            $found = TRUE;
            $this->_httpBasicAuthImages = TRUE;
        } 
        //now check for invalid cert
        if(stripos($img, "https") !== FALSE){
            curl_close($curl);
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $img);
            curl_setopt($curl, CURLOPT_HEADER, true);
            curl_setopt($curl, CURLOPT_NOBODY, true);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
            curl_setopt($curl, CURLOPT_TIMEOUT, 7); 
            $result = curl_exec($curl);
            if(!$result) {
                $found = TRUE;
                $this->_invalidSSL = TRUE;
            }
        }
    } else {
        //stalled ping, probably behind a firewall
        $found = TRUE;
        $this->_firewallImg = TRUE;
    }
    curl_close($curl);
    return $found;
}

That code works great on our development Windows server (returns all the proper response codes) but unfortunately it does not work on our production Linux server. 该代码在我们的开发Windows服务器上很好地工作(返回所有正确的响应代码),但是不幸的是,它在我们的生产Linux服务器上不起作用。 Basically no response code is returned on the Linux server when an image is behind an authenticated zone (ie 401 status code). 基本上,当映像位于经过身份验证的区域之后时,Linux服务器上不会返回任何响应代码(即401状态代码)。 The response is blank. 响应为空白。

Has anyone encountered this same issue? 有没有人遇到过同样的问题? If so, how do I fix it so the proper response code will be returned on our Linux server? 如果是这样,我该如何解决它,以便正确的响应代码将在我们的Linux服务器上返回?

Thanks for your time. 谢谢你的时间。

OK, I found a solution. 好的,我找到了解决方案。 Not sure its the most elegant (I would rather use CURL for everything) but it works on the Linux server: 不知道它最优雅(我想对所有内容都使用CURL),但是它可以在Linux服务器上使用:

@file_get_contents($img, NULL, stream_context_create(array('http'=>array('method' => "HEAD",'follow_location' => 0,'timeout'=>7))));
    if (!empty($http_response_header)){
        $code = "";
        sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code);
        if($code == "401"){ 
            $found = TRUE;
            $this->_httpBasicAuthImages = TRUE;
        }}

I hope this helps anyone else encountering the same issue. 我希望这可以帮助其他任何遇到相同问题的人。

More details about the new functionality can be found on the following page: http://hakre.wordpress.com/2011/09/17/head-first-with-php-streams/ 可在以下页面上找到有关新功能的更多详细信息: http : //hakre.wordpress.com/2011/09/17/head-first-with-php-streams/

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

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