php判断一个网站是否能够打开
1个回答
展开全部
<?php
$url = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch); // $resp = curl_exec($ch);
$curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($curl_code == 200) {
echo '连接成功,状态码:' . $curl_code;
} else {
echo '连接失败,状态码:' . $curl_code;
}
如果对于302这样的跳转也算作访问成功的话,你也可以加入到判断里面。
if ($curl_code == 200 || $curl_code == 302) {
echo '连接成功,状态码:' . $curl_code;
} else {
echo '连接失败,状态码:' . $curl_code;
}
$url = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch); // $resp = curl_exec($ch);
$curl_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($curl_code == 200) {
echo '连接成功,状态码:' . $curl_code;
} else {
echo '连接失败,状态码:' . $curl_code;
}
如果对于302这样的跳转也算作访问成功的话,你也可以加入到判断里面。
if ($curl_code == 200 || $curl_code == 302) {
echo '连接成功,状态码:' . $curl_code;
} else {
echo '连接失败,状态码:' . $curl_code;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询