It is required to use PHP code to determine whether the remote URL is valid. The remote URL includes remote pictures, web pages, videos and other elements. 1. Use php to solve Use the file_get_contents function, but the advantages and disadvantages are that if the url cannot be accessed, there will be a problem of terminating the program. Use curl to return, and then determine whether it is executed correctly. Use the get_headers function to check whether there is 200 based on the http return value. 2. Use js to solve: Use the native js function activexobject, which only supports browsers with IE core. Use jq extension. This article mainly introduces the third PHP solution. This method is rarely used, but it feels good. Get_headers needs support. php_openssl support Check phpinfo to see if it is enabled allow_url_fopen=on Modify php.ini and run it using remote opening. Function introduction: array get_headers ( string $url [, int $format ] ) get_headers() returns an array containing the headers sent by the server in response to an http request. If it fails, it returns false and issues an e_warning level error message. If the optional format parameter is set to 1, get_headers() will parse the corresponding information and set the array keys. For example: Example:
Explanation: Determine whether the remote image ur is valid. According to whether there is 200 information in the return value http, determine whether it is a valid url resource. Test Results: You can see the correct return ----------------------- test ok Advantages and disadvantages: Allow_url_fopen=on needs to be turned on, which is somewhat similar to the file_get_contents function usage conditions, but the return value is relatively small. You can use function_exists to determine whether the method can be used. |