沐朽阁
Hello World!
Mu Xiu Ge

PHP 常用封装函数大全

PHP 常用封装函数大全

万能Curl函数

function curl(url,paras = array())
{
    ch = curl_init();
    curl_setopt(ch, CURLOPT_URL, url);
    curl_setopt(ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt(ch, CURLOPT_SSL_VERIFYHOST, false);
    if (@paras['Header']) {
        Header =paras['Header'];
    } else {
        Header[] = "Accept:*/*";Header[] = "Accept-Encoding:gzip,deflate,sdch";
        Header[] = "Accept-Language:zh-CN,zh;q=0.8";Header[] = "Connection:close";
    }
    curl_setopt(ch, CURLOPT_HTTPHEADER,Header);
    if (@paras['ctime']) { // 连接超时
        curl_setopt(ch, CURLOPT_CONNECTTIMEOUT, paras['ctime']);
    } else {
        curl_setopt(ch, CURLOPT_CONNECTTIMEOUT, 30);
    }
    if (@paras['rtime']) { // 读取超时
        curl_setopt(ch, CURLOPT_TIMEOUT, paras['rtime']);
    }
    if (@paras['post']) {
        curl_setopt(ch, CURLOPT_POST, 1);
        curl_setopt(ch, CURLOPT_POSTFIELDS, paras['post']);
    }
    if (@paras['header']) {
        curl_setopt(ch, CURLOPT_HEADER, true);
    }
    if (@paras['cookie']) {
        curl_setopt(ch, CURLOPT_COOKIE,paras['cookie']);
    }
    if (@paras['refer']) {
        if (paras['refer'] == 1) {
            curl_setopt(ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
        } else {
            curl_setopt(ch, CURLOPT_REFERER, paras['refer']);
        }
    }
    if (@paras['ua']) {
        curl_setopt(ch, CURLOPT_USERAGENT,paras['ua']);
    } else {
        curl_setopt(ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
    }
    if (@paras['nobody']) {
        curl_setopt(ch, CURLOPT_NOBODY, 1);
    }
    curl_setopt(ch, CURLOPT_ENCODING, "gzip");
    curl_setopt(ch, CURLOPT_RETURNTRANSFER, 1);
    if (@paras['GetCookie']) {
        curl_setopt(ch, CURLOPT_HEADER, 1);result = curl_exec(ch);
        preg_match_all("/Set-Cookie: (.*?);/m",result, matches);headerSize = curl_getinfo(ch, CURLINFO_HEADER_SIZE);header = substr(result, 0,headerSize); //状态码
        body = substr(result, headerSize);ret = [
            "Cookie" => matches, "body" =>body, "header" => header, 'code' => curl_getinfo(ch, CURLINFO_HTTP_CODE)
        ];
        curl_close(ch);
        returnret;
    }
    ret = curl_exec(ch);
    if (@paras['loadurl']) {Headers = curl_getinfo(ch);ret = Headers['redirect_url'];
    }
    curl_close(ch);
    return $ret;
}

邮箱验证

function is_valid_email(email) {
    if (preg_match('/^[\w\-\.]+@[\w\-\.]+(\.\w+)+/', $email)) {
        return true;
    } else {
        return false;
    }
}

或者

function check_email(email)
{result = trim(email);
    if (filter_var(result, FILTER_VALIDATE_EMAIL)) {
        return true;
    } else {
        return false;
    }
}

301重定向跳转

function redirect(url) {
    header('location:'.url, false, 301);
    exit;
}

获取客户端IP

function real_ip(type = 0)
{ip = _SERVER['REMOTE_ADDR'];
    if (type <= 0 && isset(_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s',_SERVER['HTTP_X_FORWARDED_FOR'], matches)) {
        foreach (matches[0] as xip) {
            if (filter_var(xip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
                ip =xip;
                break;
            }
        }
    } elseif (type <= 0 && isset(_SERVER['HTTP_CLIENT_IP']) && filter_var(_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {ip = _SERVER['HTTP_CLIENT_IP'];
    } elseif (type <= 1 && isset(_SERVER['HTTP_CF_CONNECTING_IP']) && filter_var(_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
        ip =_SERVER['HTTP_CF_CONNECTING_IP'];
    } elseif (type <= 1 && isset(_SERVER['HTTP_X_REAL_IP']) && filter_var(_SERVER['HTTP_X_REAL_IP'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {ip = _SERVER['HTTP_X_REAL_IP'];
    }
    returnip;
}

X_FORWARDED_FOR:之前的获取真实IP方式,极易被伪造IP
X_REAL_IP:在网站使用CDN的情况下选择此项,在不使用CDN的情况下也会被伪造
REMOTE_ADDR:直接获取真实请求IP,无法被伪造,但可能获取到的是CDN节点IP

取中间文本

function getSubstr(str,leftStr, rightStr)
{left = strpos(str,leftStr);
    right = strpos(str, rightStr,left);
    if (left<0) return '';
    if (right > 0) {
        return substr(str,left + strlen(leftStr),right - left - strlen(leftStr));
    } else {
        return substr(str,left + strlen($leftStr));
    }
}

是否HTTPS访问

function is_https()
{
    if (isset(_SERVER['SERVER_PORT']) &&_SERVER['SERVER_PORT'] == 443) {
        return true;
    } elseif (isset(_SERVER['HTTPS']) && (strtolower(_SERVER['HTTPS']) == 'on' || server['HTTPS'] == '1')) {
        return true;
    } elseif (isset(_SERVER['HTTP_X_CLIENT_SCHEME']) && _SERVER['HTTP_X_CLIENT_SCHEME'] == 'https') {
        return true;
    } elseif (isset(_SERVER['HTTP_X_FORWARDED_PROTO']) && _SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        return true;
    } elseif (isset(_SERVER['REQUEST_SCHEME']) && _SERVER['REQUEST_SCHEME'] == 'https') {
        return true;
    } elseif (isset(_SERVER['HTTP_EWS_CUSTOME_SCHEME']) && $_SERVER['HTTP_EWS_CUSTOME_SCHEME'] == 'https') {
        return true;
    }
    return false;
}

JSONP转数组

function jsonp_decode(jsonp,assoc = false)
{
    jsonp = trim(jsonp);
    if (isset(jsonp[0]) &&jsonp[0] !== '[' && jsonp[0] !== '{') {begin = strpos(jsonp, '(');
        if (false !==begin) {
            end = strrpos(jsonp, ')');
            if (false !== end) {jsonp = substr(jsonp,begin + 1, end -begin - 1);
            }
        }
    }
    return json_decode(jsonp,assoc);
}
赞赏
©版权归属沐朽阁所有

Mu Xiu Ge

PHP 常用封装函数大全
PHP 常用封装函数大全 万能Curl函数 function curl($url, $paras = array()) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VER…
扫描二维码继续阅读
2020-12-22