Home  >  Article  >  Backend Development  >  curl - php determines whether a website can be opened

curl - php determines whether a website can be opened

WBOY
WBOYOriginal
2016-09-19 09:16:281855browse

I want to judge whether a website can be opened
1. First use php to ping the website to see if it can be pinged
2. Whether the http return is 200Ok
3. Whether the file content has html tags
Now I don’t know what to do in the third step Implementation is to directly file_get_content, and then check whether there is html in this file?
Use strpos?

Reply content:

I want to judge whether a website can be opened
1. First use php to ping the website to see if it can be pinged
2. Whether the http return is 200Ok
3. Whether the file content has html tags
Now I don’t know what to do in the third step Implementation is to directly file_get_content, and then check whether there is html in this file?
Use strpos?

<code><?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;
}</code>

If a jump like 302 is also considered a successful access, you can also add it to the judgment.

<code>if ($curl_code == 200 || $curl_code == 302) {
    echo '连接成功,状态码:' . $curl_code;
} else {
    echo '连接失败,状态码:' . $curl_code;
}</code>

http://www.baidu.com/returns 200;
http://www.baidu.com/xxxreturns 302. During the actual visit, http://www.baidu. com/xxx jumps to https://www.baidu.com/search/error.html like this.

I think you can get the file content first and then judge whether there is an html tag! There’s nothing to worry about ^_^

curl Simulate a visit once and judge by the return value.

curl determines whether http status is 200

Statement:
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