<?php
// 这是系统自动生成的公共文件
/**
* @param $data
* @param bool $a
* 断点调试
*/
function p($data , $a = true){
echo '<pre>';
if ($a) {
print_r($data);
} else {
var_dump($data);
}
die;
}
function http_post($url,$post_data='',$headers = null) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT , 10);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
if ($headers) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
// CURLINFO_HEADER_OUT选项可以拿到请求头信息
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
//执行命令
$data = curl_exec($curl);
// 打印请求头信息
// echo curl_getinfo($curl, CURLINFO_HEADER_OUT);
//关闭URL请求
curl_close($curl);
return $data;
}
/**
* @param $text
* @param $length
* @return string
* 字符串截取
*/
function subtext($text, $length)
{
if(mb_strlen($text, 'utf8') > $length)
return mb_substr($text, 0, $length, 'utf8').'***';
return $text;
}
/**
* @return string
* 随机生成邀请码
*/
function create_invite_code() {
$code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$rand = $code[rand(0,25)]
.strtoupper(dechex(date('m')))
.date('d')
.substr(time(),-5)
.substr(microtime(),2,5)
.sprintf('%02d',rand(0,99));
for(
$a = md5( $rand, true ),
$s = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
$d = '',
$f = 0;
$f < 6;
$g = ord( $a[ $f ] ),
$d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
$f++
);
return $d;
}
/**
* @param $data
* @param string $msg
* @param string $table_name
* 写日志
*/
function write_log($data,$msg = '',$table_name = ''){
$url = './log/'. date('Y-m') .'/'. date('Y-m-d').'--' . $table_name .'.txt';
$dir_name = dirname($url);
if(!file_exists($dir_name)) {
mkdir(iconv("UTF-8","GBK",$dir_name),0755,true);
}
$fp = fopen($url,"a");//打开文件资源通道 不存在则自动创建
fwrite($fp, '时间:' . date('Y-m-d H:i:s') ."\r\n");
if ($msg) {
fwrite($fp, $msg ."\r\n");
}
fwrite($fp,var_export($data,true)."\r\n");//写入文件
fwrite($fp, '=========================================================================' ."\r\n");
fclose($fp);//关闭资源通道
}
/**
* @return string
* 获取IP
*/
function getIP()
{
static $realip;
if (isset($_SERVER)){
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$realip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$realip = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
/**
* 通过IP获得城市
*/
function getCity($ip = '')
{
$mip = file_get_contents("http://whois.pconline.com.cn/ip.jsp?ip=".$ip); //太平洋电脑网接口
$mip = iconv("gbk","utf-8",$mip);
$str = str_replace(PHP_EOL, '', $mip);
return $str;
}
/**
* @param $code
* @param $msg
* @param array $data
* @return string
* 返回JSON
*/
function returnJson($code,$msg,$data = []){
header('Content-Type:application/json; charset=utf-8');
exit( json_encode(['code'=>$code,'msg'=>$msg,'data'=>$data],JSON_UNESCAPED_UNICODE));
}