/** * 设置红包活动抽奖开关 * * 开发者实时控制红包活动抽奖的开启和关闭。 * 注意活动抽奖开关只在红包活动有效期之内才能生效, * 如果不能确定红包活动有效期,请尽量将红包活动有效期的范围设置大。 * * @param unknown $accessToken * @param unknown $lottery_id 红包抽奖id,来自addlotteryinfo返回的lottery_id * @param boolean $onoff 活动抽奖开关,false:关闭,true:开启 * @throws YDWXException * @return boolean */ function ydwx_packet_setlotteryswitch($accessToken, $lottery_id, $onoff) { $http = new YDHttp(); $info = $http->get(YDWX_WEIXIN_BASE_URL2 . "shakearound/lottery/setlotteryswitch?access_token={$accessToken}&lottery_id={$lottery_id}&onoff=" . ($onoff ? "1" : "0")); $rst = new YDWXResponse($info); if (!$rst->isSuccess()) { throw new YDWXException($rst->errmsg, $rst->errcode); } return true; }
function ydwx_refresh_card_jsapi_ticket($token) { $http = new YDHttp(); $msg = $http->get(YDWX_WEIXIN_BASE_URL . "ticket/getticket?type=wx_card&access_token=" . $token); $ticket = new YDWXJsapiTicketResponse($msg); if ($ticket->isSuccess()) { return $ticket; } throw new YDWXException($ticket->errmsg, $ticket->errcode); }
/** * 网站应用获取用户信息 * @param unknown $accessToken 注意,这里的access token是oauth认证第二步微信返回的 * @param unknown $openid * @throws YDWXException * @return YDWXOAuthUser */ function ydwx_sns_userinfo($accessToken, $openid) { $http = new YDHttp(); $user = $http->get(YDWX_WEIXIN_WEB_BASE_URL . "userinfo?access_token={$accessToken}&openid={$openid}&lang=zh_CN"); $user = new YDWXOAuthUser($user); if ($user->isSuccess()) { return $user; } throw new YDWXException($user->errmsg); }
function ydwx_get_current_selfmenu_info($accessToken) { $http = new YDHttp(); $menus = json_decode($http->get(YDWX_WEIXIN_BASE_URL . "get_current_selfmenu_info?access_token=" . $accessToken), true); $array = array(); if (!@$menus['selfmenu_info']['button']) { return array(); } foreach ($menus['selfmenu_info']['button'] as $menu) { $array[] = YDWXMenu::build($menu); } return $array; }
<?php /** * 网站进行微信OAuth登陆 * 该页面可通过Redirect方式进行访问,或者直接在需要的地方include_once */ chdir(dirname(__FILE__)); //把工作目录切换到文件所在目录 include_once dirname(__FILE__) . '/__config__.php'; // state为交互时双方都会带着的get参数,用于做一些逻辑判断,如果没指定,则默认一个 if (!$state) { $state = "fromydwx"; } $redirect = YDWX_SITE_URL . 'ydwx/webauth.php'; if (!@$_GET['code'] && !@$_GET['state']) { ob_clean(); header("Location: https://open.weixin.qq.com/connect/qrconnect?appid=" . YDWX_WEIXIN_WEB_APP_ID . "&redirect_uri={$redirect}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect"); die; } if (!@$_GET['code'] && @$_GET['state']) { YDWXHook::do_hook(YDWXHook::AUTH_CANCEL); die; } $http = new YDHttp(); $info = json_decode($http->get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . YDWX_WEIXIN_WEB_APP_ID . "&secret=" . YDWX_WEIXIN_WEB_APP_SECRET . "&code=" . $_GET['code'] . "&grant_type=authorization_code"), true); if (!@$info['openid']) { YDWXHook::do_hook(YDWXHook::AUTH_FAIL, YDWXAuthFailResponse::errMsg($info['errmsg'], $info['errcode'])); die; } YDWXHook::do_hook(YDWXHook::AUTH_WEB_SUCCESS, ydwx_sns_userinfo($info['access_token'], $info['openid'], $_GET['state']));
/** * Make an HTTP request * * @return API results */ protected function http($url, $method, $postfields = NULL, $multi = false) { curl_setopt($this->ci, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($this->ci, CURLOPT_SSL_VERIFYHOST, 1); if (YDWX_WEIXIN_COMPONENT_APP_ID) { $cert_file = YDWXHook::do_hook(YDWXHook::GET_HOST_APICLIENT_CERT_PATH, $this->appid); if (SAE_TMP_PATH) { if (copy($cert_file, SAE_TMP_PATH . "cert.pem")) { $cert_file = SAE_TMP_PATH . "cert.pem"; } } } else { $cert_file = YDWX_WEIXIN_APICLIENT_CERT; } curl_setopt($this->ci, CURLOPT_SSLCERTTYPE, 'PEM'); curl_setopt($this->ci, CURLOPT_SSLCERT, $cert_file); if (YDWX_WEIXIN_COMPONENT_APP_ID) { $key_file = YDWXHook::do_hook(YDWXHook::GET_HOST_APICLIENT_KEY_PATH, $this->appid); if (SAE_TMP_PATH) { if (copy($key_file, SAE_TMP_PATH . "key.pem")) { $key_file = SAE_TMP_PATH . "key.pem"; } } } else { $key_file = YDWX_WEIXIN_APICLIENT_KEY; } curl_setopt($this->ci, CURLOPT_SSLKEYTYPE, 'PEM'); curl_setopt($this->ci, CURLOPT_SSLKEY, $key_file); curl_setopt($this->ci, CURLOPT_SSLKEYPASSWD, YDWX_WEIXIN_COMPONENT_APP_ID ? YDWXHook::do_hook(YDWXHook::GET_HOST_MCH_KEY, $this->appid) : YDWX_WEIXIN_MCH_KEY); return parent::http($url, $method, $postfields, $multi); }
/** * 获取公众号中所设置的客服基本信息,包括客服工号、客服昵称、客服登录账号。 * * @param unknown $accessToken * @throws YDWXException * @return multitype:KFAccount */ function ydwx_kfaccount_getall($accessToken) { $http = new YDHttp(); $info = $http->post(YDWX_WEIXIN_BASE_URL . "customservice/getkflist?access_token={$accessToken}"); $rst = new YDWXResponse($info); if (!$rst->isSuccess()) { throw new YDWXException($rst->errmsg); } $rsts = array(); foreach ($rst->kf_list as $list) { $kf = new KFAccount(); $kf->kf_account = $list['kf_account']; $kf->kf_nick = $list['kf_nick']; $kf->kf_id = $list['kf_id']; $kf->kf_headimgurl = $list['kf_headimgurl']; $rsts[] = $kf; } return $rsts; }
/** * 根据分组群发消息, 要求认证 * * @see http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E6.A0.B9.E6.8D.AE.E5.88.86.E7.BB.84.E8.BF.9B.E8.A1.8C.E7.BE.A4.E5.8F.91.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91 * @param unknown $accessToken * @param YDWXMassByGroupRequest $arg * @return YDWXMassResponse */ function ydwx_message_send_by_group($accessToken, YDWXMassByGroupRequest $arg) { $openids = (array) $openids; $http = new YDHttp(); $info = $http->post(YDWX_WEIXIN_BASE_URL . "message/mass/sendall?access_token={$accessToken}", $arg->toJSONString()); return new YDWXMassResponse($info); }
/** * 拉取会员卡数据接口 * * 为支持开发者调用API查看卡券相关数据,微信卡券团队封装数据接口并面向具备卡券功能权限的开发者开放使用。开发者调用该接口可获取本商户下的所有卡券相关的总数据以及指定卡券的相关数据。开发过程请务必注意以下事项: * 1.查询时间区间需<=62天,否则报错{errcode: 61501,errmsg: "date range error"}; * 2.传入时间格式需严格参照示例填写”2015-06-15”,否则报错{errcode":61500,"errmsg":"date format error"}; * 3.需在获取卡券相关数据前区分卡券创建渠道:公众平台创建、调用卡券接口创建。 * * 支持开发者调用该接口拉取公众平台创建的会员卡相关数据。 * @param unknown $accessToken * @param unknown $begindate * @param unknown $enddate * @param unknown $cond_source * @throws YDWXException * @return multitype:YDWXCardCardStatistic */ function ydwx_card_datacube_getmembercardinfo($accessToken, $begindate, $enddate, $cond_source) { $http = new YDHttp(); $info = $http->post(YDWX_WEIXIN_BASE_URL2 . "datacube/getcardmembercardinfo?access_token={$accessToken}", ydwx_json_encode(array("begin_date" => $begindate, "end_date" => $enddate, "cond_source" => $cond_source))); $msg = new YDWXResponse($info); if ($msg->isSuccess()) { $array = array(); foreach ($msg->list as $info) { $obj = new YDWXMemberCardStatistic(); foreach ($info as $name => $value) { $obj->{$name} = $value; } $array[] = $obj; } return $array; } throw new YDWXException($msg->errmsg); }
/** * 微信统一下单接口,根据构造YDWXPayUnifiedOrderRequest的方式不同返回不同 * 通过 new YDWXPayUnifiedOrderRequest(true);返回的YDWXPayUnifiedOrderResponse中会有code_url(二维码内容) * 其他情况没用 * * 建议采用http://ydimage.yidianhulian.com/qrcode?str=二维码内容来生产二维码 * * @param YDWXPayUnifiedOrderRequest arg * @return YDWXPayUnifiedOrderResponse */ function ydwx_pay_unifiedorder(YDWXPayUnifiedOrderRequest $arg) { $arg->sign(); $args = $arg->toXMLString(); $http = new YDHttp(); $info = $http->post(YDWX_WEIXIN_PAY_URL . "pay/unifiedorder", $args); $msg = new YDWXPayUnifiedOrderResponse($info); if (!$msg->isSuccess()) { throw new YDWXException($msg->errmsg, $msg->errcode); } return $msg; }
/** * 批量拉取子商户信息接口 * 母商户 可以通过该接口批量拉取 子商户的相关信息,一次调用最多拉起100个子商户的信息,可 以通过多次拉去满足不同的查询需求 * @param unknown $accessToken 第三方平台的access token * @param unknown $begin_id merchant_id * @param unknown $limit 数量 最大100 * @param unknown $status YDWX_CARD_MERCHANT_XX * @throws YDWXException * @return YDWXCardSubmerchantBatchGetResponse */ function ydwx_card_submerchant_batchget($accessToken, $begin_id, $limit, $status) { if ($limit > 100) { throw new YDWXException("商户批量查询数量最大100"); } $http = new YDHttp(); $info = $http->get(YDWX_WEIXIN_BASE_URL2 . "card/submerchant/batchget?access_token={$accessToken}", ydwx_json_encode(array("begin_id" => intval($begin_id), "limit" => intval($limit), "status" => $status))); $msg = new YDWXCardSubmerchantBatchGetResponse($info); if ($msg->isSuccess()) { return $msg; } throw new YDWXException($msg->errmsg, $msg->errcode); }
/** * 批量查询页面统计数据接口 * 接口说明 查询指定时间商家帐号下的每个页面进行摇周边操作的人数、次数,点击摇周边消息的人数、次数。 * 只能查询最近90天内的数据,且一次只能查询一天。 * 此接口无法获取当天的数据,最早只能获取前一天的数据。由于系统在凌晨处理前一天的数据,太早调用此接口可能获取不到数据,建议在早上8:00之后调用此接口。 * 注意:对于摇周边人数、摇周边次数、点击摇周边消息的人数、点击摇周边消息的次数都为0的页面,不在结果列表中返回。 * @param unknown $accessToken * @param unknown $date * @param unknown $page * @throws YDWXException * @return YDWXZBPageStatisticResult */ function ydwx_shakearound_statistics_pagelist($accessToken, $date, $page) { $http = new YDHttp(); $array = array(); $array['page_index'] = $page; $array['date'] = $date; $info = $http->post(YDWX_WEIXIN_BASE_URL2 . "shakearound/statistics/pagelist?access_token={$accessToken}", ydwx_json_encode($array)); $msg = new YDWXZBPageStatisticResult($info); if ($msg->isSuccess()) { return $msg; } throw new YDWXException($msg->errmsg); }
//公众号自己的授权 $isAgent = false; $appid = YDWX_WEIXIN_ACCOUNT_TYPE == YDWX_WEIXIN_ACCOUNT_TYPE_CROP ? YDWX_WEIXIN_CROP_ID : YDWX_WEIXIN_APP_ID; $secret = YDWX_WEIXIN_ACCOUNT_TYPE == YDWX_WEIXIN_ACCOUNT_TYPE_CROP ? YDWX_WEIXIN_CROP_SECRET : YDWX_WEIXIN_APP_SECRET; $authorize_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . urlencode($redirect) . "&response_type=code&scope=snsapi_base&state={$state}#wechat_redirect"; $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $secret . "&code=%s&grant_type=authorization_code"; } //第一步,引导用户到微信页面授权 if (!@$_GET['code'] && !@$_GET['state']) { ob_clean(); header($authorize_url); die; } //第二步,用户授权后返回,获取授权用户信息 if ($isAgent || YDWX_WEIXIN_ACCOUNT_TYPE != YDWX_WEIXIN_ACCOUNT_TYPE_CROP) { $http = new YDHttp(); $info = json_decode($http->get(sprintf($access_token_url, $_GET['code'])), true); if (!@$info['openid']) { YDWXHook::do_hook(YDWXHook::AUTH_FAIL, YDWXAuthFailResponse::errMsg($info['errmsg'], $info['errcode'])); die; } if ($isAgent) { $access_token = YDWXHook::do_hook(YDWXHook::GET_HOST_ACCESS_TOKEN, $appid); } else { $access_token = YDWXHook::do_hook(YDWXHook::GET_ACCESS_TOKEN); } if ($access_token) { try { $user = ydwx_user_info($access_token, $info['openid']); } catch (\Exception $e) { $user = new YDWXSubscribeUser();
/** * 该 API 用于第三方平台确认接受公众号将某权限集高级权限的授权 * @param unknown $appid * @param unknown $funcscope_category_id 功能集合,见YDWX_FUNC_XX常量 * @param boolean $confirm true 确认 false取消 * @throws YDWXException * @return boolean */ function ydwx_agent_confirm_authorization($appid, $funcscope_category_id, $confirm) { $accessToken = YDWXHook::do_hook(YDWXHook::GET_AGENT_ACCESS_TOKEN); $http = new YDHttp(); $info = $http->post(YDWX_WEIXIN_BASE_URL . "component/api_confirm_authorization?component_access_token={$accessToken}", ydwx_json_encode(array("component_appid" => YDWX_WEIXIN_COMPONENT_APP_ID, "authorizer_appid" => $appid, "funcscope_category_id" => $funcscope_category_id, "confirm_value" => $confirm ? 1 : 2))); $msg = new YDWXResponse($info); if ($msg->isSuccess()) { return true; } throw new YDWXException($msg->errmsg); }
/** * 取得poi类目 * @param unknown $accessToken * @throws YDWXException * @return array */ function ydwx_poi_getwxcategory($accessToken) { $http = new YDHttp(); $info = $http->get(YDWX_WEIXIN_BASE_URL . "api_getwxcategory?access_token={$accessToken}"); $msg = new YDWXResponse($info); if ($msg->isSuccess()) { return $msg->category_list; } throw new YDWXException($msg->errmsg); }
/** * 母商户可以通过该接口批量拉取子商户的相关信息,一次调用最多拉取 100 个子商户 * 的信息,可以通过多次拉去满足不同的查询需求。 * * @param unknown $accessTokent 第三方平台的access token * @throws YDWXException * @return YDWXCardMPMerchantBatchGetResponse */ function ydwx_card_batchget_mpmerchant($accessToken, $next_get) { $http = new YDHttp(); $info = $http->post(YDWX_WEIXIN_BASE_URL . "component/batchget_card_merchant?access_token={$accessToken}", ydwx_json_encode(array("next_get" => $next_get))); $msg = new YDWXCardMPMerchantBatchGetResponse($info); if ($msg->isSuccess()) { return $msg; } throw new YDWXException($msg->errmsg, $msg->errcode); }
/** * 下载临时文件 * * @param unknown $accessToken * @param unknown $mediaid * @param string $isVideo * @return boolean|content */ function ydwx_media_get($accessToken, $mediaid, $isVideo = false) { $http = new YDHttp(); $content = $http->get(($isVideo ? YDWX_WEIXIN_NO_SSL_URL : YDWX_WEIXIN_BASE_URL) . "media/get?access_token={$accessToken}&media_id={$mediaid}"); $info = json_decode($content, true); if (array_key_exists("errcode", $info)) { return false; } return $content; }