Example #1
0
function getGooglePageSpeedReport($setting = null)
{
    if (file_exists(NITRO_FOLDER . 'data/googlepagespeed.tpl')) {
        $data = file_get_contents(NITRO_FOLDER . 'data/googlepagespeed.tpl');
        $data = base64_decode($data);
        //$data = json_decode($data,true);
        $returnData = $data;
    } else {
        // Fetch the report and save it
        $catalogURL = defined('HTTPS_CATALOG') && HTTPS_CATALOG != '' ? HTTPS_CATALOG : defined('HTTP_CATALOG') && HTTP_CATALOG != '' ? HTTP_CATALOG : defined('HTTPS_SERVER') && HTTPS_SERVER != '' ? HTTPS_SERVER : HTTP_SERVER;
        $catalogURL = $catalogURL . '#red' . rand(1000, 99999);
        $persistence = getNitroPersistence();
        $key = !empty($persistence['Nitro']['GooglePageSpeedApiKey']) ? $persistence['Nitro']['GooglePageSpeedApiKey'] : 'AIzaSyCxptR6CbHYrHkFfsO_XN3nkf6FjoQp2Mg';
        $url = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=" . $catalogURL . "&key=" . $key;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);
        curl_close($ch);
        if (!empty($output)) {
            $returnData = $output;
            setGooglePageSpeedReport($returnData);
        } else {
            $returnData = false;
        }
    }
    if (!empty($setting)) {
        if (!empty($returnData['NitroCache'][$setting])) {
            $returnData = $returnData['NitroCache'][$setting];
        } else {
            $returnData = false;
        }
    }
    return $returnData;
}
Example #2
0
function getGooglePageSpeedReport($setting = null, $strategies = array('mobile', 'desktop'))
{
    foreach ($strategies as $strategy) {
        if (!file_exists(NITRO_FOLDER . 'data/googlepagespeed-' . $strategy . '.tpl')) {
            // Fetch the report and save it
            $catalogURL = defined('HTTP_SERVER') && HTTP_SERVER != '' ? dirname(HTTP_SERVER) . '/' : false;
            if (!$catalogURL) {
                return false;
            }
            $persistence = getNitroPersistence();
            $persistence = $persistence['Nitro'];
            $key = !empty($persistence['GooglePageSpeedApiKey']) ? $persistence['GooglePageSpeedApiKey'] : 'AIzaSyCxptR6CbHYrHkFfsO_XN3nkf6FjoQp2Mg';
            $url = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=" . $catalogURL . "&key=" . $key . "&strategy=" . $strategy;
            $output = fetchRemoteContent($url);
            if (!empty($output)) {
                setGooglePageSpeedReport($output, $strategy);
            }
        }
    }
    $returnData = false;
    foreach ($strategies as $strategy) {
        if (file_exists(NITRO_FOLDER . 'data/googlepagespeed-' . $strategy . '.tpl')) {
            if (!is_array($returnData)) {
                $returnData = array();
            }
            $returnData[$strategy] = base64_decode(file_get_contents(NITRO_FOLDER . 'data' . DS . 'googlepagespeed-' . $strategy . '.tpl'));
        }
    }
    return $returnData;
}