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

发布于 2024-12-06 12:24:36 字数 1812 浏览 0 评论 0 原文

我正在尝试检测图像的 URL 在防火墙后面或经过身份验证的区域后面是否有效。下面是我编写的函数:

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

该代码在我们的开发 Windows 服务器上运行良好(返回所有正确的响应代码),但不幸的是它在我们的生产 Linux 服务器上不起作用。当图像位于经过身份验证的区域后面时(即 401 状态代码),Linux 服务器上基本上不会返回响应代码。响应是空白的。

有人遇到过同样的问题吗?如果是这样,我该如何修复它以便在我们的 Linux 服务器上返回正确的响应代码?

感谢您抽出时间。

I am trying to detect whether a URL to an image is valid, behind a firewall or behind an authenticated area. 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. Basically no response code is returned on the Linux server when an image is behind an authenticated zone (ie 401 status code). 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?

Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃→可爱长大的 2024-12-13 12:24:36

好的,我找到了解决方案。不确定它是最优雅的(我宁愿使用 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;
        }}

我希望这可以帮助其他遇到同样问题的人。

有关新功能的更多详细信息,请访问以下页面:http://hakre.wordpress.com/2011/09/17/head-first-with-php-streams/

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:

@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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文