common.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. function flag_options()
  13. {
  14. return ['a' => 'a', 'b' => 'b'];
  15. }
  16. function post_json_data($url, $data_string)
  17. {
  18. $ch = curl_init();
  19. curl_setopt($ch, CURLOPT_POST, 1);
  20. curl_setopt($ch, CURLOPT_URL, $url);
  21. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  22. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  23. 'Content-Type: application/json; charset=utf-8',
  24. 'Content-Length: ' . strlen($data_string))
  25. );
  26. ob_start();
  27. curl_exec($ch);
  28. $return_content = ob_get_contents();
  29. ob_end_clean();
  30. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  31. return array('code' => $return_code, 'result' => $return_content);
  32. }
  33. $arr = array('a' => '555', 'b' => 56454564);
  34. //dump(post_json_data('http://192.168.211.1/html/dump.php',json_encode($arr)));
  35. /**
  36. * curl
  37. *
  38. * @param
  39. * string url
  40. * @param
  41. * array 数据
  42. * @param
  43. * int 请求超时时间
  44. * @param
  45. * bool HTTPS时是否进行严格认证
  46. * @return string
  47. */
  48. function curl_huawei_sms($url, $data, $timeout=100){
  49. $CA = false;
  50. // $url = "http://www.baidu.com";
  51. $cacert = getcwd() . '/cacert.pem'; // CA根证书
  52. $SSL = substr($url, 0, 8) == "https://" ? true : false;
  53. $ch = curl_init();
  54. curl_setopt($ch, CURLOPT_POST, true);
  55. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  56. //var_dump($data);exit;
  57. curl_setopt($ch, CURLOPT_URL, $url);
  58. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  59. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
  60. //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;cli-test)');
  61. if ($SSL && $CA) {
  62. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
  63. curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布)
  64. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配
  65. } else if ($SSL && !$CA) {
  66. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  67. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 检查证书中是否设置域名
  68. }
  69. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //不输出内容到页面
  70. // curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  71. // 'Expect:'
  72. // ));
  73. // var_dump($data);
  74. date_default_timezone_set("UTC");
  75. $nonce = '66C92B11FF8A425FB8D4CCFE';
  76. //$nonce = $nonce1 = mt_rand(100000,9999999999);
  77. $time = time();
  78. $created = date('Y-m-d', $time) . 'T' . date('H:i:s', $time) . 'Z';
  79. //$created = '2019-07-10T15:02:08Z';
  80. $username = 'd9c3190120db4b2ab078a543d59ba47d';
  81. $password = '33ab0a75ee246f85e41f2c15d867ca5d';
  82. //正式
  83. $username='67ef6805e6004cce9cce24b7f13767c6';
  84. $password = '1e88f55b4e034b1783e2686ec9dfbffd';
  85. $nonce = base64_encode($nonce);// . $created . $password;
  86. $signRawString = $nonce . $created . $password;//;//
  87. echo "明文:", $signRawString,"\n";
  88. echo "明文:", $signRawString,"\n";
  89. //$signRawString = '66C92B11FF8A425FB8D4CCFE2019-07-08T17:56:46Z33ab0a75ee246f85e41f2c15d867ca5d';
  90. //$signRawString='a';
  91. //var_dump(hash('sha256',$signRawString,true));exit;
  92. $passwordDigest = base64_encode(hash('sha256',$signRawString,true));
  93. //echo "明文: $signRawString \n";
  94. //echo "密文: $passwordDigest \n";
  95. //exit;
  96. $headerArr[] = 'Authorization: WSSE realm="SDP", profile="UsernameToken", type="Appkey"';
  97. $headerArr[] = 'X-WSSE:UsernameToken Username="'.$username.'",PasswordDigest="' . $passwordDigest . '",Nonce="' . $nonce . '",Created="' . $created . '"';
  98. $headerArr[] = 'Content-Type: application/json; charset=UTF-8';
  99. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
  100. //var_dump(C('test_proxy'));exit;
  101. //if(C('test_proxy')) {
  102. curl_setopt($ch, CURLOPT_PROXY, '192.168.16.96:8888');
  103. //}
  104. $ret = curl_exec($ch);
  105. if (empty($ret)) {
  106. var_dump(curl_error($ch)); // 查看报错信息
  107. }
  108. // exit('x');
  109. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  110. if ($httpCode != 200) {
  111. }
  112. curl_close($ch);
  113. //var_dump($ret);
  114. return $ret;
  115. }
  116. function curl_post_json_pay($url, $data,$headers=[],$timeout=100){
  117. $CA = false;
  118. $cacert = getcwd() . '/cacert.pem'; // CA根证书
  119. $SSL = substr($url, 0, 8) == "https://" ? true : false;
  120. $ch = curl_init();
  121. curl_setopt($ch, CURLOPT_POST, true);
  122. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  123. //var_dump($data);exit;
  124. curl_setopt($ch, CURLOPT_URL, $url);
  125. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  126. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
  127. //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;cli-test)');
  128. if ($SSL && $CA) {
  129. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
  130. curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布)
  131. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配
  132. } else if ($SSL && !$CA) {
  133. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  134. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 检查证书中是否设置域名
  135. }
  136. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //不输出内容到页面
  137. date_default_timezone_set("UTC");
  138. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  139. //var_dump(C('test_proxy'));exit;
  140. //if(C('test_proxy')) {
  141. curl_setopt($ch, CURLOPT_PROXY, '192.168.16.96:8888');
  142. //}
  143. $ret = curl_exec($ch);
  144. if (empty($ret)) {
  145. var_dump(curl_error($ch)); // 查看报错信息
  146. }
  147. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  148. //
  149. // if ($httpCode != 200) {
  150. // $tmp = array();
  151. // $tmp['http_code'] = $httpCode;
  152. // $tmp['data'] = $ret;
  153. //
  154. // //echo "\n服务器错误:$httpCode";
  155. // //echo $ret;
  156. // $ret = json_encode($tmp);
  157. // }
  158. curl_close($ch);
  159. //var_dump($ret);
  160. return $ret;
  161. }
  162. /**
  163. * curl
  164. *
  165. * @param
  166. * string url
  167. * @param
  168. * array 数据
  169. * @param
  170. * int 请求超时时间
  171. * @param
  172. * bool HTTPS时是否进行严格认证
  173. * @return string
  174. */
  175. function curl_post_json_sdk($url, $data,$timeout=100){
  176. $CA = false;
  177. // $url = "http://www.baidu.com";
  178. $cacert = getcwd() . '/cacert.pem'; // CA根证书
  179. $SSL = substr($url, 0, 8) == "https://" ? true : false;
  180. $ch = curl_init();
  181. curl_setopt($ch, CURLOPT_POST, true);
  182. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  183. //var_dump($data);exit;
  184. curl_setopt($ch, CURLOPT_URL, $url);
  185. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  186. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
  187. //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;cli-test)');
  188. if ($SSL && $CA) {
  189. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
  190. curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布)
  191. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配
  192. } else if ($SSL && !$CA) {
  193. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  194. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 检查证书中是否设置域名
  195. }
  196. //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  197. // curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  198. // 'Expect:'
  199. // ));
  200. // var_dump($data);
  201. $headerArr=[];
  202. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
  203. //var_dump(C('test_proxy'));exit;
  204. //if(C('test_proxy')) {
  205. curl_setopt($ch, CURLOPT_PROXY, '192.168.16.96:8888');
  206. //}
  207. $ret = curl_exec($ch);
  208. if (empty($ret)) {
  209. var_dump(curl_error($ch)); // 查看报错信息
  210. }
  211. // var_dump($ret);
  212. // exit('x');
  213. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  214. if ($httpCode != 200) {
  215. $tmp = array();
  216. $tmp['http_code'] = $httpCode;
  217. $tmp['data'] = $ret;
  218. //echo "\n服务器错误:$httpCode";
  219. //echo $ret;
  220. $ret = json_encode($tmp);
  221. }
  222. curl_close($ch);
  223. //var_dump($ret);
  224. return $ret;
  225. }
  226. function I(string $key = '', $default = null, $filter = ''){
  227. return input($key,$default,$filter);
  228. }