Home > Backend Development > PHP Tutorial > The best way to determine whether a URL is valid in PHP_PHP Tutorial

The best way to determine whether a URL is valid in PHP_PHP Tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 14:58:14
Original
850 people have browsed it

1. Determine whether a URL can be accessed normally to avoid the problem of a fatal error and program termination when using file_get_contents because the URL cannot be accessed.

$url = 'http://www.bkjia.com';
$ch = curl_init();
$timeout = 10;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$contents = curl_exec ($ch);

if(false == $contents)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
….
}

Also, you can use the following code:

curl_getinfo($ch, CURLINFO_HTTP_CODE);

Get the code returned by the HTTP header file. If it is 200, the url can be accessed normally, but this function must be used after curl_exec(), which seems a bit redundant. .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363883.htmlTechArticle1. Determine whether a url can be accessed normally to avoid a fatal error when using file_get_contents because the url cannot be accessed. , the problem of terminating the program. $url = http://www.bkjia.com; $ch =...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template