選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

88 行
3.1 KiB

  1. <?php
  2. //调用
  3. require_once '../Common/Config.php';
  4. require_once '../Common/Function.php';
  5. if (!isset($_GET['code'])){
  6. a:
  7. $baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
  8. $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".$baseUrl."&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
  9. Header("Location: $url");
  10. exit();
  11. }else {
  12. $code=$_GET['code'];
  13. $openIdUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$code."&grant_type=authorization_code";
  14. $openIdResult=http_request($openIdUrl);
  15. if ($openIdResult){
  16. $userInfo=json_decode($openIdResult,true);
  17. if ($userInfo['errcode'] ==40029){
  18. goto a;
  19. }
  20. $dir="../Log";
  21. if (!is_dir($dir)){
  22. mkdir($dir);
  23. }
  24. $token_file=$dir.'/access_token';
  25. if (file_exists($token_file) && time()-filemtime($token_file)<4800){
  26. write_log("getInfo--文件中读取的token:".file_get_contents($token_file));
  27. $access_token= file_get_contents($token_file);
  28. }else{
  29. $accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
  30. $result=http_request($accessTokenUrl);
  31. echo $accessTokenUrl;
  32. echo $result;
  33. var_dump(json_decode($result,true));
  34. if(!$result){
  35. write_log("getInfo-获取token出错");
  36. echo "getInfo-获取token出错";
  37. exit;
  38. }
  39. $result_obj=json_decode($result);
  40. file_put_contents($token_file, $result_obj->access_token);
  41. write_log("getInfo-url获取的token:".$result_obj->access_token);
  42. $access_token= $result_obj->access_token;
  43. }
  44. $getUserInfoUrl="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$userInfo['openid']."&lang=zh_CN";
  45. $getUsesrInfo=http_request($getUserInfoUrl);
  46. print_r(json_decode($getUsesrInfo,true));
  47. }
  48. }
  49. exit;
  50. /* $sql="INSERT INTO qrcode(media_id,url,createtime) values('".$media_id."','".$Rurl."','".date("Y-m-d H:i:s")."')";
  51. $pdo = new PDO("sqlsrv:Server=".HOST.";Database=".DB, USER, PASSWORD);//5.6版本pdo连接sqlsrv
  52. $result=$pdo->exec($sql);
  53. $id= $pdo->lastInsertId(); */
  54. $getOpenidListUrl="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token;
  55. $getOpenidList=http_request($getOpenidListUrl);
  56. echo $getOpenidList;
  57. print_r(json_decode($getOpenidList,true));exit;
  58. if ($getOpenidList){
  59. write_log($getOpenidList);
  60. $openidList_obj=json_decode($getOpenidList);
  61. $ticket=$openidList_obj->ticket;
  62. write_log("qrcode.php-- $ticketUrl url获取ticket:".$ticket);
  63. }
  64. $qrcodeUrl="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
  65. write_log("$qrcodeUrl".$qrcodeUrl);
  66. $imageInfo=downloadImageFromWeiXin($qrcodeUrl);
  67. $dir="./Images";
  68. if (!is_dir($dir)){
  69. mkdir($dir);
  70. }
  71. $filename=$dir."/qrcode"."-".$id."-".time().".jpg";
  72. $local_file=fopen($filename, 'w');
  73. if ($local_file !==false){
  74. if(fwrite($local_file, $imageInfo["body"]) !== false){
  75. fclose($local_file);
  76. /* $filePath=dirname($_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'])."/".$filename;
  77. downfile($filePath); */
  78. }
  79. }
  80. header("Location: $qrcodeUrl");
  81. exit;