简体   繁体   中英

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. 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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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