common.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_post_json($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. $password = '33ab0a75ee246f85e41f2c15d867ca5d';
  81. $nonce = base64_encode($nonce);// . $created . $password;
  82. $signRawString = $nonce . $created . $password;//;//
  83. // echo "明文:", $signRawString,"\n";
  84. // echo "明文:", $signRawString,"\n";
  85. //$signRawString = '66C92B11FF8A425FB8D4CCFE2019-07-08T17:56:46Z33ab0a75ee246f85e41f2c15d867ca5d';
  86. //$signRawString='a';
  87. //var_dump(hash('sha256',$signRawString,true));exit;
  88. $passwordDigest = base64_encode(hash('sha256',$signRawString,true));
  89. //echo "明文: $signRawString \n";
  90. //echo "密文: $passwordDigest \n";
  91. //exit;
  92. $headerArr[] = 'Authorization: WSSE realm="SDP", profile="UsernameToken", type="Appkey"';
  93. $headerArr[] = 'X-WSSE:UsernameToken Username="d9c3190120db4b2ab078a543d59ba47d",PasswordDigest="' . $passwordDigest . '",Nonce="' . $nonce . '",Created="' . $created . '"';
  94. $headerArr[] = 'Content-Type: application/json; charset=UTF-8';
  95. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
  96. //var_dump(C('test_proxy'));exit;
  97. //if(C('test_proxy')) {
  98. //curl_setopt($ch, CURLOPT_PROXY, '192.168.1.211:8888');
  99. //}
  100. $ret = curl_exec($ch);
  101. if (empty($ret)) {
  102. var_dump(curl_error($ch)); // 查看报错信息
  103. }
  104. // exit('x');
  105. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  106. if ($httpCode != 200) {
  107. }
  108. curl_close($ch);
  109. //var_dump($ret);
  110. return $ret;
  111. }
  112. function curl_post_json_pay($url, $data,$headers=[],$timeout=100){
  113. $CA = false;
  114. $cacert = getcwd() . '/cacert.pem'; // CA根证书
  115. $SSL = substr($url, 0, 8) == "https://" ? true : false;
  116. $ch = curl_init();
  117. curl_setopt($ch, CURLOPT_POST, true);
  118. if(!empty($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  119. //var_dump($data);exit;
  120. curl_setopt($ch, CURLOPT_URL, $url);
  121. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  122. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
  123. //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;cli-test)');
  124. if ($SSL && $CA) {
  125. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
  126. curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布)
  127. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配
  128. } else if ($SSL && !$CA) {
  129. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  130. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 检查证书中是否设置域名
  131. }
  132. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //不输出内容到页面
  133. date_default_timezone_set("UTC");
  134. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  135. //var_dump(C('test_proxy'));exit;
  136. //if(C('test_proxy')) {
  137. //curl_setopt($ch, CURLOPT_PROXY, '192.168.16.96:8888');
  138. //}
  139. $ret = curl_exec($ch);
  140. if (empty($ret)) {
  141. var_dump(curl_error($ch)); // 查看报错信息
  142. }
  143. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  144. //
  145. // if ($httpCode != 200) {
  146. // $tmp = array();
  147. // $tmp['http_code'] = $httpCode;
  148. // $tmp['data'] = $ret;
  149. //
  150. // //echo "\n服务器错误:$httpCode";
  151. // //echo $ret;
  152. // $ret = json_encode($tmp);
  153. // }
  154. curl_close($ch);
  155. //var_dump($ret);
  156. return $ret;
  157. }
  158. /**
  159. * curl
  160. *
  161. * @param
  162. * string url
  163. * @param
  164. * array 数据
  165. * @param
  166. * int 请求超时时间
  167. * @param
  168. * bool HTTPS时是否进行严格认证
  169. * @return string
  170. */
  171. function curl_post_json_sdk($url, $data,$timeout=100){
  172. $CA = false;
  173. // $url = "http://www.baidu.com";
  174. $cacert = getcwd() . '/cacert.pem'; // CA根证书
  175. $SSL = substr($url, 0, 8) == "https://" ? true : false;
  176. $ch = curl_init();
  177. curl_setopt($ch, CURLOPT_POST, true);
  178. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  179. //var_dump($data);exit;
  180. curl_setopt($ch, CURLOPT_URL, $url);
  181. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  182. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
  183. //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;cli-test)');
  184. if ($SSL && $CA) {
  185. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
  186. curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布)
  187. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配
  188. } else if ($SSL && !$CA) {
  189. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  190. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 检查证书中是否设置域名
  191. }
  192. //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  193. // curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  194. // 'Expect:'
  195. // ));
  196. // var_dump($data);
  197. $headerArr=[];
  198. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
  199. //var_dump(C('test_proxy'));exit;
  200. //if(C('test_proxy')) {
  201. curl_setopt($ch, CURLOPT_PROXY, '192.168.16.96:8888');
  202. //}
  203. $ret = curl_exec($ch);
  204. if (empty($ret)) {
  205. var_dump(curl_error($ch)); // 查看报错信息
  206. }
  207. // var_dump($ret);
  208. // exit('x');
  209. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  210. if ($httpCode != 200) {
  211. $tmp = array();
  212. $tmp['http_code'] = $httpCode;
  213. $tmp['data'] = $ret;
  214. //echo "\n服务器错误:$httpCode";
  215. //echo $ret;
  216. $ret = json_encode($tmp);
  217. }
  218. curl_close($ch);
  219. //var_dump($ret);
  220. return $ret;
  221. }
  222. function I(string $key = '', $default = null, $filter = ''){
  223. return input($key,$default,$filter);
  224. }