首页 > 开发 > PHP > 正文

一些php项目中比较通用的php自建函数的详解

2024-05-04 22:26:30
字体:
来源:转载
供稿:网友
以下一些php函数是我们it动力最常用的项目开发函数,这些函数还算是在比较多的项目中使用到的,也是比较通用的。
1.请求接口的处理函数
代码如下:
/**
 * curl访问程序接口
 * @param string
 * @return array
 */ 
function getCurlDate($url, $datas, $key) { 
    $datas['time'] = $_SERVER['REQUEST_TIME'] + 300; 
    $post_data['post'] = urlencode(authcode(serialize($datas), "ENCODE", $key)); 
//    echo $url; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    // 我们在POST数据哦! 
    curl_setopt($ch, CURLOPT_POST, 1); 
    // 把post的变量加上 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
    $output = curl_exec($ch); 
 //   dump(curl_error($ch)); 
    curl_close($ch); 
    return json_decode($output, true); 


2.获取文件扩展名
代码如下:
/**
 * @获取文件扩展名
 * @$pic string 图片路径
 */ 
function get_file_ext($pic) { 
    return substr($pic, strrpos($pic, '.') + 1); 


3.可逆的加密、解密函数
代码如下:
/**
 * 字符串加密
 * @param   $string     需加密的字符
 * @param   $operation  加密或解密
 * @param   $key        网站加密key,防止破解
 * @return  string
 */ 
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) { 
    $ckey_length = 4; 
    $key = md5($key ? $key : '^www.itokit.com$'); 
    $keya = md5(substr($key, 0, 16)); 
    $keyb = md5(substr($key, 16, 16)); 
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : ''; 

    $cryptkey = $keya . md5($keya . $keyc); 
    $key_length = strlen($cryptkey); 

    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string; 
    $string_length = strlen($string); 

    $result = ''; 
    $box = range(0, 255); 

    $rndkey = array(); 
    for ($i = 0; $i <= 255; $i++) { 
        $rndkey[$i] = ord($cryptkey[$i % $key_length]); 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表