Several Methods for PHP to determine whether a remote URL is valid

Source: Internet
Author: User

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
    1. Use the file_get_contents function. However, if the URL cannot be accessed, termination occurs.ProgramProblem
    2. Use curl to return the result and determine whether the execution is correct.
    3. Use the get_headers function to check whether 200 exists based on the HTTP return value.
Solve with JS:
    1. The native JS function activexobject is used and only supports ie kernel browsers.
    2. 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
    1. Php_openssl supports viewing phpinfo to see if it is enabled
    2. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.