Home > Article > Backend Development > Summary of several methods for universalthemepatcher PHP to determine whether the remote URL is valid
Solution:
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
Use js solution:
Use the native js function ActiveXObject, which only supports IE kernel browsers
Use jq extension
This article mainly introduces the third PHP solution. This method is rarely used, but it feels good again,
get_headers needs support
php_openssl supports checking phpinfo to see if it is enabled
allow_url_fopen=on Modify php.ini, run and use remote opening
Function introduction:
array get_headers ( string $url [, int $format ] )
get_headers() returns an array , contains 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 key name of the array. For example:
Copy code The code is as follows:
$url = "/upload/201110/20111008192257383.gif";
$array = get _headers($ url,1);
if(preg_match('/200/',$array[0])){
echo "
Advantages and disadvantages:
Requires allow_url_fopen=on, which is somewhat similar to the file_get_contents function usage conditions, but the return value is relatively small, OK Use
function_exists to determine whether the method can be used