123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace wx;
- /**
- * 微信开放平台接口
- */
- class Oplatform
- {
- /**
- * 获取普通token
- * @param $appid
- * @param $secret
- * @return mixed
- */
- public function getAccessToken($appid, $secret, $code)
- {
- $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
- $res = $this->curl($url);
- return json_decode($res, true);
- }
- /**
- * 请求接口
- * @param $url
- * @param null $data
- * @return mixed|null
- */
- protected function curl($url, $data=null)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-type: application/json; encoding=utf-8']);
- if (!empty($data)) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_HEADER, false);
-
- $data = curl_exec($curl);
- curl_close($curl);
- return $data;
- }
- /**
- * -JS-SDK使用权限签名算法
- * @param $accesstoken
- */
- public function getTicket($accesstoken)
- {
- $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$accesstoken.'&type=jsapi';
- $res = $this->curl($url);
- return json_decode($res, true);
- }
- /**
- * 普通access_token
- */
- public function AccessToken($appid, $secret)
- {
- $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
- $res = $this->curl($url);
- return json_decode($res, true);
- }
- }
|