app_id.'&redirect_uri='.$turl.'&&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'); exit(); } else { //获取code码,以获取openid $code = $_GET['code']; $access_info = $this->get_access_token($code); return $access_info; } } /** * 获取授权token网页授权 * * @param string $code 通过get_authorize_url获取到的code */ public function get_access_token($code = '') { $appid = $this->app_id; $appsecret = $this->app_secret; $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $code . "&grant_type=authorization_code"; //echo $token_url; $token_data = $this->http($token_url); // var_dump( $token_data); if ($token_data[0] == 200) { $ar = json_decode($token_data[1], TRUE); return $ar; } return $token_data[1]; } public function getAcc($code = ''){ $appid = $this->app_id; $appsecret = $this->app_secret; $token_url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appsecret."&js_code=".$code."&grant_type=authorization_code"; $token_data = $this->http($token_url); if ($token_data[0] == 200) { $ar = json_decode($token_data[1], TRUE); return $ar; } return $token_data[1]; } public function getToken(){ $appid = $this->app_id; $appsecret = $this->app_secret; $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; $token_data = $this->http($token_url); var_dump($token_data); if ($token_data[0] == 200) { $ar = json_decode($token_data[1], TRUE); $this->send($ar['access_token']); } return $token_data[1]; } public function send($access_token){ $token_url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$access_token; $data = [ 'template_id' => 'kKgDn4gfOLtH9KFbqQcCd4VZN03FOoUYoCKciIhgvks', // 所需下发的订阅模板id 'touser' => 'ouOMm41xnIKqoZ66ygwJa-qJQpgw', // 接收者(用户)的 openid 'kKgDn4gfOLtH9KFbqQcCd4VZN03FOoUYoCKciIhgvks'=>'developer', 'page' => '', // 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。 'data' => [ // 模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } } 'amount1' => [ 'value' => '10.00', ], 'character_string2' => [ 'value' => 11111, ], 'date3' => [ 'value' => "2019-10-17 11:19:30", ], 'thing4' => [ 'value' => "测试详情", ], 'thing7' => [ 'value' => "姓名-手机号码-地址", ], ], ]; $data = json_encode($data); $token_data = $this->http($token_url,'POST',$data); var_dump($token_data); } public function http($url, $method = '', $postfields = null, $headers = array(), $debug = false) { $ci = curl_init(); /* Curl settings */ curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); switch ($method) { case 'POST': curl_setopt($ci, CURLOPT_POST, true); if (!empty($postfields)) { curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); $this->postdata = $postfields; } break; } // 本地调试 curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ci, CURLOPT_URL, $url); curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLINFO_HEADER_OUT, true); // 本地调试 curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); $response = curl_exec($ci); $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); if ($debug) { echo "=====post data======\r\n"; var_dump($postfields); echo '=====info=====' . "\r\n"; print_r(curl_getinfo($ci)); echo '=====$response=====' . "\r\n"; print_r($response); } curl_close($ci); return array($http_code, $response); } }