Home > Article > Backend Development > php determines whether the website is accessible
PHP-powered libcurl library created by Daniel Stenberg allows you to connect and communicate with a variety of servers using various types of protocols.
libcurl currently supports http, https, ftp, gopher, telnet, dict, file and ldap protocols. libcurl also supports HTTPS authentication, HTTP POST, HTTP PUT, FTP upload (this can also be completed through PHP's FTP extension), HTTP form-based upload, proxy, cookies and username and password authentication. (Recommended learning: PHP Programming from entry to proficiency)
This is a PHP code that detects whether a URL can be opened normally. Use the following code to detect whether a URL can be accessed normally. If it is normal The value of http status code 200 will be returned. If it is other, it will be abnormal; we can use this code in many places.
<?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; }
If a jump like 302 is also considered a successful access, you can also add it to the judgment.
if ($curl_code == 200 || $curl_code == 302) { echo '连接成功,状态码:' . $curl_code; } else { echo '连接失败,状态码:' . $curl_code; }
The above is the detailed content of php determines whether the website is accessible. For more information, please follow other related articles on the PHP Chinese website!