function.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. function getOrderNo()
  3. {
  4. global $timestamp;
  5. return rand(11, 99) . $timestamp . rand(11, 99);
  6. }
  7. function WechatReturnSuccess()
  8. {
  9. echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
  10. exit();
  11. }
  12. function get_url_contents($url)
  13. {
  14. $ch = curl_init();
  15. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  16. curl_setopt($ch, CURLOPT_URL, $url);
  17. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  18. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  19. $result = curl_exec($ch);
  20. curl_close($ch);
  21. return $result;
  22. }
  23. function getAccessTokenByCode($code = '', $appid = '', $appsecret = '')
  24. {
  25. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code';
  26. return json_decode(get_url_contents($url), true);
  27. }
  28. function getBaseAccessToken($appid = '', $appsecret = '')
  29. {
  30. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
  31. $data = json_decode(get_url_contents($url), true);
  32. if (isset($data['access_token']) && isset($data['expires_in'])) {
  33. return $data['access_token'];
  34. }
  35. else {
  36. return false;
  37. }
  38. }
  39. ?>