Several methods for PHP to determine whether a remote url is valid _ PHP Tutorial

Source: Internet
Author: User
PHP can determine whether a remote url is valid. Determine whether the remote URL is valid. the remote url includes remote images, webpages, videos, and other elements. Solution: Use PHP to use the file_get_contents function. However, the advantages and disadvantages are as follows:

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, the program will be terminated.
  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:

Array get_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, FALSE is returned and an E_WARNING-level error message is sent.

If you set the optional format parameter to 1, get_headers () will parse the corresponding information and set the key name of the array. For example:

Simple example:

$ Url = "http://cn.php.net/images/php.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. 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

Source: http://phphuaibei.cnblogs.com/

The handler needs to determine whether the remote URL is valid. the remote url includes remote images, webpages, videos, and other elements. Solution: Use PHP to use the file_get_contents function, but the advantages and disadvantages are as follows...

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.