Wechat portal binding , Wechat event handling , WeChat API All operations are contained in these files .
The content includes : Wechat shake the interface / Wechat multi customer service interface / Wechat payment interface / Wechat red packet interface / Wechat card and coupon interface / Wechat store interface /JSAPI

<?php
class WxApi {
const appId = "";
const appSecret = "";
const mchid = ""; // Merchant number
const privatekey = ""; // Private key
public $parameters = array();
public $jsApiTicket = NULL;
public $jsApiTime = NULL; public function __construct(){ } /****************************************************
* Wechat submission API Method , Return to wechat to specify JSON
****************************************************/ public function wxHttpsRequest($url,$data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, );
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, );
$output = curl_exec($curl);
curl_close($curl);
return $output;
} /****************************************************
* Submit data with certificate on wechat - Use of wechat red envelope
****************************************************/ public function wxHttpsRequestPem($url, $vars, $second=,$aHeader=array()){
$ch = curl_init();
// Timeout time
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, );
// Set up an agent here , If any
//curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); // Choose one of the following two ways // The first method ,cert And key They belong to two .pem file
// The default format is PEM, Can comment
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/apiclient_cert.pem');
// The default format is PEM, Can comment
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/apiclient_key.pem'); curl_setopt($ch,CURLOPT_CAINFO,'PEM');
curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/rootca.pem'); // The second way , Two files make one .pem file
//curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem'); if( count($aHeader) >= ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
} curl_setopt($ch,CURLOPT_POST, );
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
} /****************************************************
* Wechat access to AccessToken Return to the designated WeChat official account. at Information
****************************************************/ public function wxAccessToken($appId = NULL , $appSecret = NULL){
$appId = is_null($appId) ? self::appId : $appId;
$appSecret = is_null($appSecret) ? self::appSecret : $appSecret; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
$result = $this->wxHttpsRequest($url);
//print_r($result);
$jsoninfo = json_decode($result, true);
$access_token = $jsoninfo["access_token"]; return $access_token;
} /****************************************************
* Wechat access to ApiTicket Return to the designated WeChat official account. at Information
****************************************************/ public function wxJsApiTicket($appId = NULL , $appSecret = NULL){
$appId = is_null($appId) ? self::appId : $appId;
$appSecret = is_null($appSecret) ? self::appSecret : $appSecret; $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$this->wxAccessToken();
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
$ticket = $jsoninfo['ticket'];
//echo $ticket . "<br />";
return $ticket;
} public function wxVerifyJsApiTicket($appId = NULL , $appSecret = NULL){
if(!empty($this->jsApiTime) && intval($this->jsApiTime) > time() && !empty($this->jsApiTicket)){
$ticket = $this->jsApiTicket;
}
else{
$ticket = $this->wxJsApiTicket($appId,$appSecret);
$this->jsApiTicket = $ticket;
$this->jsApiTime = time() + ;
}
return $ticket;
} /****************************************************
* Wechat through OPENID Get user information , Returns an array of
****************************************************/ public function wxGetUser($openId){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$wxAccessToken."&openid=".$openId."&lang=zh_CN";
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat generates two-dimensional code ticket
****************************************************/ public function wxQrCodeTicket($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
return $result;
} /****************************************************
* Wechat through ticket Generate qr code
****************************************************/
public function wxQrCode($ticket){
$url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode($ticket);
return $url;
} /****************************************************
* Wechat sends the specified template information to the specified user , After sending, return to the specified JSON data
****************************************************/ public function wxSendTemplate($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
return $result;
} /****************************************************
* Send custom template messages
****************************************************/ public function wxSetSend($touser, $template_id, $url, $data, $topcolor = '#7B68EE'){
$template = array(
'touser' => $touser,
'template_id' => $template_id,
'url' => $url,
'topcolor' => $topcolor,
'data' => $data
);
$jsonData = urldecode(json_encode($template));
echo $jsonData;
$result = $this->wxSendTemplate($jsonData);
return $result;
} /****************************************************
* Wechat settings OAUTH Jump URL, Returns string information - SCOPE = snsapi_base // Verification does not return to the confirmation page , Can only get OPENID
****************************************************/ public function wxOauthBase($redirectUrl,$state = "",$appId = NULL){
$appId = is_null($appId) ? self::appId : $appId;
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_base&state=".$state."#wechat_redirect";
return $url;
} /****************************************************
* Wechat settings OAUTH Jump URL, Returns string information - SCOPE = snsapi_userinfo // Get the user's complete information
****************************************************/ public function wxOauthUserinfo($redirectUrl,$state = "",$appId = NULL){
$appId = is_null($appId) ? self::appId : $appId;
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect";
return $url;
} /****************************************************
* WeChat OAUTH Jump to specify URL
****************************************************/ public function wxHeader($url){
header("location:".$url);
} /****************************************************
* Wechat through OAUTH Return to the page for AT Information
****************************************************/ public function wxOauthAccessToken($code,$appId = NULL , $appSecret = NULL){
$appId = is_null($appId) ? self::appId : $appId;
$appSecret = is_null($appSecret) ? self::appSecret : $appSecret;
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code";
$result = $this->wxHttpsRequest($url);
//print_r($result);
$jsoninfo = json_decode($result, true);
//$access_token = $jsoninfo["access_token"];
return $jsoninfo;
} /****************************************************
* Wechat through OAUTH Of Access_Token Get the current user information // Only in snsapi_userinfo mode
****************************************************/ public function wxOauthUser($OauthAT,$openId){
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$OauthAT."&openid=".$openId."&lang=zh_CN";
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Create a custom menu
****************************************************/ public function wxMenuCreate($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Get custom menu
****************************************************/ public function wxMenuGet(){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Delete custom menu
****************************************************/ public function wxMenuDelete(){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Get third party custom menu
****************************************************/ public function wxMenuGetInfo(){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat customer service interface - Add Add customer service staff
****************************************************/ public function wxServiceAdd($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat customer service interface - Update Edit customer service staff
****************************************************/ public function wxServiceUpdate($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat customer service interface - Delete Delete customer service personnel
****************************************************/ public function wxServiceDelete($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*******************************************************
* Wechat customer service interface - Upload your avatar
*******************************************************/
public function wxServiceUpdateCover($kf_account, $media = '') {
$wxAccessToken = $this->wxAccessToken();
//$data['access_token'] = $wxAccessToken;
$data['media'] = '@D:\\workspace\\htdocs\\yky_test\\logo.jpg';
$url = "https:// api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=".$wxAccessToken."&kf_account=".$kf_account;
$result = $this->wxHttpsRequest($url,$data);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat customer service interface - Get customer service list
****************************************************/ public function wxServiceList(){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat customer service interface - Get online customer service information
****************************************************/ public function wxServiceOnlineList(){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat customer service interface - Customer service sends information
****************************************************/ public function wxServiceSend($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat customer service session interface - Create a session
****************************************************/ public function wxServiceSessionAdd($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/customservice/kfsession/create?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat customer service session interface - Close session
****************************************************/ public function wxServiceSessionClose(){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/customservice/kfsession/close?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat customer service session interface - Get session
****************************************************/ public function wxServiceSessionGet($openId){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/customservice/kfsession/getsession?access_token=".$wxAccessToken."&openid=" . $openId;
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat customer service session interface - Get the conversation list
****************************************************/ public function wxServiceSessionList($kf_account){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/customservice/kfsession/getsessionlist?access_token=".$wxAccessToken."&kf_account=" . $kf_account ;
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat customer service session interface - Session not accessed
****************************************************/ public function wxServiceSessionWaitCase(){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/customservice/kfsession/getwaitcase?access_token=".$wxAccessToken;
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat shakes - Application for equipment ID
****************************************************/ public function wxDeviceApply($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/shakearound/device/applyid?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat shakes - Editing devices ID
****************************************************/ public function wxDeviceUpdate($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/shakearound/device/update?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat shakes - Related equipment of our store
****************************************************/ public function wxDeviceBindLocation($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/shakearound/device/bindlocation?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat shakes - Query device list
****************************************************/ public function wxDeviceSearch($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/shakearound/device/search?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat shakes - New page
****************************************************/ public function wxPageAdd($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/shakearound/page/add?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat shakes - Edit page
****************************************************/ public function wxPageUpdate($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/shakearound/page/update?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat shakes - Query page
****************************************************/ public function wxPageSearch($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/shakearound/page/search?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat shakes - Delete page
****************************************************/ public function wxPageDelete($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/shakearound/page/delete?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*******************************************************
* Wechat shakes - Upload image material
*******************************************************/
public function wxMaterialAdd($media = '') {
$wxAccessToken = $this->wxAccessToken();
//$data['access_token'] = $wxAccessToken;
$data['media'] = '@D:\\workspace\\htdocs\\yky_test\\logo.jpg';
$url = "https://api.weixin.qq.com/shakearound/material/add?access_token=".$wxAccessToken;
$result = $this->wxHttpsRequest($url,$data);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat shakes - Configure the relationship between the device and the page
****************************************************/ public function wxDeviceBindPage($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/shakearound/device/bindpage?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat shakes - Access to the surrounding equipment and user information
****************************************************/ public function wxGetShakeInfo($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/shakearound/user/getshakeinfo?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /****************************************************
* Wechat shakes - Data statistics interface based on device dimension
****************************************************/ public function wxGetShakeStatistics($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/shakearound/statistics/device?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*****************************************************
* Generate random string - The longest is 32 A string
*****************************************************/
public function wxNonceStr($length = , $type = FALSE) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = ; $i < $length; $i++) {
$str .= substr($chars, mt_rand(, strlen($chars) - ), );
}
if($type == TRUE){
return strtoupper(md5(time() . $str));
}
else {
return $str;
}
} /*******************************************************
* Wechat merchant order number - The longest 28 A string
*******************************************************/ public function wxMchBillno($mchid = NULL) {
if(is_null($mchid)){
if(self::mchid == "" || is_null(self::mchid)){
$mchid = time();
}
else{
$mchid = self::mchid;
}
}
else{
$mchid = substr(addslashes($mchid),,);
}
return date("Ymd",time()).time().$mchid;
} /*******************************************************
* Wechat format array becomes parameter format - Support url encryption
*******************************************************/ public function wxSetParam($parameters){
if(is_array($parameters) && !empty($parameters)){
$this->parameters = $parameters;
return $this->parameters;
}
else{
return array();
}
} /*******************************************************
* Wechat format array becomes parameter format - Support url encryption
*******************************************************/ public function wxFormatArray($parameters = NULL, $urlencode = FALSE){
if(is_null($parameters)){
$parameters = $this->parameters;
}
$restr = "";// Initialization null
ksort($parameters);// Sort parameters
foreach ($parameters as $k => $v){// Loop customization parameters
if (null != $v && "null" != $v && "sign" != $k) {
if($urlencode){// If the parameter needs to be increased URL Encryption increases , No need, no need
$v = urlencode($v);
}
$restr .= $k . "=" . $v . "&";// Returns the complete string
}
}
if (strlen($restr) > ) {// If there is data, it will finally “&” Delete
$restr = substr($restr, , strlen($restr)-);
}
return $restr;// Return string
} /*******************************************************
* WeChat MD5 Signature generator - You need to convert the parameter array to a string [wxFormatArray Method ]
*******************************************************/
public function wxMd5Sign($content, $privatekey){
try {
if (is_null($privatekey)) {
throw new Exception(" TenPay signature key Can't be empty !");
}
if (is_null($content)) {
throw new Exception(" TenPay signature content cannot be empty ");
}
$signStr = $content . "&key=" . $privatekey;
return strtoupper(md5($signStr));
}
catch (Exception $e)
{
die($e->getMessage());
}
} /*******************************************************
* WeChat Sha1 Signature generator - You need to convert the parameter array to a string [wxFormatArray Method ]
*******************************************************/
public function wxSha1Sign($content){
try {
if (is_null($content)) {
throw new Exception(" Signature content cannot be empty ");
}
//$signStr = $content;
return sha1($content);
}
catch (Exception $e)
{
die($e->getMessage());
}
} /*******************************************************
* WeChat jsApi Integration approach - Get by calling this method jsapi data
*******************************************************/
public function wxJsapiPackage(){
$jsapi_ticket = $this->wxVerifyJsApiTicket(); // Be careful URL Be sure to get it dynamically , You can't hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == ) ? "https://" : "http://";
$url = $protocol.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]; $timestamp = time();
$nonceStr = $this->wxNonceStr(); $signPackage = array(
"jsapi_ticket" => $jsapi_ticket,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url
); // Here, the order of the parameters should be in the following order key value ASCII Code ascending sort
$rawString = "jsapi_ticket=$jsapi_ticket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; //$rawString = $this->wxFormatArray($signPackage);
$signature = $this->wxSha1Sign($rawString); $signPackage['signature'] = $signature;
$signPackage['rawString'] = $rawString;
$signPackage['appId'] = self::appId; return $signPackage;
} /*******************************************************
* WeChat card vouchers :JSAPI Card voucher Package - The base parameter does not come with any value - In the reproduction environment, it needs to be modified according to the actual situation
*******************************************************/
public function wxCardPackage($cardId , $timestamp = ''){
$api_ticket = $this->wxVerifyJsApiTicket();
if(!empty($timestamp)){
$timestamp = $timestamp;
}
else{
$timestamp = time();
} $arrays = array(self::appSecret,$timestamp,$cardId);
sort($arrays , SORT_STRING);
//print_r($arrays);
//echo implode("",$arrays)."<br />";
$string = sha1(implode($arrays));
//echo $string;
$resultArray['cardId'] = $cardId;
$resultArray['cardExt'] = array();
$resultArray['cardExt']['code'] = '';
$resultArray['cardExt']['openid'] = '';
$resultArray['cardExt']['timestamp'] = $timestamp;
$resultArray['cardExt']['signature'] = $string;
//print_r($resultArray);
return $resultArray;
} /*******************************************************
* WeChat card vouchers :JSAPI All cards and coupons Package
*******************************************************/
public function wxCardAllPackage($cardIdArray = array(),$timestamp = ''){
$reArrays = array();
if(!empty($cardIdArray) && (is_array($cardIdArray) || is_object($cardIdArray))){
//print_r($cardIdArray);
foreach($cardIdArray as $value){
//print_r($this->wxCardPackage($value,$openid));
$reArrays[] = $this->wxCardPackage($value,$timestamp);
}
//print_r($reArrays);
}
else{
$reArrays[] = $this->wxCardPackage($cardIdArray,$timestamp);
}
return strval(json_encode($reArrays));
} /*******************************************************
* WeChat card vouchers : Get the card coupon list
*******************************************************/
public function wxCardListPackage($cardType = "" , $cardId = ""){
//$api_ticket = $this->wxVerifyJsApiTicket();
$resultArray = array();
$timestamp = time();
$nonceStr = $this->wxNonceStr();
//$strings =
$arrays = array(self::appId,self::appSecret,$timestamp,$nonceStr);
sort($arrays , SORT_STRING);
$string = sha1(implode($arrays)); $resultArray['app_id'] = self::appId;
$resultArray['card_sign'] = $string;
$resultArray['time_stamp'] = $timestamp;
$resultArray['nonce_str'] = $nonceStr;
$resultArray['card_type'] = $cardType;
$resultArray['card_id'] = $cardId;
return $resultArray;
} /*******************************************************
* Parse the array XML - Wechat red packet interface
*******************************************************/
public function wxArrayToXml($parameters = NULL){
if(is_null($parameters)){
$parameters = $this->parameters;
} if(!is_array($parameters) || empty($parameters)){
die(" The parameter is not an array and cannot be resolved ");
} $xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val))
{
$xml.="<".$key.">".$val."</".$key.">";
}
else
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
$xml.="</xml>";
return $xml;
} /*******************************************************
* WeChat card vouchers : Upload LOGO - Need to rewrite dynamic function
*******************************************************/
public function wxCardUpdateImg() {
$wxAccessToken = $this->wxAccessToken();
//$data['access_token'] = $wxAccessToken;
$data['buffer'] = '@D:\\workspace\\htdocs\\yky_test\\logo.jpg';
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$wxAccessToken;
$result = $this->wxHttpsRequest($url,$data);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
//array(1) { ["url"]=> string(121) "http://mmbiz.qpic.cn/mmbiz/ibuYxPHqeXePNTW4ATKyias1Cf3zTKiars9PFPzF1k5icvXD7xW0kXUAxHDzkEPd9micCMCN0dcTJfW6Tnm93MiaAfRQ/0" }
} /*******************************************************
* WeChat card vouchers : Get the color
*******************************************************/
public function wxCardColor(){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/card/getcolors?access_token=".$wxAccessToken;
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*******************************************************
* WeChat card vouchers : Pull store list
*******************************************************/
public function wxBatchGet($offset = , $count = ){
$jsonData = json_encode(array('offset' => intval($offset) , 'count' => intval($count)));
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/card/location/batchget?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*******************************************************
* WeChat card vouchers : Create card ticket
*******************************************************/
public function wxCardCreated($jsonData) {
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/card/create?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*******************************************************
* WeChat card vouchers : Check card and coupon details
*******************************************************/
public function wxCardGetInfo($jsonData) {
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/card/get?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*******************************************************
* WeChat card vouchers : Set the whitelist
*******************************************************/
public function wxCardWhiteList($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/card/testwhitelist/set?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*******************************************************
* WeChat card vouchers : Consumption card coupon
*******************************************************/
public function wxCardConsume($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/card/code/consume?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*******************************************************
* WeChat card vouchers : Delete card ticket
*******************************************************/
public function wxCardDelete($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/card/delete?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*******************************************************
* WeChat card vouchers : Choose a card or coupon - analysis CODE
*******************************************************/
public function wxCardDecryptCode($jsonData){
$wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/card/code/decrypt?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*******************************************************
* WeChat card vouchers : Change inventory
*******************************************************/
public function wxCardModifyStock($cardId , $increase_stock_value = , $reduce_stock_value = ){
if(intval($increase_stock_value) == && intval($reduce_stock_value) == ){
return false;
} $jsonData = json_encode(array("card_id" => $cardId , 'increase_stock_value' => intval($increase_stock_value) , 'reduce_stock_value' => intval($reduce_stock_value))); $wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/card/modifystock?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
} /*******************************************************
* WeChat card vouchers : Query the user CODE
*******************************************************/
public function wxCardQueryCode($code , $cardId = ''){ $jsonData = json_encode(array("code" => $code , 'card_id' => $cardId )); $wxAccessToken = $this->wxAccessToken();
$url = "https://api.weixin.qq.com/card/code/get?access_token=" . $wxAccessToken;
$result = $this->wxHttpsRequest($url,$jsonData);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
}
}

( Practical )php Official wechat interface ( Wechat payment 、 WeChat Lucky Money 、 Wechat shakes 、 Wechat shop ) More articles about

  1. Wechat interface appears “ Call payment jsapi Lack of parameter appid”

    Wechat interface appears “ Call payment jsapi Lack of parameter appid” Be careful :@Html.Raw(ViewBag.wxJsApiParam),//json strand ViewBag.wxJsApiParam It's a backstage one j ...

  2. php Official wechat interface

    Wechat portal binding , Wechat event handling , WeChat API All operations are contained in these files . The content includes : Wechat shake the interface / Wechat multi customer service interface / Wechat payment interface / Wechat red packet interface / Wechat card and coupon interface / Wechat store interface /JSAPI <?php class ...

  3. C# Develop wechat portal and Application (1)-- Start using wechat interface

    Wechat application in full swing , Many companies want to catch the information express , This is a business opportunity , It is also a direction of technology , therefore , If you have time to study . Learn the related development of wechat , It becomes one of the most important things in daily planning . This series of articles hopes to take a step-by-step perspective , Comprehensive introduction ...

  4. Wechat interface requests universal functions http_request

    keyword :http_request http_request post get http request original text : http://www.cnblogs.com/txw1958/p/http_request ...

  5. turn C# Develop wechat portal and Application (1)-- Start using wechat interface

    Wechat application in full swing , Many companies want to catch the information express , This is a business opportunity , It is also a direction of technology , therefore , If you have time to study . Learn the related development of wechat , It becomes one of the most important things in daily planning . This series of articles hopes to take a step-by-step perspective , Comprehensive introduction ...

  6. C# Calling wechat interface code

    What needs to be prepared before calling wechat interface . 1. Development of wechat public platform appid 2. Development of wechat public platform secret 3.. obtain tokenid 4. obtain ticket 5. Generate a random string of signatures 6. Generate signature timestamp 7. Generate signature ...

  7. C# About calling wechat interface code

    What needs to be prepared before calling wechat interface . 1. Development of wechat public platform appid 2. Development of wechat public platform secret 3.. obtain tokenid 4. obtain ticket 5. Generate a random string of signatures 6. Generate signature timestamp 7. Generate signature ...

  8. ( turn )C# Develop wechat portal and Application (1)-- Start using wechat interface

    http://www.cnblogs.com/wuhuacong/p/3613826.html Wechat application in full swing , Many companies want to catch the information express , This is a business opportunity , It is also a direction of technology , therefore , If you have time to study . Study ...

  9. ( Practical ) Wechat payment scanning payment php edition

    This article is an example for you to share php Wechat code scanning payment source code , For your reference , The details are as follows The code contains four files createUrl.php.ArrayToXML.php.returnGoodsUrl.php.notifyUr ...

Random recommendation

  1. Create a second vlan network &quot;vlan101&quot; - Every day 5 Minutes to play OpenStack(96)

    We created vlan100, And deployed instance, Today we will continue to create a second vlan network "vlan101". subnet IP The address is 172.16.101. ...

  2. Empty form All the information in the form input value -------------jquery

    $(':input','#' + formid).not(':button, :submit, :reset').val('').removeAttr('checked').removeAttr('s ...

  3. PowerDesigner Derived sql Remove double quotes from , It is mainly used for Oracle

    As the title , These double quotes Oracle It's terrible to build a watch : resolvent : ( Reprint , The author's Baidu has hung up , Unable to put link , Be selfish , Former Oracle The project is gone , Can't show ) 1. Get rid of Oracle Generated SQL Create statement ...

  4. Generated by other people's websites json

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sb.ToString()); WebResponse rep = req.GetResp ...

  5. ASP Connect 11 A common syntax of database

    1.Access Database DSN-less Connection method : Here's the quote : set adocon=Server.Createobject("adodb.connection") ado ...

  6. 【C Crazy textbook 】( Nine )C Language pointer ( One )

    1. What is an address ? Memory is made up of several components 1 A memory consisting of four bytes of memory cells In order to manage the memory unit . Number every byte The number is the address 2. What is a pointer ? The address is the pointer Address : The memory unit value that the address points to : Number corresponding ...

  7. Protect DNS The server 3 The big way

    Protect DNS The server 3 The big way        DNS Full name DomainNameSystem Domain name resolution system , In layman's terms ,DNS Is to help users in Internet Look for names and IP The corresponding parsing service . To make it easier to use network resources ,DN ...

  8. Vue Series of =&gt; webpack Of url loader

    install : npm i url-loader file-loader -D  //url-loader Internal dependence file-loader webpack.config.js const path = requ ...

  9. docker-machine on azure

    1. Get ready Azure Virtual machine , install docker-machine because azure The administrator account of the virtual machine is not root, So here we use our own Administrator yy 1.base=https://github.com/d ...

  10. js Page value of cookie.session

    jquery Of cookie: Read all cookie: document.cookie: Read a single cookie:$.cookie('cookiename'); newly added cookie: $.cookie(' ...