判断一个远程url是否有效我们通常可以这样来处理:
1、get_headers()函数判断
用这个函数返回值如下所示:
测试代码:
$header = get_headers("http://www.xinghaixu.com/wp-content/uploads/2014/07//get_header.png",1); if(preg_grep("/200/", $header)){ echo "<pre>"; echo "匹配成功!"; echo "</pre>"; }else{ echo "匹配失败!"; }
2、 CURL判断处理
直接来代码演示说明吧
//判断远程文件 function check_remote_file_exists($url) { $curl = curl_init($url); // 不取回数据 curl_setopt($curl, CURLOPT_NOBODY, true); // 发送请求 $result = curl_exec($curl); $found = false; // 如果请求没有发送失败 if ($result !== false) { // 再检查http响应码是否为200 $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($statusCode == 200) { $found = true; } curl_close($curl); return $found; } }
转自:http://www.cnblogs.com/zzxbest/archive/2011/10/10/2205886.html
你若安好,便是晴天。
Thanks very interesting blog!
Hello everyone, it’s my first pay a visit at this web page, and piece of writing
is in fact fruitful designed for me, keep up posting these content.
Heya i am for the first time here. I came across this board and I find It really useful
& it helped me out a lot. I hope to give something back and help others like you helped me.
I’ll right away clutch your rss as I can’t find your email
subscription hyperlink or e-newsletter service. Do you have any?
Please let me understand in order that I may just subscribe.
Thanks.
I read this article completely regarding the comparison of most up-to-date
and preceding technologies, it’s amazing article.