Ignore:
Timestamp:
Oct 30, 2020 11:44:05 PM (4 years ago)
Author:
anonymous
Message:

Minor polish. Add getHttpHeader function

File:
1 edited

Legend:

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

    r729 r735  
    15191519* @since    02 May 2015 15:10:09
    15201520*/
    1521 function httpExists($url)
     1521function httpExists($url, $timeout=5)
    15221522{
    15231523    $ch = curl_init($url);
    1524     curl_setopt($ch, CURLOPT_TIMEOUT, 5);
     1524    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    15251525    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    15261526    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     
    15471547    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    15481548    return preg_match('/^[23]\d\d$/', $http_code);
     1549}
     1550
     1551/*
     1552* Get a HTTP response header.
     1553*
     1554* @access   public
     1555* @param    string  $url    URL to hit.
     1556* @param    string  $key    Name of the header to return.
     1557* @param    array   $valid_response_codes   Array of acceptable HTTP return codes.
     1558* @return   string  Value of the http header.
     1559* @author   Quinn Comendant <quinn@strangecode.com>
     1560* @since    28 Oct 2020 20:00:36
     1561*/
     1562function getHttpHeader($url, $key, Array $valid_response_codes=[200])
     1563{
     1564    $headers = @get_headers($url, 1);
     1565
     1566    if ($headers && preg_match(sprintf('/\b(%s)\b/', join('|', $valid_response_codes)), $headers[0])) {
     1567        $headers = array_change_key_case($headers, CASE_LOWER);
     1568        $key = strtolower($key);
     1569        if (isset($headers[$key])) {
     1570            return $headers[$key];
     1571        }
     1572    }
     1573
     1574    return false;
    15491575}
    15501576
Note: See TracChangeset for help on using the changeset viewer.