如何仅在

时间:2015-09-01 10:35:04

标签: php

enter code here

    $loadMessageDisplayed = FALSE;
  function file_get_contents_curl($url){
    global $loadMessageDisplayed;
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);

//PAGE LOAD TIME
    if(!curl_errno($ch) && !$loadMessageDisplayed){
            $cinfo =  curl_getinfo($ch); 
            echo 'Page loaded in '.$cinfo['total_time'].' seconds';
            $loadMessageDisplayed = TRUE;
    }

    curl_close($ch);
    return $data;
}

我只想显示一次页面加载时间。再次,如果我调用相同的函数,它不应该显示加载时间。

1 个答案:

答案 0 :(得分:0)

function file_get_contents_curl($url)
{

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);

    //PAGE LOAD TIME
    if (!defined('constant')) {
        define('constant', 'value');

        if (!curl_errno($ch)) {
            $cinfo = curl_getinfo($ch);
            echo 'Page loaded in ' . $cinfo['total_time'] . ' seconds';
        }
    }

    curl_close($ch);
    return $data;
}