You need to determine whether the remote URL is valid. The remote URL includes remote pictures, web pages, videos and other elements
This article mainly introduces the third PHP solution. This method is rarely used, but it feels pretty good.
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:
$url = "http://cn.php.net/images/php.gif";
$array = get_headers($url,1);
if( preg_match('/200/',$array[0])){
echo "
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:
You can see the correct return -----------------------Test ok
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/