Php uses the request header to obtain the remote Image size
- $ Fp = fsockopen ("www.baidu.com", 80, $ errno, $ errstr, 30 );
- If ($ fp ){
- // Set the request to HEAD.
- $ Out = "HEAD/img/baidu_sylogo1.gif HTTP/1.1 \ r \ n ";
- $ Out. = "Host: www.baidu.com \ r \ n ";
- $ Out. = "Connection: Close \ r \ n ";
- Fwrite ($ fp, $ out );
- While (! Feof ($ fp )){
- $ Header = fgets ($ fp );
- If (stripos ($ header, 'Content-length ')! = False ){
- $ Size = trim (substr ($ header, strpos ($ header, ':') + 1 ));
- Echo $ size;
- }
- }
- Fclose ($ fp );
- } Else {
- Echo "$ errstr ($ errno )";
- }
-
Like initiating a GET request, you only need to set the request type "GET" to "HEAD. Modify the requested host and path as needed. Summary: php can also use get_headers to obtain header information. this function has been tested and is a GET request. for details, refer to: the php function get_headers is a HEAD request or GET request. In addition, some servers may block HEAD Requests. if they are blocked, they can only use GET requests. In this way, you can use the ready-made function getimagesize to obtain the image size. |