|
- <?php
- //调用
- require_once '../Common/Config.php';
- require_once '../Common/Function.php';
- if (!isset($_GET['code'])){
- a:
- $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
- $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".$baseUrl."&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
- Header("Location: $url");
- exit();
- }else {
- $code=$_GET['code'];
- $openIdUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$code."&grant_type=authorization_code";
- $openIdResult=http_request($openIdUrl);
- if ($openIdResult){
- $userInfo=json_decode($openIdResult,true);
- if ($userInfo['errcode'] ==40029){
- goto a;
- }
-
- $dir="../Log";
- if (!is_dir($dir)){
- mkdir($dir);
- }
- $token_file=$dir.'/access_token';
- if (file_exists($token_file) && time()-filemtime($token_file)<4800){
- write_log("getInfo--文件中读取的token:".file_get_contents($token_file));
- $access_token= file_get_contents($token_file);
- }else{
- $accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
- $result=http_request($accessTokenUrl);
- echo $accessTokenUrl;
- echo $result;
- var_dump(json_decode($result,true));
- if(!$result){
- write_log("getInfo-获取token出错");
- echo "getInfo-获取token出错";
- exit;
- }
-
- $result_obj=json_decode($result);
- file_put_contents($token_file, $result_obj->access_token);
- write_log("getInfo-url获取的token:".$result_obj->access_token);
- $access_token= $result_obj->access_token;
- }
-
- $getUserInfoUrl="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$userInfo['openid']."&lang=zh_CN";
- $getUsesrInfo=http_request($getUserInfoUrl);
- print_r(json_decode($getUsesrInfo,true));
- }
-
- }
- exit;
- /* $sql="INSERT INTO qrcode(media_id,url,createtime) values('".$media_id."','".$Rurl."','".date("Y-m-d H:i:s")."')";
- $pdo = new PDO("sqlsrv:Server=".HOST.";Database=".DB, USER, PASSWORD);//5.6版本pdo连接sqlsrv
- $result=$pdo->exec($sql);
- $id= $pdo->lastInsertId(); */
-
-
- $getOpenidListUrl="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token;
- $getOpenidList=http_request($getOpenidListUrl);
- echo $getOpenidList;
- print_r(json_decode($getOpenidList,true));exit;
- if ($getOpenidList){
- write_log($getOpenidList);
- $openidList_obj=json_decode($getOpenidList);
- $ticket=$openidList_obj->ticket;
- write_log("qrcode.php-- $ticketUrl url获取ticket:".$ticket);
- }
- $qrcodeUrl="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
- write_log("$qrcodeUrl".$qrcodeUrl);
- $imageInfo=downloadImageFromWeiXin($qrcodeUrl);
- $dir="./Images";
- if (!is_dir($dir)){
- mkdir($dir);
- }
- $filename=$dir."/qrcode"."-".$id."-".time().".jpg";
- $local_file=fopen($filename, 'w');
- if ($local_file !==false){
- if(fwrite($local_file, $imageInfo["body"]) !== false){
- fclose($local_file);
- /* $filePath=dirname($_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'])."/".$filename;
- downfile($filePath); */
- }
- }
- header("Location: $qrcodeUrl");
- exit;
|