common_helper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. function curl_post1($url, $data = []) {
  3. $ch = curl_init ();
  4. $header = array ("Accept-Charset: utf-8",'Expect:' );
  5. curl_setopt ( $ch, CURLOPT_URL, $url );
  6. curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
  7. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  8. // curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
  9. // curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
  10. curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
  11. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
  12. curl_setopt($ch, CURLOPT_ENCODING, "gzip");
  13. curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
  14. 15 Accept-Encoding: gzip,deflate,sdch' );
  15. curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, false );
  16. // curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
  17. curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 );
  18. // 最好加上http_build_query 转换,防止有些服务器不兼容
  19. if($data){
  20. curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query ( $data ) );
  21. }
  22. curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
  23. $result = curl_exec ( $ch );
  24. curl_close ( $ch );
  25. return $result;
  26. }
  27. function curl_post($url, $data = []) {
  28. $ch = curl_init();
  29. curl_setopt($ch, CURLOPT_URL, $url);
  30. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  31. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  32. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  33. curl_setopt($ch, CURLOPT_POST, 1);
  34. if($data){
  35. curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query ( $data ) );
  36. }
  37. $output = curl_exec($ch);
  38. curl_close($ch);
  39. return $output;
  40. }
  41. /**
  42. * AES 128 ecb 加密 与java加密保持一致
  43. * @param $data 加密字符串
  44. * @param $key 加密key
  45. * @return string 加密串
  46. */
  47. function aes_encrypt($data, $key='') {
  48. if(!$key){
  49. $key = get_instance()->config->item('encryption_key');
  50. }
  51. $data = openssl_encrypt($data, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
  52. return base64_encode($data);
  53. }
  54. /**
  55. * AES 128 ecb 解密 与java加密保持一致
  56. * @param $data 解密字符串
  57. * @param $key 加密key
  58. * @return string 解密串
  59. */
  60. function aes_decrypt($data, $key='') {
  61. if(!$key){
  62. $key = get_instance()->config->item('encryption_key');
  63. }
  64. $encrypted = base64_decode($data);
  65. return openssl_decrypt($encrypted, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
  66. }
  67. /**
  68. * 多维数组键值下划线转换为驼峰
  69. * @param $arr
  70. * @return array
  71. */
  72. function array_change_line_to_hump($arr){
  73. if(!is_array($arr)){
  74. return $arr;
  75. }
  76. $tem = [];
  77. foreach ($arr as $k=>$v){
  78. if(is_array($v)){
  79. $tem[line_to_hump($k)] = array_change_line_to_hump($v);
  80. }else{
  81. $tem[line_to_hump($k)] = $v;
  82. }
  83. }
  84. return $tem;
  85. }
  86. /**
  87. * 驼峰转下划线
  88. * @param $str
  89. * @return string|string[]|null
  90. */
  91. function hump_to_line($str){
  92. return preg_replace_callback('/([A-Z])/', function ($match) {
  93. return '_' . lcfirst($match[0]);
  94. }, $str);
  95. }
  96. /**
  97. * 下划线转驼峰
  98. * @param $str
  99. * @return string|string[]|null
  100. */
  101. function line_to_hump($str){
  102. $str = strtolower($str);
  103. return preg_replace_callback('/(_[a-z])/', function ($match) {
  104. return ucfirst(trim($match[0], '_'));
  105. }, $str);
  106. }