マイクロ文字メッセージのアラートを送信するために、パブリックユーザーの数にPHP開発

  最近メンバーに入金ユーザの変更や資金が彼にマイクロ手紙を促すメッセージを送信する必要があるプロジェクトでは、なかったです。このために、テンプレートを使用したいメッセージの先頭が、アプリケーションの数がちょうど公共のメッセージテンプレートを適用するには数日かかる登録し、十分な時間がない場合には、顧客サービスのメッセージインタフェースを使用することを選択した、マイクロチャネル文書住所:https://mp.weixin.qq.com /ウィキ。

    ここでは、ページが、主に、access_tokenは取得した顧客サービスのニュースリリースを見るために、ユーザー認証や情報へのアクセス、ステップ・インターフェース要求をスキップすることを確認し世間の注目と他のインターフェイスの数。

 

1. access_tokenはを取得します。

// 获取access_token
public function getAccessToken($weid) { $appID = "wxfaddfdfdfd6cf6fc3569"; // 服务号appID $appSecret = "071bebfdfdofdfd23687bf53d63a"; // 服务号appSerect $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appID&secret=$appSecret"; $content = ihttp_get($url); // 自定义请求函数 if(is_error($content)) { return error('-1', '获取微信公众号授权失败, 请稍后重试!错误详情: ' . $content['message']); } if (empty($content['content'])) { return error('-1', 'AccessToken获取失败,请检查appid和appsecret的值是否与微信公众平台一致!'); } $token = @json_decode($content['content'], true); if ($token['errcode'] == '40164') { return error(-1, $this->errorCode($token['errcode'], $token['errmsg'])); } if(empty($token) || !is_array($token) || empty($token['access_token']) || empty($token['expires_in'])) { $errorinfo = substr($content['meta'], strpos($content['meta'], '{')); $errorinfo = @json_decode($errorinfo, true); return error('-1', '获取微信公众号授权失败, 请稍后重试! 公众平台返回原始数据为: 错误代码-' . $errorinfo['errcode'] . ',错误信息-' . $errorinfo['errmsg']); } $record = array(); $record['token'] = $token['access_token']; $record['expire'] = TIMESTAMP + $token['expires_in'] - 200; $cachekey = cache_system_key('accesstoken', array('acid' => $weid)); cache_write($cachekey, $record); return $record['token']; }

 

2.世間の注目の数かどうかを判断します。

// 判断当前用户是否关注公众号
public public function isSubscribe($weid,$userid) { // 获取当前用户信息 $userinfo = pdo_get('hcface_users',array('uid'=>$userid)); //return $userinfo; if(empty($userinfo)) { return false; } // 获取access_token $accessToken = $this->getAccessToken($weid); // 是否关注接口 $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$accessToken."&openid=".$userinfo['openid']."&lang=zh_CN"; $res = ihttp_request($url); if(is_error($res)) { return false; } if($res['code'] != '200') { return false; } $result = @json_decode($res['content'],true); if($result['subscribe'] == 1) { $updateData = []; // 判断当前用户头像和昵称是否更换 if($userinfo['avatar'] != $result['headimgurl']) { $updateData['avatar'] = $result['headimgurl']; } if($userinfo['nickname'] != $result['nickname']) { $updateData['avatar'] = $result['nickname']; } if(!empty($updateData)) { pdo_update('hcface_users',$updateData,array('uid'=>$userid)); } } $userInfoData = [ "subscribe" => $result['subscribe'], "user_openid" => $userinfo['openid'], "nickname" => $userinfo['nickname'], ]; return $userInfoData; }

 

3.メッセージサービスを送信します。

public function solPushMsg($openid, $content, $wid) { // 获取access_token $accessToken = $this->getAccessToken($wid); $data = array( 'touser' => $openid, // 用户openID 'msgtype' => 'text', 'text' => [ 'content' => $content, // 内容 ], ); $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$accessToken; $res = ihttp_request($url,json_encode($data,JSON_UNESCAPED_UNICODE)); // json_encode第二个参数必须带上,不然发出的消息可能是unicode编码的 if(is_error($res)) { return false; } if($res['code'] != '200') { return false; } return @json_decode($res['content'],true); }

 

4.インターフェースのマイクロチャネルアレイを返します。

 

効果が実現しました。

 

いいえ公共ありません

おすすめ

転載: www.cnblogs.com/zerofc/p/11121619.html