Oplatform.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace wx;
  3. /**
  4. * 微信开放平台接口
  5. */
  6. class Oplatform
  7. {
  8. /**
  9. * 获取普通token
  10. * @param $appid
  11. * @param $secret
  12. * @return mixed
  13. */
  14. public function getAccessToken($appid, $secret, $code)
  15. {
  16. $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
  17. $res = $this->curl($url);
  18. return json_decode($res, true);
  19. }
  20. /**
  21. * 请求接口
  22. * @param $url
  23. * @param null $data
  24. * @return mixed|null
  25. */
  26. protected function curl($url, $data=null)
  27. {
  28. $curl = curl_init();
  29. curl_setopt($curl, CURLOPT_URL, $url);
  30. curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-type: application/json; encoding=utf-8']);
  31. if (!empty($data)) {
  32. curl_setopt($curl, CURLOPT_POST, 1);
  33. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  34. }
  35. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  36. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  37. curl_setopt($curl, CURLOPT_HEADER, false);
  38. $data = curl_exec($curl);
  39. curl_close($curl);
  40. return $data;
  41. }
  42. /**
  43. * -JS-SDK使用权限签名算法
  44. * @param $accesstoken
  45. */
  46. public function getTicket($accesstoken)
  47. {
  48. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$accesstoken.'&type=jsapi';
  49. $res = $this->curl($url);
  50. return json_decode($res, true);
  51. }
  52. /**
  53. * 普通access_token
  54. */
  55. public function AccessToken($appid, $secret)
  56. {
  57. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
  58. $res = $this->curl($url);
  59. return json_decode($res, true);
  60. }
  61. }