〝 古人学问遗无力,少壮功夫老始成 〞
php常见方法封装,希望能给大家带来帮助,你知道的越多,你不知道的越多,常码字不易,出精品更难,没有特别幸运,那么请先特别努力,别因为懒惰而失败,还矫情地将原因归于自己倒霉。你必须特别努力,才能显得毫不费力。如果这篇文章能给你带来一点帮助,希望给飞兔小哥哥一键三连,表示支持,谢谢各位小伙伴们。
目录
一、curl的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;
-
}
二、curl的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;
-
}
三、随机字符串
-
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;
-
}
四、返回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();
-
}
五、数据过滤
-
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;
-
}
六、重定向
-
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);
-
}
-
}
七、获取客户端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;
-
}
转载:https://blog.csdn.net/weixin_41635750/article/details/117176817
查看评论