PHP input()接收数据(get,post等)

/**
 * 获取输入数据
 * @param string $key 获取的变量名
 * @param mixed $default 默认值
 * @param string $function 处理函数
 * @return mixed
 */
function input($key = '', $default = '', $function = '')
{
    if ($pos = strpos($key, '.')) {
        list($method, $key) = explode('.', $key, 2);
        if ( ! in_array($method, array('get', 'post', 'request'))) {
            $key    = $method.'.'.$key;
            $method = 'param';
        }
    } else {
        $method = 'param';
    }

    $method = strtolower($method);

    if ($method == 'get') {
        return empty($key) ? $_GET : (isset($_GET[$key]) ? ($function ? $function($_GET[$key]) : $_GET[$key]) : $default);
    } elseif ($method == 'post') {
        if (empty($_POST)) {
            $_POST = file_get_contents('php://input');
            if (strpos($_POST, '=')) {
                $_POST = parse_str($_POST, $data);
                $_POST = $data;
            } else {
                $_POST = json_decode(file_get_contents('php://input'), true);
            }
        }

        return empty($key) ? $_POST : (isset($_POST[$key]) ? ($function ? $function($_POST[$key]) : $_POST[$key]) : $default);
    } elseif ($method == 'request') {
        return empty($key) ? $_REQUEST : (isset($_REQUEST[$key]) ? ($function ? $function($_REQUEST[$key]) : $_REQUEST[$key]) : $default);
    } elseif ($method == 'param') {
        $param = array_merge($_GET, is_array($_POST) ? $_POST : array(), $_REQUEST);

        return empty($key) ? $param : (isset($param[$key]) ? ($function ? $function($param[$key]) : $param[$key]) : $default);
    } else {
        return false;
    }
}
PHP input()接收数据(get,post等)
© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论

    blank

    暂无评论内容