phpcssfunctionexternal

Dynamically fetching css from CDN with fallback to local copy


I am trying to fetch my CSS from external CDN service let's say http://cdn.example.com/.

This code is suppose to check if file exists on external CDN and if it does, then get that file else get it from a local folder.

Currently this is fetching only the local copy even though the file exists on external CDN.

Please help me correct this code.

function getCSSfile($filename)
{
    $externalcss = EXTERNAL_CDN.'css/'.$filename.'.css';
    $localcss = LOCAL_CDN.'css/'.$filename.'.css';

    $ctx = stream_context_create(array(
        'http' => array(
            'method' => 'HEAD'
        )
    ));

    if (file_get_contents($externalcss, false, $ctx) !== false) {
        echo '<link href="' . $externalcss . '" rel="stylesheet" type="text/css" media="all"/>';
    } else if (file_exists($localcss)) {
        echo '<link href="' . $localcss . '" rel="stylesheet" type="text/css" media="all"/>';
    } else {
        echo 'Error loading CSS File'; 
    }
}

UPDATE:

Updated the code as per @DaveRandom's suggestion. Currently code runs fine it fetches the copy from external CDN but if the external CDN is unavailable it does fetches the local copy BUT throws the following error too:

Warning: file_get_contents(http://cdn.localtest.com/css/core.css): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in D:\xampp\htdocs\localtest2\core\core.php on line 165

and in line 165 this is the code

if (file_get_contents($externalcss, false, $ctx) !== false) {

I am testing it locally and created some domains on my XAMPP Server, hence cannot share a live link.


Solution

  • Okay since the error solution was not explained by anyone... I decided to answer my own question. This is the solution I found:

    function getCSSfile($filename)
    {
        $externalcss = EXTERNAL_CDN.'css/'.$filename.'.css';
        $localcss = LOCAL_CDN.'css/'.$filename.'.css';
    
        $ctx = stream_context_create(array(
            'http' => array(
                'method' => 'HEAD'
            )
        ));
    
        if (@file_get_contents($externalcss, false, $ctx) !== false) {
            echo '<link href="' . $externalcss . '" rel="stylesheet" type="text/css" media="all"/>';
        } else if (file_exists($localcss)) {
            echo '<link href="' . $localcss . '" rel="stylesheet" type="text/css" media="all"/>';
        } else {
            echo 'Error loading CSS File'; 
        }
    }
    

    As suggested by Mark B

    As stated in the docs, file_get_contents() will throw a warning if the stated resource cannot be found.

    This is one of the few cases in which using the @ error suppression operator may be justified, e.g.

    if (@file_get_contents($externalcss, false, $ctx) !== false)
    

    to prevent the warning from mucking up the output.