Php method for determining whether a remote url is valid

Source: Internet
Author: User
Php method for determining whether a remote url is valid

You must use php code to determine whether the remote url is valid. the remote url includes remote images, webpages, videos, and other elements.

I. use php to solve the problem of using the file_get_contents function. However, if the url cannot be accessed, the program will be terminated. Use curl to return and determine whether the execution is correct. Use the get_headers function to check whether 200 exists based on the http return value.

II. solve with js: use the native js function activexobject and only support ie kernel browsers. Use the jq extension. This article mainly introduces the third method in php solution. this method is rarely used, but it feels good again. get_headers needs to be supported. Php_openssl supports viewing phpinfo to see if allow_url_fopen = on is enabled to modify php. ini. run the command to open 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:

Example:

  1. $ Url = "http://cn.php.net/images/php.gif ";
  2. $ Array = get_headers ($ url, 1 );
  3. If (preg_match ('/ 200/', $ array [0]) {
  4. Echo"
    ";
  5. Print_r ($ array );
  6. } Else {
  7. Echo "invalid url resource! ";
  8. }

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 http value. Test results: We can see that the correct response ------------------- test OK has advantages and disadvantages: the allow_url_fopen = on function must be enabled, which is similar to the use conditions of the file_get_contents function, but the return value is relatively small, you can use function_exists to determine whether the method can be used.

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.