Home  >  Article  >  Backend Development  >  Summary of several methods for universalthemepatcher PHP to determine whether the remote URL is valid

Summary of several methods for universalthemepatcher PHP to determine whether the remote URL is valid

WBOY
WBOYOriginal
2016-07-29 08:46:57821browse

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:

 PHP判断远程url是否有效的几种方法小结

Simple 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 "

"; 
print_r($array);
}else{
echo "Invalid url resource! ";
}


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:

 PHP判断远程url是否有效的几种方法小结

You can see the correct return - -----------------------Test ok

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

The above has introduced a summary of several methods for universalthemepatcher PHP to determine whether the remote URL is valid, including the content of universalthemepatcher. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn