|
You must use php code to determine whether the remote url is valid. the remote url includes remote images, webpages, videos, and other elements.
I. use php to solve the problem of using the file_get_contents function. However, if the url cannot be accessed, the program will be terminated. Use curl to return and determine whether the execution is correct. Use the get_headers function to check whether 200 exists based on the http return value.
II. solve with js: use the native js function activexobject and only support ie kernel browsers. Use the jq extension. This article mainly introduces the third method in php solution. this method is rarely used, but it feels good again. get_headers needs to be supported. Php_openssl supports viewing phpinfo to see if allow_url_fopen = on is enabled to modify php. ini. run the command to open 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:
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 http value. Test results: We can see that the correct response ------------------- test OK has advantages and disadvantages: the allow_url_fopen = on function must be enabled, which is similar to the use conditions of the file_get_contents function, but the return value is relatively small, you can use function_exists to determine whether the method can be used. |