Php implementation code that uses curl to determine whether a remote file exists

Source: Internet
Author: User
Tags ranges

Copy codeThe Code is as follows: // determine the Remote File
Function check_remote_file_exists ($ url)
{
$ Curl = curl_init ($ url );
// Do not retrieve data
Curl_setopt ($ curl, CURLOPT_NOBODY, true );
// Send the request
$ Result = curl_exec ($ curl );
$ Found = false;
// Failed to send the request
If ($ result! = False ){
// Check whether the http response code is 200.
$ StatusCode = curl_getinfo ($ curl, CURLINFO_HTTP_CODE );
If ($ statusCode = 200 ){
$ Found = true;
}
}
Curl_close ($ curl );

Return $ found;
}

Recently, I was working on an html5 music playing website. I wanted to make my iphone and ipad feel better. I used a jquery plug-in jplayer on the front end. After the transformation, the results were quite good.
The background uses PHP to regularly collect Baidu MP3 files. Considering that my server space is very tight, of course, only MP3 addresses can be collected, and files are not downloaded to the local device. Considering that the Baidu MP3 path often changes, it is really a pain point. Therefore, you must regularly determine whether the MP3 path is correct, so you have PHP to determine whether the remote file exists in this soft article. I started to use the get_headers () method. Later I heard that there was a efficiency problem, so I didn't use this solution, but I also mentioned it. Let's take a look at the effect of the get_headers function:Copy codeThe Code is as follows: // default Effect
Print_r (get_headers ("http://www.baidu.com/img/baidu_sylogo1.gif "));
Result:
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 = Bytes: 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
)
// Add the result of parameter 1
Print_r (get_headers ("http://www.baidu.com/img/baidu_sylogo1.gif", 1 ));
Result:
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 = Bytes: 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] = & gt; 1630
[Cache-Control] => max-age = 315360000.
[Expires] => Sun, 30 May 2021 02:49:28 GMT
[Connection] => Close
[Content-Type] => image/gif
)

How about the get_headers function? But since there is a problem with efficiency, we have to give priority to it. curl is good. Let's take a look at curl's practices.Copy codeThe Code is as follows: function check_remote_file_exists ($ url)
{
$ Curl = curl_init ($ url );
// Do not retrieve data
Curl_setopt ($ curl, CURLOPT_NOBODY, true );
Curl_setopt ($ curl, CURLOPT_CUSTOMREQUEST, 'get'); // if this parameter is not added, 403 is returned. If the parameter is added, the correct 200 is returned. The cause is unknown.
// Send the request
$ Result = curl_exec ($ curl );
$ Found = false;
// Failed to send the request
If ($ result! = False)
{
// Check whether the http response code is 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? 'Exist': 'nonexistent ';
$ Exists = check_remote_file_exists ('HTTP: // www.baidu.com/test.jpg ');
Echo $ exists? 'Exist': 'nonexistent ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.