PHP uses curl to determine if a remote file exists code

Source: Internet
Author: User
Tags ranges response code
    1. Judging 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 does not fail to send
    11. if ($result!== false) {
    12. Check if 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

Recently in a HTML5 music broadcast site, want to make my iphone and ipad cool, front-end with a jquery plug-in Jplayer, after the transformation of the effect is good. The backstage uses PHP, collects the MP3 of Baidu regularly. Considering I server space chrysanthemum tight, of course, can only collect MP3 address, the file is not downloaded to the local. Considering the Baidu MP3 path often change, it is the egg pain, so must be timed to determine the MP3 path is not right, so there is a PHP to determine whether the remote file exists this soft article. Start with the Get_headers () method, and later heard that there is an efficiency problem, so do not use this solution, but incidentally, let's 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

Results: Array ([0] = http/1.1 ok[1] [Date:thu], June 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, Jan 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, 2021 02:47:2 7 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

Results: Array ([0] = http/1.1 ok[date] = Thu, Geneva June (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, Jan 07:15:35 Gmt[etag] = "65E-49A41E65933C0" [accept-ranges] = Bytes[c Ontent-length] = 1630[cache-control] [Max-age=315360000[expires] = Sun, May 2021 02:49:28 Gmt[connection] = Close[content-type] = image/gif)

How, Get_headers function is still good, but since the efficiency has a problem, that had to not first consider, curl is good, below see curl Practice

    1. !--? php 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 ');//Do not add this will return 403, add to return the correct 200, the reason is unknown
    7. //Send request
    8. $result = curl_exec ($curl);
    9. $found = false;
    10. //If the request does not fail to send
    11. if ($result!== false)
    12. {
    13. //re-check HTTP response code is up to $
    14. $statusCode = Curl_getinfo ($curl, curlinfo_http_code);
    15. if ($statusCode = =)
    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? ' Existence ': ' not present ';
    25. $exists = check_remote_file_exists (' http://www.baidu.com/test.jpg ');
    26. Echo $exists? ' Existence ': ' not present ';
    27. ;
Copy Code
  • 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.