Several ways to determine whether the remote URL is valid in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 14:58:12
Original
865 people have browsed it

Premise

You need to determine whether the remote URL is valid. The remote URL includes remote pictures, web pages, videos and other elements

Solution:

Use PHP to solve

  1. 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
  2. Use curl to return, and then determine whether it is executed correctly
  3. Use the get_headers function to check whether there is 200 based on the HTTP return value

Use js to solve:

  1. Use native js function ActiveXObject, only support browsers with IE kernel
  2. Use jq extension

This article mainly introduces the third PHP solution. This method is rarely used, but it feels pretty good.

get_headers needs support

  1. php_openssl support Check phpinfo to see if it is enabled
  2. allow_url_fopen=on Modify php.ini and run it using remote opening

Function introduction:

array get_headers ( string $url [, int $format ] )

get_headers() returns an array containing 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:

Several ways to determine whether the remote URL is valid in PHP_PHP Tutorial

Simple example:

$url = "http://cn.php.net/images/php.gif";
$array = get_headers($url,1);
if( preg_match('/200/',$array[0])){
echo "

Copy after login
";
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:

Several ways to determine whether the remote URL is valid in PHP_PHP Tutorial

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

Advantages and Disadvantages:

Requires allow_url_fopen=on, which is similar to the file_get_contents function, but the return value is less, you can use

function_exists determines whether the method can be used

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363886.htmlTechArticleThe premise is to determine whether the remote URL is valid. The remote URL includes remote pictures, web pages, videos and other elements. Solution: Use PHP uses the file_get_contents function, but the advantages and disadvantages are as follows...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template