小言_互联网的博客

php常见方法封装

267人阅读  评论(0)

〝 古人学问遗无力,少壮功夫老始成 〞

php常见方法封装,希望能给大家带来帮助,你知道的越多,你不知道的越多,常码字不易,出精品更难,没有特别幸运,那么请先特别努力,别因为懒惰而失败,还矫情地将原因归于自己倒霉。你必须特别努力,才能显得毫不费力。如果这篇文章能给你带来一点帮助,希望给飞兔小哥哥一键三连,表示支持,谢谢各位小伙伴们。

目录

一、curl的get请求

二、curl的post请求

三、随机字符串

四、返回json数据

五、数据过滤

六、重定向

七、获取客户端ip


一、curl的get请求


  
  1. function get_curl($url, $timeout = 5) {
  2. $ch = curl_init();
  3. curl_setopt($ch,CURLOPT_URL,$url);
  4. curl_setopt($ch,CURLOPT_RETURNTRANSFER, true );
  5. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  6. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  7. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  8. $result = curl_exec( $ch );
  9. curl_close($ch);
  10. return $result;
  11. }

二、curl的post请求


  
  1. function post_curl($url, $data, $timeout = 5) {
  2. $ch = curl_init();
  3. curl_setopt($ch,CURLOPT_URL,$url);
  4. curl_setopt($ch,CURLOPT_RETURNTRANSFER, true );
  5. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  6. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  7. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  8. curl_setopt($ch, CURLOPT_POST, true);
  9. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  10. $result = curl_exec( $ch );
  11. curl_close($ch);
  12. return $result;
  13. }

三、随机字符串


  
  1. function random_str($len=10, $type=1){
  2. switch($type){
  3. case 2:
  4. $chars= '0123456789';
  5. break;
  6. case 3:
  7. $chars= 'abcdefghijklmnopqrstuvwxyz';
  8. break;
  9. case 4:
  10. $chars= 'ABDEFGHIJKLMNOPQRSTUVWXYZ';
  11. break;
  12. case 5:
  13. $chars= 'abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPQRSTUVWXYZ';
  14. break;
  15. case 6:
  16. $chars= 'abcdefghijklmnopqrstuvwxyz0123456789';
  17. break;
  18. default:
  19. $chars= 'abcdefghijklmnopqrstuvwxyzABDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  20. break;
  21. }
  22. $str = '';
  23. for ( $i = 0; $i <$len; $i++ ){
  24. $str .= $chars[ mt_rand( 0, strlen($chars) - 1) ];
  25. }
  26. return $str;
  27. }

四、返回json数据


  
  1. function json_back($data, $is_exit = true) {
  2. $callback = $_GET[ 'callback'] ?? '';
  3. if ($callback) {
  4. echo '' . $callback . "(" . json_encode($data, JSON_UNESCAPED_UNICODE) . ")";
  5. } else {
  6. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  7. }
  8. if ($is_exit) exit();
  9. }

五、数据过滤


  
  1. function check_data($data){
  2. if(is_array($data)){
  3. foreach($data as $key => $v){
  4. $data[$key] = $this->check_data($v);
  5. }
  6. } else{
  7. $data = trim($data);
  8. $data = strip_tags($data);
  9. $data = htmlspecialchars($data);
  10. $data = addslashes($data);
  11. }
  12. return $data;
  13. }

六、重定向


  
  1. function redirect($url = 'https://blog.csdn.net/weixin_41635750'){
  2. if (!headers_sent()) {
  3. header( "Location: {$url}");
  4. exit();
  5. } else{
  6. $str = "<meta http-equiv='Refresh' content='0;URL={$url}'>";
  7. exit($str);
  8. }
  9. }

七、获取客户端ip


  
  1. function get_real_ip() {
  2. if(@$_SERVER[ "HTTP_ALI_CDN_REAL_IP"]){
  3. $ip = $_SERVER[ "HTTP_ALI_CDN_REAL_IP"];
  4. }
  5. elseif (@$_SERVER[ "HTTP_X_FORWARDED_FOR"] ?: false) {
  6. $ip = $_SERVER[ "HTTP_X_FORWARDED_FOR"];
  7. $ips = explode( ',', $ip);
  8. $ip = $ips[ 0];
  9. } elseif (@$_SERVER[ "HTTP_CDN_SRC_IP"] ?: false) {
  10. $ip = $_SERVER[ "HTTP_CDN_SRC_IP"];
  11. } elseif (getenv( 'HTTP_CLIENT_IP')) {
  12. $ip = getenv( 'HTTP_CLIENT_IP');
  13. } elseif (getenv( 'HTTP_X_FORWARDED')) {
  14. $ip = getenv( 'HTTP_X_FORWARDED');
  15. } elseif (getenv( 'HTTP_FORWARDED_FOR')) {
  16. $ip = getenv( 'HTTP_FORWARDED_FOR');
  17. } elseif (getenv( 'HTTP_FORWARDED')) {
  18. $ip = getenv( 'HTTP_FORWARDED');
  19. } else {
  20. $ip = $_SERVER[ 'REMOTE_ADDR'];
  21. }
  22. $ip = str_replace( array( '::ffff:', '[', ']'), array( '', '', ''), $ip);
  23. return $ip;
  24. }

 


转载:https://blog.csdn.net/weixin_41635750/article/details/117176817
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场