欢迎来到代码驿站!

PHP代码

当前位置:首页 > 软件编程 > PHP代码

PHP常见方法封装总结

时间:2022-09-28 09:39:52|栏目:PHP代码|点击:

1.get请求

function get_curl($url, $timeout = 5) {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    $result = curl_exec( $ch );
    curl_close($ch);
    return $result;
}

2.post请求

function post_curl($url, $data, $timeout = 5) {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
    $result = curl_exec( $ch );
    curl_close($ch);
    return $result;
}

3.随机字符串

function random_str($len=10, $type=1){
    switch($type){
        case 2:
            $chars='0123456789';
            break;
        case 3:
            $chars='abcdefghijklmnopqrstuvwxyz';
            break;
        case 4:
            $chars='ABDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case 5:
            $chars='abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case 6:
            $chars='abcdefghijklmnopqrstuvwxyz0123456789';
            break;
        default:
            $chars='abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
            break;
    }
 
    $str = '';
    for ( $i = 0; $i <$len; $i++ ){
        $str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }
    return $str;
}

4.返回json数据

function json_back($data, $is_exit = true) {
    $callback = $_GET['callback'] ?? '';
    if ($callback) {
        echo '' . $callback . "(" . json_encode($data, JSON_UNESCAPED_UNICODE) . ")";
    } else {
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
    }
    if ($is_exit) exit();
}

5.数据过滤

function check_data($data){
    if(is_array($data)){
        foreach($data as $key => $v){
            $data[$key] = $this->check_data($v);
        }
    }else{
        $data = trim($data);
        $data = strip_tags($data);
        $data = htmlspecialchars($data);
        $data = addslashes($data);
    }
    return $data;
}

6.重定向

function redirect($url = 'https://blog.csdn.net/weixin_41635750'){
    if (!headers_sent()) {
        header("Location: {$url}");
        exit();
    }else{
        $str = "<meta http-equiv='Refresh' content='0;URL={$url}'>";
        exit($str);
    }
}

7.获取客户端ip

function get_real_ip() {
    if(@$_SERVER["HTTP_ALI_CDN_REAL_IP"]){
        $ip = $_SERVER["HTTP_ALI_CDN_REAL_IP"];
    }
    elseif (@$_SERVER["HTTP_X_FORWARDED_FOR"] ?: false) {
        $ip  = $_SERVER["HTTP_X_FORWARDED_FOR"];
        $ips = explode(',', $ip);
        $ip  = $ips[0];
    } elseif (@$_SERVER["HTTP_CDN_SRC_IP"] ?: false) {
        $ip = $_SERVER["HTTP_CDN_SRC_IP"];
    } elseif (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } elseif (getenv('HTTP_X_FORWARDED')) {
        $ip = getenv('HTTP_X_FORWARDED');
    } elseif (getenv('HTTP_FORWARDED_FOR')) {
        $ip = getenv('HTTP_FORWARDED_FOR');
    } elseif (getenv('HTTP_FORWARDED')) {
        $ip = getenv('HTTP_FORWARDED');
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    $ip = str_replace(array('::ffff:', '[', ']'), array('', '', ''), $ip);
 
    return $ip;
}

上一篇:php实现分页工具类分享

栏    目:PHP代码

下一篇:没有了

本文标题:PHP常见方法封装总结

本文地址:http://www.codeinn.net/misctech/214935.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有