Lei Zhang

时光已逝永不回,
往事只能回味。
... ...
春风又吹红了花蕊,
你已经也添了新岁。

▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ 96%



PHP工具函数

2017-11-29 » PHP / 代码块

服务端回复消息

if (!function_exists('echoJson')) {

    /**
     * 输出Json
     * @param type $arr
     * @return boolean
     */
    function echoJson($arr) {
        header('Content-Type: application/json; charset=utf-8');
        if (strpos(PHP_VERSION, '5.3') > -1) {
            // php 5.3-
            echo json_encode($arr);
        } else {
            // php 5.4+
            echo json_encode($arr, JSON_UNESCAPED_UNICODE);
        }
        return true;
    }

}
if (!function_exists('echoText')) {

    /**
     * 输出Text
     * @param type $arr
     * @return boolean
     */
    function echoText($arr) {
        header('Content-Type: text/plain; charset=utf-8');
        if (strpos(PHP_VERSION, '5.3') > -1) {
            // php 5.3-
            echo json_encode($arr);
        } else {
            // php 5.4+
            echo json_encode($arr, JSON_UNESCAPED_UNICODE);
        }
        return true;
    }

}

 

if (!function_exists('echoMsg')) {

    /**
     * 输出JSON消息
     * @param type $code
     * @param type $msg
     * @return type
     */
    function echoMsg($code, $msg = '') {
        return echoJson(array(
            'ret_code' => $code,
            'ret_msg' => $msg
        ));
    }

}

 

if (!function_exists('echoSuccess')) {

    /**
     * 输出成功JSON消息
     */
    function echoSuccess() {
        echoMsg(0, 'success');
    }

}

 

if (!function_exists('echoFail')) {

    /**
     * 输出失败JSON消息
     */
    function echoFail() {
        echoMsg(-1, 'failed');
    }

}

数组转 Json

if (!function_exists('toJson')) {

    /**
     * 数组转JSON
     * @param type $arr
     * @return type
     */
    function toJson($arr) {
        return print_r(json_encode($arr), true);
    }

}

POST/GET

if (!function_exists('http_post')) {

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    function http_post($url, $param, $post_file = false) {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

}


if (!function_exists('http_get')) {

    /**
     * GET 请求
     * @param string $url
     */
    function http_get($url) {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

}

时间戳转换

if (!function_exists('tranOldTime')) {

    /**
     * 将时间戳转换为时间轴的字符串,如:“xxx分钟前“
     * @param type $time UNIX时间戳
     * @return type
     */
    function tranOldTime($time) {
        $rtime = date("m-d H:i", $time);
        $htime = date("H:i", $time);
        $time = time() - $time;
        if ($time < 60) {
            $str = '刚刚';
        } elseif ($time < 60 * 60) {
            $min = floor($time / 60);
            $str = $min . '分钟前';
        } elseif ($time < 60 * 60 * 24) {
            $h = floor($time / (60 * 60));
            $str = $h . '小时前 ' . $htime;
        } elseif ($time < 60 * 60 * 24 * 3) {
            $d = floor($time / (60 * 60 * 24));
            if ($d == 1) {
                $str = '昨天 ' . $rtime;
            } else {
                $str = '前天 ' . $rtime;
            }
        } else {
            $str = $rtime;
        }
        return $str;
    }

}

 

if (!function_exists('tranNewTime')) {

    /**
     * 将时间戳转换为时间轴的字符串,如:“xxx分钟后“
     * @param type $time UNIX时间戳
     * @return type
     */
    function tranNewTime($time) {
        $rtime = date("m-d H:i", $time);
        $htime = date("H:i", $time);
        $time = $time - time();
        if ($time < 60) {
            $str = '即将';
        } elseif ($time < 60 * 60) {
            $min = floor($time / 60);
            $str = $min . '分钟后';
        } elseif ($time < 60 * 60 * 24) {
            $h = floor($time / (60 * 60));
            $str = $h . '小时后';
        } elseif ($time < 60 * 60 * 24 * 3) {
            $d = floor($time / (60 * 60 * 24));
            if ($d == 1) {
                $str = '明天';
            } else {
                $str = '后天';
            }
        } else {
            $str = $rtime;
        }
        return $str;
    }

}
展开选填信息