PHP常见方法封装总结
吾爱主题
阅读:238
2022-11-09 15:46:00
评论:0
1.get请求
?1 2 3 4 5 6 7 8 9 10 11 | 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 ; } |
2.post请求
?1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 ; } |
3.随机字符串
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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 ; } |
4.返回json数据
?1 2 3 4 5 6 7 8 9 | 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 (); } |
5.数据过滤
?1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 ; } |
6.重定向
?1 2 3 4 5 6 7 8 9 | 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 ); } } |
7.获取客户端ip
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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 ; } |
到此这篇关于PHP常见方法封装总结的文章就介绍到这了,更多相关PHP方法封装内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://mp.weixin.qq.com/s/iEJjZLsKdMb3bSwWJbmTTw
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。