当前位置:网站首页>PHP检测url网址链接是否正常可访问
PHP检测url网址链接是否正常可访问
2022-07-28 10:53:00 【幕尘枫】
导读:
在一些场景中,需要通过访问URL网址链接实现http请求,如果网址链接不通(比如服务器关闭了),那么http请求就会出现阻塞,PHP是单线程的,一旦长时间没反应,就会给用户非常不好的体验。因此,好的解决方法是先检测网址是否能正常连接。
方法一:
/**
* 检测网址连接是否可用
**/
function check_url($url){
if (@fopen($url, 'r')==false) {
return false;
}
return true;
}方法二:
/**
* 检测网址连接是否可用
**/
function check_url($url){
$httpcode = 0;
$ch = curl_init();
$timeout = 1; // 设置超时的时间[单位:秒]
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);
# 获取状态码赋值
$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode == 200){
return true;
}else{
return false;
}
}边栏推荐
- 【MySQL从入门到精通】【高级篇】(十)MyISAM的索引方案&&索引的优缺点
- 本地化、低时延、绿色低碳:阿里云正式启用福州数据中心
- Nodejs: set up the express service, set up the session and realize the exit operation
- Ten thousand words detailed Google play online application standard package format AAB
- Nodejs: mongodb simple fuzzy + paging query instance
- I use the applet container to improve the efficiency of mobile R & D by 5 times!
- 融云 IM & RTC 能力上新盘点
- Offsetof macro and container_ Of macro analysis details
- WPF依赖属性(wpf 依赖属性)
- JS - modify the key name of the object in the array
猜你喜欢

CVPR2021 行人重识别/Person Re-identification 论文+开源代码汇总

Why should coding and modulation be carried out before transmission

leetcode:1300. 转变数组后最接近目标值的数组和【二分】

2022-2023 年十大应用程序发展趋势

Inventory: 6 books teach you the necessary skills for career promotion

小水滴2.0网站导航网模板

低代码(aPaas)为什么最近又火了?

接口测试的作用
![[JS advanced] JS functions, overloads, anonymous functions, scopes and scope chains_ 03](/img/50/754f7915ee4b2e04fdd21ae68130c6.png)
[JS advanced] JS functions, overloads, anonymous functions, scopes and scope chains_ 03

Remote shutdown of computer with mobile phone
随机推荐
Two point, three point, 01 point plan [bullet I]
Rongyun IM & RTC capabilities on new sites
【C语言】的%*d、%.*s等详解:「建议收藏」
用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
STM32驱动ST7701S芯片(vⅰV0手机换屏价)
vim命令下显示行号[通俗易懂]
What is WordPress
几个数据库的相关概念
Use the statement object to execute DDL statements to create tables
[MySQL from introduction to proficiency] [advanced chapter] (IX) precautions for InnoDB's b+ tree index
Summary of the second semester of junior year
BOM part attributes and understanding
用手机对电脑进行远程关机
五面阿里技术专家岗,已拿offer,这些面试题你能答出多少
Three ways for Cortex-M kernel to manage global interrupts
开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
Leetcode:981. time based key value storage [trap of iteration for: on]
I use the applet container to improve the efficiency of mobile R & D by 5 times!
好用到爆!IDEA 版 Postman 面世了,功能真心强大