Home > Article > Backend Development > How to check whether the URL can be opened normally in php
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('http://www.php.cn'); ?> <br/> 如果显示为200则正常,如果显示其它值表示不正常;$timeout后面的3是设置超时秒数。
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!