运用php推断url是不是能够翻开的解决办法:
1、运用file_get_contents函数,不过瑕玷是,假如url无法访问,会涌现停止顺序题目
2、运用curl返回,然后推断是不是准确实行
3、运用get_headers函数,依据HTTP返回值检察是不是有200
get_headers须要支撑
1、php_openssl支撑 检察phpinfo看看是不是开启
2、allow_url_fopen=on 修正php.ini,运转运用长途翻开
函数引见:
array get_headers ( string $url [, int $format ] )
get_headers()返回一个数组,包括有服务器响应一个 HTTP 要求所发送的标头。假如失利则返回 FALSE 并发出一条 E_WARNING 级别的错误信息。假如将可选的 format 参数设为 1,则 get_headers() 会剖析响应的信息并设定数组的键名。 比方:
实例:
<?php $url = "http://cn.php.net/images/php.gif"; $array = get_headers($url,1); if(preg_match('/200/',$array[0])){ echo "<pre/>"; print_r($array); }else{ echo "无效url资本!"; }
效果:
优瑕玷:
须要allow_url_fopen=on 开启,有点和file_get_contents函数运用前提相似,然则返回值比较少,能够运用function_exists推断该要领是不是能够运用。
引荐教程:PHP视频教程
以上就是php推断url是不是能够翻开的细致内容,更多请关注ki4网别的相干文章!