php determines whether the website is accessible

(*-*)浩
Release: 2023-02-25 10:52:01
Original
6094 people have browsed it

PHP-powered libcurl library created by Daniel Stenberg allows you to connect and communicate with a variety of servers using various types of protocols.

php determines whether the website is accessible

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 = &#39;&#39;;
$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 &#39;连接成功,状态码:&#39; . $curl_code;
} else {
echo &#39;连接失败,状态码:&#39; . $curl_code;
}
Copy after login

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 &#39;连接成功,状态码:&#39; . $curl_code;
} else {
echo &#39;连接失败,状态码:&#39; . $curl_code;
}
Copy after login

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!

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