扫码关注官方订阅号
我想判断一个网站是否能够打开1.先用php ping这个网站看是否能够ping通2.http返回是否是200Ok3.是否文件内容有没有html标签现在主要不知道第三步怎么实现,是直接file_get_content,然后查找这个文件里有没有html?用strpos?
闭关修行中......
<?php $url = 'http://www.baidu.com/'; $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; }
http://www.baidu.com/返回的是200;http://www.baidu.com/xxx返回的是302,实际访问中,http://www.baidu.com/xxx跳转为https://www.baidu.com/search/error.html这样的。
http://www.baidu.com/
http://www.baidu.com/xxx
https://www.baidu.com/search/error.html
我觉得你可以先得到文件内容在做判断是否存在html标签!这没有什么好纠结的^_^
curl 模拟访问一次看看,在通过返回值判断。
curl
curl 判断http status 是否200
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
如果对于302这样的跳转也算作访问成功的话,你也可以加入到判断里面。
http://www.baidu.com/返回的是200;http://www.baidu.com/xxx返回的是302,实际访问中,http://www.baidu.com/xxx跳转为https://www.baidu.com/search/error.html这样的。我觉得你可以先得到文件内容在做判断是否存在html标签!这没有什么好纠结的^_^
curl模拟访问一次看看,在通过返回值判断。curl 判断http status 是否200