手机
当前位置:查字典教程网 >编程开发 >php教程 >php下利用curl判断远程文件是否存在的实现代码
php下利用curl判断远程文件是否存在的实现代码
摘要:复制代码代码如下://判断远程文件functioncheck_remote_file_exists($url){$curl=curl_ini...

复制代码 代码如下:

//判断远程文件

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;

}

最近在弄一个html5音乐播放的网站,想让我的iphone和ipad爽一爽,前端采用jquery的一个插件jplayer,经过改造之后效果还不错。

后台采用PHP,定时采集百度的MP3。 考虑到本人服务器空间菊紧,当然只能采集MP3地址,文件并没有下载到本地。考虑到百度MP3路径经常变,实在是蛋疼,于是必须定时判断一下MP3路径还对不对,于是就有了PHP判断远程文件是否存在这篇软文。开始用get_headers() 方法,后来听说存在效率问题,于是不使用此解决方案,但是也顺带一提吧,下面看看get_headers函数的效果:

复制代码 代码如下:

//默认效果

print_r(get_headers("http://www.baidu.com/img/baidu_sylogo1.gif"));

结果:

Array

(

[0] => HTTP/1.1 200 OK

[1] => Date: Thu, 02 Jun 2011 02:47:27 GMT

[2] => Server: Apache

[3] => P3P: CP=" OTI DSP COR IVA OUR IND COM "

[4] => Set-Cookie: BAIDUID=7F6A5A2ED03878A7791C89C526966F3A:FG=1; expires=Fri, 01-Jun-12 02:47:27 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1

[5] => Last-Modified: Thu, 20 Jan 2011 07:15:35 GMT

[6] => ETag: "65e-49a41e65933c0"

[7] => Accept-Ranges: bytes

[8] => Content-Length: 1630

[9] => Cache-Control: max-age=315360000

[10] => Expires: Sun, 30 May 2021 02:47:27 GMT

[11] => Connection: Close

[12] => Content-Type: image/gif

)

//加参数1的效果

print_r(get_headers("http://www.baidu.com/img/baidu_sylogo1.gif", 1));

结果:

Array

(

[0] => HTTP/1.1 200 OK

[Date] => Thu, 02 Jun 2011 02:49:28 GMT

[Server] => Apache

[P3P] => CP=" OTI DSP COR IVA OUR IND COM "

[Set-Cookie] => BAIDUID=4D875812FC482C0ADE4F5C17068849EE:FG=1; expires=Fri, 01-Jun-12 02:49:28 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1

[Last-Modified] => Thu, 20 Jan 2011 07:15:35 GMT

[ETag] => "65e-49a41e65933c0"

[Accept-Ranges] => bytes

[Content-Length] => 1630

[Cache-Control] => max-age=315360000

[Expires] => Sun, 30 May 2021 02:49:28 GMT

[Connection] => Close

[Content-Type] => image/gif

)

怎么样,get_headers函数还是不错的吧,不过既然效率有问题,那只好不优先考虑了,curl就不错,下面看看curl的做法

复制代码 代码如下:

function check_remote_file_exists($url)

{

$curl = curl_init($url);

// 不取回数据

curl_setopt($curl, CURLOPT_NOBODY, true);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); //不加这个会返回403,加了才返回正确的200,原因不明

// 发送请求

$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;

}

$exists = check_remote_file_exists('http://www.baidu.com/img/baidu_sylogo1.gif');

echo $exists ? '存在' : '不存在';

$exists = check_remote_file_exists('http://www.baidu.com/test.jpg');

echo $exists ? '存在' : '不存在';

【php下利用curl判断远程文件是否存在的实现代码】相关文章:

php删除文本文件中重复行的方法

如何利用php+mysql保存和输出文件

php中命名空间namespace用法介绍

PHP获取远程图片并保存到本地的方法

php使用cookie实现记住用户名和密码实现代码

php将图片文件转换成二进制输出的方法

php使用curl打开https网站的方法

PHP实现远程下载文件到本地

基于文本的访客签到簿

文件上传的实现

精品推荐
分类导航