Home > Article > Backend Development > Implementation code for using curl to determine whether a remote file exists under PHP
The 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 request
$result = curl_exec($curl);
$found = false;
// If the request is not sent and fails
if ($result !== false) {
// Check again whether the http response code is 200
$ statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode == 200) {
$found = true;
}
}
curl_close($curl);
return $found;
}
Working on it recently An HTML5 music playing website, I want to make my iPhone and iPad feel better. The front end uses jplayer, a plug-in of jquery. After modification, the effect is pretty good.
The background uses PHP to collect Baidu MP3 regularly. Considering that my server space is tight, of course I can only collect MP3 addresses, and the files are not downloaded locally. Considering that the Baidu MP3 path often changes, it is really annoying, so it is necessary to regularly judge whether the MP3 path is still correct, so there is a soft article about PHP judging whether the remote file exists. I started using the get_headers() method, but later I heard that there was an efficiency problem, so I didn’t use this solution. But by the way, let’s take a look at the effect of the get_headers function:
Copy the code The 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=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
)
//The effect of adding parameter 1
print_r(get_headers("http://www.baidu.com/img/baidu_sylogo1.gif", 1 ); 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
)
How about the get_headers function is still good, but since there is a problem with efficiency, we have to not give it priority. Curl is good. Let’s take a look at how curl works. Copy the code. The 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 you don't add this, 403 will be returned. If you add it, it will return the correct 200. The reason is unknown
//Send request
$result = curl_exec($curl);
$found = false;
//If the request fails to be sent
if ($result !== false)
{
// Check again 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 ? 'Exists' : 'Does not exist';
$exists = check_remote_file_exists('http://www.baidu.com/test.jpg');
echo $exists ? 'Exists' : 'Does not exist';