Skip to content

Instantly share code, notes, and snippets.

@c9n
Created January 14, 2015 07:17
Show Gist options
  • Save c9n/a26924bba541e926c477 to your computer and use it in GitHub Desktop.
Save c9n/a26924bba541e926c477 to your computer and use it in GitHub Desktop.
获取微信 jsapi_ticket
<?php
Class WeixinApi {
private $appid;
private $secret;
function __construct ($appid, $secret) {
$this->appid = $appid;
$this->secret = $secret;
}
public function get_access_token () {
$api = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appid . '&secret=' . $this->secret;
$resp = json_decode(file_get_contents($api), true);
return is_null($resp) ? null : $resp['access_token'];
}
public function get_jsapi_ticket () {
$access_token = $this->get_access_token();
$api = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=' . $access_token;
$resp = json_decode(file_get_contents($api), true);
return is_null($resp) ? null : $resp['ticket'];
}
}
$weixin = new WeixinApi('appid', 'secret');
$ticket = $weixin->get_jsapi_ticket();
echo $ticket;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment