PHP can determine whether a remote url is valid. Determine whether the remote URL is valid. the remote url includes remote images, webpages, videos, and other elements. Solution: Use PHP to use the file_get_contents function. However, the advantages and disadvantages are as follows:
Determine whether the remote URL is valid. the remote url includes remote images, webpages, videos, and other elements.
Solution: Use PHP
- 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:
$ Url = "http://cn.php.net/images/php.gif ";
$ Array = get_headers ($ url, 1 );
If (preg_match ('/ 200/', $ array [0]) {
Echo"
";
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
Source: http://phphuaibei.cnblogs.com/
The handler needs to determine whether the remote URL is valid. the remote url includes remote images, webpages, videos, and other elements. Solution: Use PHP to use the file_get_contents function, but the advantages and disadvantages are as follows...