php工作中常用的方法总结,会不定期更新哦,喜欢的朋友记得点赞收藏
PHP随机生成指定时间范围的时间
/**
* 生成某个范围内的随机时间
* Author:刘星麟
* @param $beginTime 起始时间 格式为 Y-m-d H:i:s
* @param string $endTime 结束时间 格式为 Y-m-d H:i:s
* @param bool $now 是否是时间戳 格式为 Boolean
* @return false|int|string
*/
function random_date($beginTime, $endTime="", $now = true) {
$begin = strtotime($beginTime);
$end = $endTime == "" ? mktime() : strtotime($endTime);
if ($begin === false || $end === false) {
return false;
}
$timestamp = rand($begin, $end);
return $now ? date("Y-m-d H:i:s", $timestamp) : $timestamp;
}
curl发送get、post、put、delete请求
if (!function_exists('curl_data')) {
/**
* curl 请求
* Author:刘星麟
* @param $url
* @param $data
* @param string $method 支持 GET POST PUT DELETE
* @param string $type
* @return bool|string
*/
function curl_data($url, $data, $method = 'POST', $type = 'form-data')
{
//初始化
$ch = curl_init();
header('Content-Type:application/json; charset=utf-8');
$headers = [
'form-data' => ['Content-Type: multipart/form-data'], 'json' => ['Content-Type: application/json'],
];
//统一转化为大写
$method = strtoupper($method);
if ($method == 'GET') {
if ($data) {
$querystring = http_build_query($data);
$url = $url . '?' . $querystring;
}
}
// 请求头,可以传数组
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers[$type]);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($method == 'POST') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if ($method == 'PUT') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if ($method == 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //最大相应超时时间
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不从证书中检查SSL加密算法是否存在
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}
文件写入
if (!function_exists('write_file')) {
/**
* 文件写入
* Author:刘星麟
* @param $filePath 文件路径
* @param $fileName 文件名称
* @param $content 写入内容
* @return bool|int
*/
function write_file($filePath, $fileName, $content)
{
//判断文件是否存在,如果不存在则创建文件夹
if (!is_dir($filePath)) {
@mkdir($filePath, 0755, true);
}
$directory_separator = substr($filePath, (strlen($filePath) - 1), 1);
if ($directory_separator != '/') {
$filePath = $filePath . '/';
}
//文件名称
$fileName = $filePath . DIRECTORY_SEPARATOR . $fileName;
return file_put_contents($fileName, $content);
}
}
字符串截取
if (!function_exists('utf_substr')) {
/**
* 字符串的截取
* Author:刘星麟
* @param $str string 原字符串
* @param $start int 开始截取位置
* @param $len int 截取的长度
* @param $flag string 标志符
* @return string
*/
function utf_substr($str, $start = 0, $len = 130, $flag = '')
{
if (str2len($str) < $len) {
$flag = '';
}
return mb_substr(str_replace(["\r", "\n"], "", $str), $start, $len) . $flag;
}
}
字符串编码转换
if (!function_exists('charset')) {
/**
* 字符串转换成UTF-8
* Author:刘星麟
* @param string $data
* @return string
*/
function charset($data = '')
{
if (!empty($data)) {
$fileType = mb_detect_encoding($data, ['UTF-8', 'GBK', 'LATIN1', 'BIG5', 'GB2312']);
if ($fileType != 'UTF-8') {
$data = mb_convert_encoding($data, 'utf-8', $fileType);
}
}
return $data;
}
}
判断字符串长度
if (!function_exists('str2len')) {
/**
* 判断字符长度
* Author:刘星麟
* @param string $str
* @return float|int
*/
function str2len($str = '')
{
$str2len = (strlen($str) + mb_strlen($str, "UTF-8")) / 2;
return $str2len;
}
}
判断数据是否为空
if (!function_exists('_empty')) {
/**
* 判断是否为空
* Author:刘星麟
* @param $param
* @return bool
*/
function _empty($param)
{
if (empty($param) && !is_numeric($param)) {
return true;
} else {
return false;
}
}
}
检测json数据格式是否正确
if (!function_exists('json_validate')) {
/**
* 检测json数据格式是否正确
* Author:刘星麟
* @param $string
* @return bool
*/
function json_validate($string)
{
if (is_string($string)) {
@json_decode($string);
return (json_last_error() === JSON_ERROR_NONE);
} else {
return false;
}
}
}
获取当前url请求协议
更多请参考: https://blog.csdn.net/liuxl57805678/article/details/100302414
if (!function_exists('get_request_scheme')) {
/**
* 获取当前url请求协议
* Author:刘星麟
* @return string
*/
function get_request_scheme()
{
return ((int)$_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . '://';
}
}
PHP数组转字符串
https://blog.csdn.net/liuxl57805678/article/details/103288924
PHP获取用户IP地址
https://blog.csdn.net/liuxl57805678/article/details/103297827