How to check whether the URL can be opened normally in php

怪我咯
Release: 2023-03-12 22:46:01
Original
2364 people have browsed it

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

The example of this article tells the simple detection of whether the URL can be opened by PHP. A way to open it normally. Share it with everyone for your reference, the details are as follows:

This is a PHP code to detect 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, such as when caching the ICO icon of a friendly link. 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 above is the detailed content of How to check whether the URL can be opened normally in php. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!