php常用函数集合:
/**
 * @param $url 发送post请求的url
 * @param $data 发送的数据
 * @return mixed
 */
function curlPost($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //Problem (2) in the Chunked-Encoded data
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    @curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);  //php5.6.0开始,需要加上这行代码方可上传,否则取不到文件
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);
    $errorno = curl_errno($ch);

    if ($errorno) {
        $rt['sta'] = "0";
        $rt['msg'] = "curl错误:$errorno";
        return json_encode($rt);
        die;
    }

    return $tmpInfo;
}




/**
 * 全局唯一标识符
 * @return string
 */
function create_guid()
{
    $charid = strtoupper(md5(uniqid(mt_rand(), true)));
    //$hyphen = chr(45);// "-"
    $hyphen="";
    $uuid = substr($charid, 6, 2) . substr($charid, 4, 2) .
        substr($charid, 2, 2) . substr($charid, 0, 2) . $hyphen
        . substr($charid, 10, 2) . substr($charid, 8, 2) . $hyphen
        . substr($charid, 14, 2) . substr($charid, 12, 2) . $hyphen
        . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12);
    return $uuid;
}



/**
 * @有效HTTP地址
 */
function EffectiveHttp($str)
{
    if (strpos($str, "http://") === 0 or strpos($str, "https://") === 0) {
        return true;
    } else {
        return false;
    }
}



/**
 * 判断是ie8以及ie8以下内核浏览器
 */
function check_browser()
{
    if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE 8.0")) {
        return true;
    } else if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE 7.0")) {
        return true;
    } else if (strpos($_SERVER["HTTP_USER_AGENT"], "MSIE 6.0")) {
        return true;
    } else if (strpos($_SERVER["HTTP_USER_AGENT"], "Firefox")) {
        return false;
    } else if (strpos($_SERVER["HTTP_USER_AGENT"], "Chrome")) {
        return false;
    } else if (strpos($_SERVER["HTTP_USER_AGENT"], "Safari")) {
        return false;
    } else if (strpos($_SERVER["HTTP_USER_AGENT"], "Opera")) {
        return false;
    } else {
        return false;
    }


}


/**
 * 随机字符
 * @param number $length 长度
 * @param string $type 类型
 * @param number $convert 转换大小写
 * @return string
 */
function random($length = 6, $type = 'string', $convert = 0)
{
    $config = array(
        'number' => '1234567890',
        'letter' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
        'string' => 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
        'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
    );

    if (!isset($config[$type])) $type = 'string';
    $string = $config[$type];

    $code = '';
    $strlen = strlen($string) - 1;
    for ($i = 0; $i < $length; $i++) {
        $code .= $string{mt_rand(0, $strlen)};
    }
    if (!empty($convert)) {
        $code = ($convert > 0) ? strtoupper($code) : strtolower($code);
    }
    return $code;
}

/**
 * 文本记录函数
 * @param string $word 输入记录的值
 */
function logResJifen($word = '')
{
    $logname = "./log/jifen/" . date("Ymd") . ".txt";
    $fp = fopen($logname, "a");
    flock($fp, LOCK_EX);
    fwrite($fp, "执行日期:" . strftime("%Y-%m-%d %H:%M:%S", time()) . "\r\n" . $word . "\r\n");
    flock($fp, LOCK_UN);
    fclose($fp);
}


/**
 * 切除过长的字符串,用...代替
 * @param $string       字符串
 * @param $sublen       设置长度
 * @param int $start 开始位置
 * @param string $code
 * @return string       返回字符串结果
 */
function cut_str($string, $sublen, $start = 0, $code = 'UTF-8')
{
    if ($code == 'UTF-8') {
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
        preg_match_all($pa, $string, $t_string);
        if (count($t_string[0]) - $start > $sublen) {
            return join('', array_slice($t_string[0], $start, $sublen)) . "...";
        }

        return join('', array_slice($t_string[0], $start, $sublen));
    } else {
        $start = $start * 2;
        $sublen = $sublen * 2;
        $strlen = strlen($string);
        $tmpstr = '';
        for ($i = 0; $i < $strlen; $i++) {
            if ($i >= $start && $i < ($start + $sublen)) {
                if (ord(substr($string, $i, 1)) > 129) {
                    $tmpstr .= substr($string, $i, 2);
                } else {
                    $tmpstr .= substr($string, $i, 1);
                }
            }
            if (ord(substr($string, $i, 1)) > 129) {
                $i++;
            }

        }
        if (strlen($tmpstr) < $strlen) {
            $tmpstr .= "...";
        }

        return $tmpstr;
    }
}


/**
 * @return bool
 * 判断是否微信内置浏览器
 */

function is_weixin()
{
    if (empty($_SERVER['HTTP_USER_AGENT']) || strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') === false && strpos($_SERVER['HTTP_USER_AGENT'], 'Windows Phone') === false) {
        return false;
    }
    return true;
}


/**
 * @return bool
 * 判断是否手机浏览器
 */
function isMobile()
{
    $useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $useragent_commentsblock = preg_match('|\(.*?\)|', $useragent, $matches) > 0 ? $matches[0] : '';

    $mobile_os_list = array('Google Wireless Transcoder', 'Windows CE', 'WindowsCE', 'Symbian', 'Android', 'armv6l', 'armv5', 'Mobile', 'CentOS', 'mowser', 'AvantGo', 'Opera Mobi', 'J2ME/MIDP', 'Smartphone', 'Go.Web', 'Palm', 'iPAQ');
    $mobile_token_list = array('Profile/MIDP', 'Configuration/CLDC-', '160×160', '176×220', '240×240', '240×320', '320×240', 'UP.Browser', 'UP.Link', 'SymbianOS', 'PalmOS', 'PocketPC', 'SonyEricsson', 'Nokia', 'BlackBerry', 'Vodafone', 'BenQ', 'Novarra-Vision', 'Iris', 'NetFront', 'HTC_', 'Xda_', 'SAMSUNG-SGH', 'Wapaka', 'DoCoMo', 'iPhone', 'iPod');

    $found_mobile = CheckSubstrs($mobile_os_list, $useragent_commentsblock) ||
        CheckSubstrs($mobile_token_list, $useragent);

    if ($found_mobile) {
        return true;
    } else {
        return false;
    }
}


/**
 * 把网址生成二维码   
 */
function url2qrcode($url){
    /*生成二维码*/
    vendor("phpqrcode.phpqrcode");
    $data =$url;

    $level = 'L';// 纠错级别:L、M、Q、H
    $size =4;// 点的大小:1到10,用于手机端4就可以了
    $QRcode = new \QRcode();

    ob_start();
    $QRcode->png($data,false,$level,$size,2);  //第二个参数false不生成图片文件
    $imageString = base64_encode(ob_get_contents());
    ob_end_clean();

    return $imageString;  //返回base64编码的图片流    前台显示图片:<img src="data:image/png;base64,base64编码的图片流" > 
}




/**
 * 获取当前页面的网址的url
 */
function getcurrenturl(){
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    return  $url;
}


/**
 * 3des加密函数
 */
function encrypt($string) {
        //Key
        $key = "wlphp.com";

        //Encryption
        $cipher_alg = MCRYPT_TRIPLEDES;

        $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); 

        $encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv);
        return base64_encode($encrypted_string);
        return $encrypted_string;
}

/**
 * 3des解密函数
 */
function decrypt($string) {
            $string = base64_decode($string);

            //key
            $key = "wlphp.com";

            $cipher_alg = MCRYPT_TRIPLEDES;

            $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); 

            $decrypted_string = mcrypt_decrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv);
            return trim($decrypted_string);
}



/*
 *获取微信公众服务号接口全局调用凭证access_token
 */
function getAccessToken($appid,$appsecret)
{
    // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
    $data = json_decode(get_php_file("access_token.php"));
    if ($data->expire_time < time()) {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
        $res = json_decode(httpGet($url));
        $access_token = $res->access_token;
        if ($access_token) {
            $data->expire_time = time() + 7000;
            $data->access_token = $access_token;
            set_php_file("access_token.php", json_encode($data));
        }
    } else {
        $access_token = $data->access_token;
    }
    return $access_token;
}

/**
 * 发送http的get请求
 * @param $url 请求地址
 * @return mixed
 */
function httpGet($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

/*
 *获取access_token
 */
function get_php_file($filename)
{
    return trim(substr(file_get_contents($filename), 15));
}


/*
 *设置access_token
 */
function set_php_file($filename, $content)
{
    $fp = fopen($filename, "w");
    fwrite($fp, "<?php exit();?>" . $content);
    fclose($fp);
}




/**
 * 二维数组根据某个字段去重
 * @param array $array  二维数组
 * @para array $result 去重后的数组
 */
 function uniquArr($array){
    $result = array();
    foreach($array as $k=>$val){
        $code = false;
        foreach($result as $_val){
            if($_val['classifyname'] == $val['classifyname']){
                $code = true;
                break;
            }
        }
        if(!$code){
            $result[]=$val;
        }
    }
    return $result;
}