Solution:
PHP Solution
Use the file_get_contents function. However, if the url cannot be accessed, the program will be terminated.
Use curl to return the result and determine whether the execution is correct.
Use the get_headers function to check whether 200 exists based on the HTTP return value.
Solve with js:
The native js function ActiveXObject is used and only supports ie kernel browsers.
Use jq Extension
This article mainly introduces the third method of PHP solution. This method is rarely used, but it feels good again,
Get_headers must be supported
Php_openssl supports viewing phpinfo to see if it is enabled
Allow_url_fopen = on modify php. ini and run it remotely.
Function introduction:
Array get_headers (string $ url [, int $ format])
Get_headers () returns an array containing the header sent by the server to respond to an HTTP request. If it fails, FALSE is returned and an E_WARNING-level error message is sent.
If you set the optional format parameter to 1, get_headers () will parse the corresponding information and set the key name of the array. For example:
Simple Example:
Copy codeThe Code is as follows:
<? Php
$ Url = "/upload/201110/20111008192257383 .gif ";
$ Array = get_headers ($ url, 1 );
If (preg_match ('/ 200/', $ array [0]) {
Echo "<pre/> ";
Print_r ($ array );
} Else {
Echo "invalid url resource! ";
}
Explanation: Determine whether the remote image ur is valid. Determine whether the url resource is valid based on whether 200 information exists in the returned value HTTP.
Test results:
The returned result is --------------------- test OK.
Advantages and disadvantages:
Enable allow_url_fopen = on. It is a bit similar to the conditions used by the file_get_contents function, but the return value is relatively small. You can use
Function_exists determines whether this method can be used