Introduction: This is a detailed page for PHP to determine whether a remote URL is valid. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 356995 'rolling = 'no'>
Prerequisites
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, termination occurs.ProgramProblem
- 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:
ArrayGet_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, returnFalseAnd issueE_warningLevel error message.
If the optionalFormatIf the parameter is set to 1Get_headers ()The corresponding information is parsed and the key name of the array is set. For example:
Simple Example:
<? PHP
$ Url = "http://cn.php.net/images/php.gif ";
$ Array = get_headers ($ URL, 1 );
If (preg_match ('/ 200/', $ array [0]) {
Echo "<PRE/> ";
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
Other methods to remotely determine whether a URL is valid:
Use the curl method: http://www.windsfly.cn/blog/article.asp? Id = 1647 http://blog.pcsql.com/archives/87.html
Using JS implementation: http://www.cnblogs.com/GuominQiu/archive/2011/04/07/2007600.html
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/356995.html pageno: 2.