Correct PHP way to check if external image exists?

后端 未结 9 1490
清歌不尽
清歌不尽 2021-02-04 08:49

I know that there are at least 10 the same questions with answers but none of them seems to work for me flawlessly. I\'m trying to check if internal or external image exists (is

9条回答
  •  青春惊慌失措
    2021-02-04 09:36

    I know you wrote "without curl" but still, somebody may find this helpfull:

    function curl_head($url) {
    
        $ch = curl_init($url);
    
        //curl_setopt($ch, CURLOPT_USERAGENT, 'Your user agent');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1); # get headers
        curl_setopt($ch, CURLOPT_NOBODY, 1); # omit body
        //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); # do SSL check
        //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); # verify domain within cert
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); # follow "Location" redirs
        //curl_setopt($ch, CURLOPT_TIMEOUT_MS, 700); # dies after 700ms
    
        $result = curl_exec($ch);
    
        curl_close($ch);
    
        return $result;
    }
    
    print_r(curl_head('https://www.example.com/image.jpg'));
    

    You will see someting like this HTTP/1.1 200 OK or HTTP/1.1 404 Not Found in returned header array. You can do also multiple parallel requests with curl multi.

提交回复
热议问题