Home > Backend Development > PHP Tutorial > PHP method to detect whether the URL can be opened normally

PHP method to detect whether the URL can be opened normally

墨辰丷
Release: 2023-03-29 07:32:01
Original
4730 people have browsed it

This article mainly introduces the method of PHP to detect whether the URL can be opened normally, involving the simple use skills of curl in PHP. Friends in need can refer to it

This is a method to detect whether the URL can be opened normally PHP code, 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 is abnormal. We can use this code in many places, such as caching the ICO icon of friendly links. It can be used when caching. When caching, first check whether the website is normal. If it is normal, the ICO icon will be cached. Otherwise, a default icon file will be called.

The code is as follows:

<?php
/*
 * Created on 2016-9-4
 *
 */
 function httpcode($url){
  $ch = curl_init();
  $timeout = 3;
  curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_exec($ch);
  return $httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
  curl_close($ch);
}
echo "判断PHP中文网的链接:".httpcode(&#39;http://www.php.cn&#39;);
?>
<br/>
如果显示为200则正常,如果显示其它值表示不正常;$timeout后面的3是设置超时秒数。
Copy after login

The running effect is as follows:

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Solution to the problem of unable to log in to the PHP magento backend

Detailed explanation of 2 methods of merging arrays in PHP

php in_array() Detailed explanation of checking whether a certain value exists in the array

The above is the detailed content of PHP method to detect whether the URL can be opened normally. For more information, please follow other related articles on the PHP Chinese website!

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