PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/system/storage/vendor/zoujingli/wechat-php-sdk/Wechat/Lib/Tools.php

https://bitbucket.org/thuctapgridy/muagium
PHP | 268 lines | 156 code | 20 blank | 92 comment | 31 complexity | 4d3951c6d3af57d94bbca68ff181741f MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. namespace Wechat\Lib;
  3. use CURLFile;
  4. /**
  5. * 微信接口通用类
  6. *
  7. * @category WechatSDK
  8. * @subpackage library
  9. * @author Anyon <zoujingli@qq.com>
  10. * @date 2016/05/28 11:55
  11. */
  12. class Tools {
  13. /**
  14. * 产生随机字符串
  15. * @param int $length
  16. * @param string $str
  17. * @return string
  18. */
  19. static public function createNoncestr($length = 32, $str = "") {
  20. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  21. for ($i = 0; $i < $length; $i++) {
  22. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  23. }
  24. return $str;
  25. }
  26. /**
  27. * 获取签名
  28. * @param array $arrdata 签名数组
  29. * @param string $method 签名方法
  30. * @return bool|string 签名值
  31. */
  32. static public function getSignature($arrdata, $method = "sha1") {
  33. if (!function_exists($method)) {
  34. return false;
  35. }
  36. ksort($arrdata);
  37. $params = array();
  38. foreach ($arrdata as $key => $value) {
  39. $params[] = "{$key}={$value}";
  40. }
  41. return $method(join('&', $params));
  42. }
  43. /**
  44. * 生成支付签名
  45. * @param array $option
  46. * @param string $partnerKey
  47. * @return string
  48. */
  49. static public function getPaySign($option, $partnerKey) {
  50. ksort($option);
  51. $buff = '';
  52. foreach ($option as $k => $v) {
  53. $buff .= "{$k}={$v}&";
  54. }
  55. return strtoupper(md5("{$buff}key={$partnerKey}"));
  56. }
  57. /**
  58. * XML编码
  59. * @param mixed $data 数据
  60. * @param string $root 根节点名
  61. * @param string $item 数字索引的子节点名
  62. * @param string $id 数字索引子节点key转换的属性名
  63. * @return string
  64. */
  65. static public function arr2xml($data, $root = 'xml', $item = 'item', $id = 'id') {
  66. return "<{$root}>" . self::_data_to_xml($data, $item, $id) . "</{$root}>";
  67. }
  68. static private function _data_to_xml($data, $item = 'item', $id = 'id', $content = '') {
  69. foreach ($data as $key => $val) {
  70. is_numeric($key) && $key = "{$item} {$id}=\"{$key}\"";
  71. $content .= "<{$key}>";
  72. if (is_array($val) || is_object($val)) {
  73. $content .= self::_data_to_xml($val);
  74. } elseif (is_numeric($val)) {
  75. $content .= $val;
  76. } else {
  77. $content .= '<![CDATA[' . preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/", '', $val) . ']]>';
  78. }
  79. list($_key,) = explode(' ', $key . ' ');
  80. $content .= "</$_key>";
  81. }
  82. return $content;
  83. }
  84. /**
  85. * 将xml转为array
  86. * @param string $xml
  87. * @return array
  88. */
  89. static public function xml2arr($xml) {
  90. return json_decode(Tools::json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  91. }
  92. /**
  93. * 生成安全JSON数据
  94. * @param array $array
  95. * @return string
  96. */
  97. static public function json_encode($array) {
  98. return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function('$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'), json_encode($array));
  99. }
  100. /**
  101. * 以get方式提交请求
  102. * @param $url
  103. * @return bool|mixed
  104. */
  105. static public function httpGet($url) {
  106. $oCurl = curl_init();
  107. if (stripos($url, "https://") !== FALSE) {
  108. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  109. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  110. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
  111. }
  112. curl_setopt($oCurl, CURLOPT_URL, $url);
  113. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  114. $sContent = curl_exec($oCurl);
  115. $aStatus = curl_getinfo($oCurl);
  116. curl_close($oCurl);
  117. if (intval($aStatus["http_code"]) == 200) {
  118. return $sContent;
  119. } else {
  120. return false;
  121. }
  122. }
  123. /**
  124. * 以post方式提交请求
  125. * @param string $url
  126. * @param array|string $data
  127. * @return bool|mixed
  128. */
  129. static public function httpPost($url, $data) {
  130. $ch = curl_init();
  131. curl_setopt($ch, CURLOPT_URL, $url);
  132. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  133. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  134. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  135. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  136. curl_setopt($ch, CURLOPT_POST, TRUE);
  137. if (is_array($data)) {
  138. foreach ($data as &$value) {
  139. if (is_string($value) && stripos($value, '@') === 0 && class_exists('CURLFile', FALSE)) {
  140. $value = new CURLFile(realpath(trim($value, '@')));
  141. }
  142. }
  143. }
  144. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  145. $data = curl_exec($ch);
  146. curl_close($ch);
  147. if ($data) {
  148. return $data;
  149. }
  150. return false;
  151. }
  152. /**
  153. * 使用证书,以post方式提交xml到对应的接口url
  154. * @param string $url POST提交的内容
  155. * @param array $postdata 请求的地址
  156. * @param string $ssl_cer 证书Cer路径 | 证书内容
  157. * @param string $ssl_key 证书Key路径 | 证书内容
  158. * @param int $second 设置请求超时时间
  159. * @return bool|mixed
  160. */
  161. static public function httpsPost($url, $postdata, $ssl_cer = null, $ssl_key = null, $second = 30) {
  162. $ch = curl_init();
  163. curl_setopt($ch, CURLOPT_TIMEOUT, $second);
  164. curl_setopt($ch, CURLOPT_URL, $url);
  165. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  166. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  167. curl_setopt($ch, CURLOPT_HEADER, FALSE);
  168. /* 要求结果为字符串且输出到屏幕上 */
  169. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  170. /* 设置证书 */
  171. if (!is_null($ssl_cer) && file_exists($ssl_cer) && is_file($ssl_cer)) {
  172. curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
  173. curl_setopt($ch, CURLOPT_SSLCERT, $ssl_cer);
  174. }
  175. if (!is_null($ssl_key) && file_exists($ssl_key) && is_file($ssl_key)) {
  176. curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
  177. curl_setopt($ch, CURLOPT_SSLKEY, $ssl_key);
  178. }
  179. curl_setopt($ch, CURLOPT_POST, true);
  180. if (is_array($postdata)) {
  181. foreach ($postdata as &$data) {
  182. if (is_string($data) && stripos($data, '@') === 0 && class_exists('CURLFile', FALSE)) {
  183. $data = new CURLFile(realpath(trim($data, '@')));
  184. }
  185. }
  186. }
  187. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  188. $result = curl_exec($ch);
  189. curl_close($ch);
  190. if ($result) {
  191. return $result;
  192. } else {
  193. return false;
  194. }
  195. }
  196. /**
  197. * 读取微信客户端IP
  198. * @return null|string
  199. */
  200. static public function getAddress() {
  201. foreach (array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP', 'REMOTE_ADDR') as $header) {
  202. if (!isset($_SERVER[$header]) || ($spoof = $_SERVER[$header]) === NULL) {
  203. continue;
  204. }
  205. sscanf($spoof, '%[^,]', $spoof);
  206. if (!filter_var($spoof, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  207. $spoof = NULL;
  208. } else {
  209. return $spoof;
  210. }
  211. }
  212. return '0.0.0.0';
  213. }
  214. /**
  215. * 设置缓存,按需重载
  216. * @param string $cachename
  217. * @param mixed $value
  218. * @param int $expired
  219. * @return bool
  220. */
  221. static public function setCache($cachename, $value, $expired = 0) {
  222. return Cache::set($cachename, $value, $expired);
  223. }
  224. /**
  225. * 获取缓存,按需重载
  226. * @param string $cachename
  227. * @return mixed
  228. */
  229. static public function getCache($cachename) {
  230. return Cache::get($cachename);
  231. }
  232. /**
  233. * 清除缓存,按需重载
  234. * @param string $cachename
  235. * @return bool
  236. */
  237. static public function removeCache($cachename) {
  238. return Cache::del($cachename);
  239. }
  240. /**
  241. * SDK日志处理方法
  242. * @param string $msg 日志行内容
  243. * @param string $type 日志级别
  244. */
  245. static public function log($msg, $type = 'MSG') {
  246. Cache::put($type . ' - ' . $msg);
  247. }
  248. }