/** * Encodes a token from parameters and returns an instance of this class. * * The encoded token can be obtained using the {@link getEncoded()} method. * * @param Authorization $authorization the authorisation to use to create * this token * @param array $scope the scope of this token - this must be a subset * of the scope provided in `$authorization` * @param int $expires_in the time to expiry or {@link Token::TTL_PERPETUAL} * @param TokenSource $source_id if the token is created from a previous authorisation * code or refresh token, the ID of those artefacts * @param array $additional any additional data to be stored on the server for this token * @return AccessToken|null */ public static function create($authorization, $scope = array(), $expires_in = Token::TTL_PERPETUAL, $source = NULL, $additional = array()) { $token = new AccessToken(); $token->init($authorization, $scope, $expires_in, $source, $additional); $token->encode(); $token->is_parsed = true; return $token; }
/** * @return string */ public function getAccessToken() { if ($this->accessToken === null) { $this->accessToken = $at = new AccessToken(); $at->setApp($this->getApp())->setLogger($this->getLogger())->setCache($this->isCache()); } return $this->accessToken->getToken(); }
/** * @return AccessToken */ protected function createNewAccessToken() { $authResponse = $this->authClient->auth(); $accessToken = new AccessToken(); $accessToken->setToken($authResponse->getAccessToken()); $accessToken->setTokenType($authResponse->getTokenType()); $accessToken->setExpiresIn($authResponse->getExpiresIn()); return $accessToken; }
/** * @param array $params * An associative array with pairs of query param name and it value. * * @return self */ public function get(array $params = []) { if (isset($this->auth)) { $this->auth->setRequestMethod(__FUNCTION__); $this->auth->setRequestFields($params); } if (!empty($params)) { $this->options[CURLOPT_URL] .= '?' . http_build_query($params); } $this->options[CURLOPT_HTTPGET] = true; return $this; }
public function deleteAccessToken(AccessToken $accessToken) { if (!isset($_SESSION['php-oauth-client']['access_token'])) { return false; } foreach ($_SESSION['php-oauth-client']['access_token'] as $k => $t) { $token = unserialize($t); if ($accessToken->getAccessToken() !== $token->getAccessToken()) { continue; } unset($_SESSION['php-oauth-client']['access_token'][$k]); return true; } return false; }
public static function long2short($url) { $url = 'https://api.weixin.qq.com/cgi-bin/shorturl?access_token=' . AccessToken::getAccessToken(); $data = array('action' => 'long2short', 'long_url' => $url); $res = Curl::httpGet($url, $data); return Error::isError($res) ? false : $res['short_url']; }
/** * 获取自动回复规则 * 本接口与自定义菜单查询接口的不同之处在于,本接口无论公众号的接口是如何设置的,都能查询到接口,而自定义菜单查询接口则仅能查询到使用API设置的菜单配置。 * Url:http://mp.weixin.qq.com/wiki/7/7b5789bb1262fb866d01b4b40b0efecb.html * @return bool|mixed * * 返回: */ public static function getAutoreply() { //获取ACCESS_TOKEN $accessToken = AccessToken::getAccessToken(); $url = 'https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token=' . $accessToken; return Curl::callWebServer($url, '', 'GET'); }
/** * get access token * * @param $code * @return mixed|void */ protected function requestAccessToken($code) { $keys = array(); $keys['code'] = $code; $keys['redirect_uri'] = $this->getOptions()->callback_url; try { $accessToken = new AccessToken(); $paramsAll = $this->getQqApi()->qq_callback(); $accessToken->setToken($paramsAll['access_token']); $accessToken->setExpiresIn($paramsAll['expires_in']); $accessToken->setOpenId($this->getQqApi()->get_openid()); return $accessToken; } catch (\OAuthException $e) { throw $e; } }
/** * 检查访问授权是否有效 * * @param AccessToken $token * @return bool */ public function isVoildAccessToken($token) { if (!$token) { return false; } return $token->getExpiresIn() < time(); }
/** * 获取微信服务器IP列表 */ public static function getWeChatIPList() { //获取ACCESS_TOKEN $accessToken = AccessToken::getAccessToken(); $url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=' . $accessToken; return Curl::callWebServer($url, '', 'GET'); }
public static function uploadNews($articles) { /* 传入data实例 $articles[] = array( 'thumb_media_id'=> $media_id, 'author'=> '天王盖地虎', 'title'=> '这个我的测试消息', 'content_source_url'=> 'our-class.cn', 'content'=> '这是一个图文摘要', 'digest'=> '', 'show_cover_pic'=> 1, ); $articles[] = array( 'thumb_media_id'=> $media_id, 'author'=> '天王盖地虎', 'title'=> '这个我的测试消息', 'content_source_url'=> 'our-class.cn', 'content'=> '这是一个图文摘要', 'digest'=> '', 'show_cover_pic'=> 1, ); */ $url = 'https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=' . AccessToken::getAccessToken(); if (count($articles) > 10) { href('文章总数大于10'); return false; } $data = array('articles' => $articles); $res = Curl::httpGet($url, $data); return Error::isError($res) ? false : $res['media_id']; }
/** * 下载多媒体文件 * @param $mediaId 多媒体ID * @return 头信息如下 * * HTTP/1.1 200 OK * Connection: close * Content-Type: image/jpeg * Content-disposition: attachment; filename="MEDIA_ID.jpg" * Date: Sun, 06 Jan 2013 10:20:18 GMT * Cache-Control: no-cache, must-revalidate * Content-Length: 339721 * curl -G "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID" */ public static function download($mediaId) { //获取ACCESS_TOKEN $accessToken = AccessToken::getAccessToken(); $queryUrl = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=' . $accessToken . '&media_id=' . $mediaId; return Curl::callWebServer($queryUrl, '', 'GET'); }
public static function getIp() { $accessToken = AccessToken::getAccessToken(); $url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=' . $accessToken; $res = Curl::httpGet($url); return Error::isError($res) ? false : $res['ip_list']; }
public static function getTemplateId($template_id_short) { $url = 'https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=' . AccessToken::getAccessToken(); $data = array('template_id_short' => $template_id_short); $res = Curl::httpGet($url, $data); return Error::isError($res) ? false : $res['template_id']; }
/** * 获取摇周边的设备及用户信息 * 获取设备信息,包括UUID、major、minor,以及距离、openID等信息。 * 根据 $ticket 获取用户及设备信息 * * @param $ticket 摇周边业务的ticket,可在摇到的URL中得到,ticket生效时间为30分钟 * * @return array("data"=>array("page_id "=>1421,"openid"=> "oVDmXjp7y8aG2AlBuRpMZTb1-cmA",'beacon_info'=>array("distance"=> 55.00620700469034,"major"=> 10001,"minor"=> 19007,"uuid"=> "FDA50693-A4E2-4FB1-AFCF-C6EB07647825")), "errcode"=> 0,"errmsg"=> "success.") */ public static function getShakeInfo($ticket) { //获取ACCESS_TOKEN $accessToken = AccessToken::getAccessToken(); $queryUrl = 'https://api.weixin.qq.com/shakearound/user/getshakeinfo?access_token=' . $accessToken; $data = json_encode(array('ticket' => $ticket)); return Curl::callWebServer($queryUrl, $data, 'POST', 1, 0); }
/** * 将一条长链接转成短链接。 * 主要使用场景:开发者用于生成二维码的原链接(商品、支付二维码等)太长导致扫码速度和成功率下降,将原长链接通过此接口转成短链接再生成二维码将大大提升扫码速度和成功率。 * @param $longUrl String 需要转换的长链接,支持http://、https://、weixin://wxpay 格式的url * @return array('errcode'=>0, 'errmsg'=>'错误信息', 'short_url'=>'http://t.cn/asdasd')错误码为0表示正常 */ public static function long2short($longUrl) { $queryUrl = 'https://api.weixin.qq.com/cgi-bin/shorturl?access_token=' . AccessToken::getAccessToken(); $queryAction = 'POST'; $template = array(); $template['long_url'] = $longUrl; $template['action'] = 'long2short'; return Curl::callWebServer($queryUrl, '', $queryAction); }
/** * 上传图片素材 * 上传在摇一摇页面展示的图片素材,素材保存在微信侧服务器上。 * 格式限定为:jpg,jpeg,png,gif,图片大小建议120px*120 px,限制不超过200 px *200 px,图片需为正方形。 * * @param $filename,文件绝对路径 * * @return array("data"=>array("pic_url"=>"http://shp.qpic.cn/wechat_shakearound_pic/0/1428377032e9dd2797018cad79186e03e8c5aec8dc/120"), "errcode"=> 0,"errmsg"=> "success.") */ public static function upload($filename) { //获取ACCESS_TOKEN $accessToken = AccessToken::getAccessToken(); $queryUrl = 'https://api.weixin.qq.com/shakearound/material/add?access_token=' . $accessToken; $data = array(); $data['media'] = '@' . $filename; return Curl::callWebServer($queryUrl, $data, 'POST', 1, 0); }
/** * 获取自动回复规则 * * @return String 返回结果与字段说明请查看http://mp.weixin.qq.com/wiki/7/7b5789bb1262fb866d01b4b40b0efecb.html */ public static function getRole($industryId1, $industryId2) { $queryUrl = 'https://api.weixin.qq.com/cgi-bin/get_current_autoreply_info?access_token=' . AccessToken::getAccessToken(); $queryAction = 'POST'; $template = array(); $template['industry_id1'] = "{$industryId1}"; $template['industry_id2'] = "{$industryId2}"; $template = json_encode($template); return Curl::callWebServer($queryUrl, $template, $queryAction); }
public function revokeAccessToken($token) { $access_token = AccessToken::with('session')->find($token); if (!$access_token || !Auth::check() || $access_token->session->owner_id != Auth::id()) { Flash::error(lang('Revoke Failed')); } else { $access_token->delete(); Flash::success(lang('Revoke success')); } return Redirect::route('users.access_tokens', Auth::id()); }
static function getuser($begin_date, $end_date, $summary = true) { if ($summary) { $url = 'https://api.weixin.qq.com/datacube/getusersummary?access_token=' . AccessToken::getAccessToken(); } else { $url = 'https://api.weixin.qq.com/datacube/getusercumulate?access_token=' . AccessToken::getAccessToken(); } $data = array('begin_date' => $begin_date, 'end_date' => $end_date); $res = Curl::httpGet($url, $data); return Error::isError($res) ? false : $res; }
public static function setImage($file, $kf_account) { if (!file_exists($file)) { href('文件不存在'); return false; } $url = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=' . AccessToken::getAccessToken() . '&kf_account=' . $kf_account; $data = array('filename' => $file); $data = array("media" => "@" . $file, 'form-data' => $data); $res = Curl::httpGet($url, $data); return Error::isError($res) ? false : $res; }
/** * 向用户推送模板消息 * @param $data = array( * 'first'=>array('value'=>'您好,您已成功消费。', 'color'=>'#0A0A0A') * 'keynote1'=>array('value'=>'巧克力', 'color'=>'#CCCCCC') * 'keynote2'=>array('value'=>'39.8元', 'color'=>'#CCCCCC') * 'keynote3'=>array('value'=>'2014年9月16日', 'color'=>'#CCCCCC') * 'keynote3'=>array('value'=>'欢迎再次购买。', 'color'=>'#173177') * ); * @param $touser 接收方的OpenId。 * @param $templateId 模板Id。在公众平台线上模板库中选用模板获得ID * @param $url URL * @param string $topcolor 顶部颜色, 可以为空。默认是红色 * @return array("errcode"=>0, "errmsg"=>"ok", "msgid"=>200228332} "errcode"是0则表示没有出错 * * 注意:推送后用户到底是否成功接受,微信会向公众号推送一个消息。 */ public static function sendTemplateMessage($data, $touser, $templateId, $url, $topcolor = '#FF0000') { $queryUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=' . AccessToken::getAccessToken(); $queryAction = 'POST'; $template = array(); $template['touser'] = $touser; $template['template_id'] = $templateId; $template['url'] = $url; $template['topcolor'] = $topcolor; $template['data'] = $data; $template = json_encode($template); return Curl::callWebServer($queryUrl, $template, $queryAction); }
public function verify() { $receivedOtp = Input::get('otp'); $email = Input::get('email'); $phone = Input::get('phone'); try { $user = User::where('email', '=', $email)->where('phone', '=', $phone)->where('otp', '=', $receivedOtp)->firstOrFail(); $accessToken = AccessToken::create(['token' => $this->generateAccessToken(), 'user_id' => $user->id]); return Response::json(['msg' => 'valid', 'user' => $user, 'token' => $accessToken->token]); } catch (ModelNotFoundException $e) { return Response::unauthorized(); } }
/** * @descrpition 从微信服务器获取js sdk api_ticket * @return Ambigous|bool */ private static function _getJsapiTicket() { $accessToken = AccessToken::getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=" . $accessToken; $JsapiTicket = Curl::callWebServer($url, '', 'GET'); if (!isset($JsapiTicket['ticket'])) { return Msg::returnErrMsg(MsgConstant::ERROR_GET_ACCESS_TOKEN, '获取js ticket失败'); } $JsapiTicket['time'] = time(); $JsapiTicketJson = json_encode($JsapiTicket); //存入数据库 $db = new mysql(); $db->connect(DBHOST, DBUSER, DBPASSWORD, DBNAME); $sql = "update jsapiticket set ticket='" . $JsapiTicketJson . "' where id=1"; $db->query($sql); return $JsapiTicket; }
public static function addMedia($file, $type = 'image', $info = array()) { $url = 'https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=' . AccessToken::getAccessToken(); if (!file_exists($file)) { href('文件不存在'); exit; } $data = array('filename' => $file); if ($type == 'video') { $description = array('title' => $info['title'], 'introduction' => $info['introduction']); $data = array("media" => "@" . $file, 'form-data' => $data, 'type' => $type, 'description' => $description); } else { $data = array("media" => "@" . $file, 'form-data' => $data, 'type' => $type); } $res = Curl::httpGet($url, $data); return Error::isError($res) ? false : $res; }
private static function getJsApiTicket() { $mmc = memcache_init(); $ticketInfo = @$mmc->get('ticket'); if (isset($ticketInfo['ticket']) && time() - $ticketInfo['expires'] < 7000) { return $ticketInfo['ticket']; } $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=" . AccessToken::getAccessToken(); $res = Curl::httpGet($url); if (Error::isError($res)) { exit; } $data['ticket'] = $res['ticket']; $data['expires'] = time(); //存入Memcache $mmc->set('ticket', $data, 7200); return $res['ticket']; }
/** * @descrpition 从微信服务器获取微信API_TICKET * @return Ambigous|bool */ private static function _getApiTicket(){ $accessToken = AccessToken::getAccessToken(true); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $apiTicket = Curl::callWebServer($url, '', 'GET'); if(!isset($apiTicket['ticket'])){ return Msg::returnErrMsg(MsgConstant::ERROR_GET_API_TICKET, '获取API_TICKET失败'); } $apiTicket['time'] = time(); $apiTicketJson = json_encode($apiTicket); //存入数据库 /** * 这里通常我会把api_ticket存起来,然后用的时候读取,判断是否过期,如果过期就重新调用此方法获取,存取操作请自行完成 * * 请将变量$apiTicketJson给存起来,这个变量是一个字符串 */ $f = fopen(self::_fileName2Store(), 'w+'); fwrite($f, $apiTicketJson); fclose($f); return $apiTicket; }
/** * 语义理解 * 单类别意图比较明确,识别的覆盖率比较大,所以如果只要使用特定某个类别,建议将category只设置为该类别。 * @param $query 输入文本串,如“查一下明天从北京到上海的南航机票" * @param $category String 需要使用的服务类型,如“flight,hotel”,多个用“,”隔开,不能为空。详见《接口协议文档》 * @param $latitude Float 纬度坐标,与经度同时传入;与城市二选一传入。详见《接口协议文档》 * @param $longitude Float 经度坐标,与纬度同时传入;与城市二选一传入。详见《接口协议文档》 * @param $region String 区域名称,在城市存在的情况下可省;与经纬度二选一传入。详见《接口协议文档》 * @param $city 城市名称,如“北京”,与经纬度二选一传入 * @param $openId * @return bool|mixed * 《接口协议文档》:http://mp.weixin.qq.com/wiki/images/1/1f/微信语义理解协议文档.zip */ public static function semanticSemproxy($query, $category, $openId, $latitude = '', $longitude = '', $region = '', $city = '') { $queryUrl = 'https://api.weixin.qq.com/semantic/semproxy/search?access_token=' . AccessToken::getAccessToken(); $queryAction = 'POST'; $template = array(); $template['query'] = $query; $template['category'] = $category; $template['appid'] = WECHAT_APPID; $template['uid'] = $openId; if (!empty($latitude)) { $template['latitude'] = $latitude; } if (!empty($longitude)) { $template['longitude'] = $longitude; } if (!empty($region)) { $template['region'] = $region; } if (!empty($city)) { $template['city'] = $city; } $template = json_encode($template); return Curl::callWebServer($queryUrl, $template, $queryAction, 0, 0); }
/** * 获取JsApiTicket * 本接口与自定义菜单查询接口的不同之处在于,本接口无论公众号的接口是如何设置的,都能查询到接口,而自定义菜单查询接口则仅能查询到使用API设置的菜单配置。 * Url:http://mp.weixin.qq.com/wiki/7/7b5789bb1262fb866d01b4b40b0efecb.html * @return string|ticket * * 返回:ticket值 */ public static function getJsApiTicket() { // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 $data = json_decode(file_get_contents("jsapi_ticket.json")); if ($data->expire_time < time()) { $accessToken = AccessToken::getAccessToken(); // 如果是企业号用以下 URL 获取 ticket // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token={$accessToken}"; $res = Curl::callWebServer($url, '', 'GET'); $ticket = $res['ticket']; if ($ticket) { $data = array(); $data['expire_time'] = time() + 7000; $data['jsapi_ticket'] = $ticket; $fp = fopen("jsapi_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; }
/** * 设置备注名 开发者可以通过该接口对指定用户设置备注名,该接口暂时开放给微信认证的服务号。 * @param $openId 用户的openId * @param $remark 新的昵称 * @return array('errorcode'=>0, 'errmsg'=>'ok') 正常时是0 } */ public static function setRemark($openId, $remark) { //获取ACCESS_TOKEN $accessToken = AccessToken::getAccessToken(); $queryUrl = 'https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=' . $accessToken; $data = json_encode(array('openid' => $openId, 'remark' => $remark)); return Curl::callWebServer($queryUrl, $data, 'POST'); }