Changeset 606 for trunk/lib


Ignore:
Timestamp:
Jun 22, 2017 12:02:35 PM (7 years ago)
Author:
anonymous
Message:

Update httpExists to work better with servers that balk at curl or HEAD requests by using a real GET request and pretending to be Chrome

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Utilities.inc.php

    r605 r606  
    14461446/*
    14471447* Returns true if the given URL resolves to a resource with a HTTP 2xx or 3xx header response.
     1448* The download will abort if it retrieves >= 10KB of data to avoid downloading large files.
     1449* We couldn't use CURLOPT_NOBODY (a HEAD request) because some services don't behave without a GET request (ahem, BBC).
     1450* This function may not be very portable, if the server doesn't support CURLOPT_PROGRESSFUNCTION.
    14481451*
    14491452* @access   public
     
    14511454* @return   bool            True if the resource exists, false otherwise.
    14521455* @author   Quinn Comendant <quinn@strangecode.com>
    1453 * @version  1.0
     1456* @version  2.0
    14541457* @since    02 May 2015 15:10:09
    14551458*/
     
    14581461    $ch = curl_init($url);
    14591462    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    1460     curl_setopt($ch, CURLOPT_NOBODY, true);
    14611463    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    14621464    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     1465    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
     1466    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Don't pass through data to the browser.
     1467    curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); // Frequent progress function calls.
     1468    curl_setopt($ch, CURLOPT_NOPROGRESS, false); // Required to use CURLOPT_PROGRESSFUNCTION.
     1469    curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function($ch, $dltot, $dlcur, $ultot, $ulcur){
     1470        // Return a non-zero value to abort the transfer. In which case, the transfer will set a CURLE_ABORTED_BY_CALLBACK error
     1471        // 10KB should be enough to catch a few 302 redirect headers and get to the actual content.
     1472        return ($dlcur > 10*1024) ? 1 : 0;
     1473    });
     1474
    14631475    curl_exec($ch);
    14641476    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Note: See TracChangeset for help on using the changeset viewer.