Home > Backend Development > PHP Tutorial > PHP code that uses curl to determine whether a remote file exists

PHP code that uses curl to determine whether a remote file exists

WBOY
Release: 2016-07-25 09:03:36
Original
1003 people have browsed it
  1. //Determine remote files

  2. function check_remote_file_exists($url)
  3. {
  4. $curl = curl_init($url);
  5. //Do not retrieve data
  6. curl_setopt($ curl, CURLOPT_NOBODY, true);
  7. //Send request
  8. $result = curl_exec($curl);
  9. $found = false;
  10. //If the request is not sent, it fails
  11. if ($result !== false) {
  12. // Check again whether the http response code is 200
  13. $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  14. if ($statusCode == 200) {
  15. $found = true;
  16. }
  17. }
  18. curl_close($curl);
  19. return $found;

  20. }
  21. ?>

Copy code

I am currently working on a website for HTML5 music playback, and I want to make my iPhone and iPad better. Cool, the front end uses jplayer, a plug-in of jquery, and the effect is pretty good after modification. The background uses PHP to collect Baidu's 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:

  1. //Default effect
  2. print_r(get_headers("http://www.baidu.com/img/baidu_sylogo1.gif"));
Copy code

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 )

  1. //The effect of adding parameter 1
  2. print_r(get_headers("http://www.baidu.com/img/baidu_sylogo1.gif", 1));
Copy code

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=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 it? The get_headers function is pretty good, but since there is a problem with efficiency, we have to give it no priority. Curl is pretty good. Let’s take a look at how curl works.

  1. function check_remote_file_exists($url)
  2. {
  3. $curl = curl_init($url);
  4. //Do not retrieve data
  5. curl_setopt($curl, CURLOPT_NOBODY, true);
  6. curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, 'GET'); //If you don't add this, 403 will be returned. If you add it, the correct 200 will be returned. The reason is unknown
  7. //Send request
  8. $result = curl_exec($curl);
  9. $found = false;
  10. // If the request fails to be sent
  11. if ($result !== false)
  12. {
  13. // Check again whether the http response code is 200
  14. $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  15. if ($statusCode == 200)
  16. {
  17. $found = true;
  18. }
  19. }
  20. curl_close($curl);
  21. return $found;
  22. }
  23. $exists = check_remote_file_exists('http://www.baidu.com/img/baidu_sylogo1.gif ');
  24. echo $exists ? 'Exists' : 'Does not exist';
  25. $exists = check_remote_file_exists('http://www.baidu.com/test.jpg');
  26. echo $exists ? 'Exists' : ' Does not exist';
  27. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template