Home > Backend Development > PHP Tutorial > Code for determining whether a URL is valid under PHP when China Telecom is disconnected

Code for determining whether a URL is valid under PHP when China Telecom is disconnected

WBOY
Release: 2016-07-29 08:46:55
Original
1484 people have browsed it

Copy the code The code is as follows:


$url = 'http://www.baidu.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
{
….
}


In addition, you can use

to copy the code code As follows:


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.

The above introduces the code to determine whether the URL is valid under PHP when the telecommunications network is disconnected, including the content of the telecommunications network disconnection. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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