You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

62 lines
2.1 KiB

  1. <?php
  2. //调用
  3. require_once '../Common/Config.php';
  4. require_once '../Common/Function.php';
  5. echo 1;
  6. /* $sql="INSERT INTO qrcode(media_id,url,createtime) values('".$media_id."','".$Rurl."','".date("Y-m-d H:i:s")."')";
  7. $pdo = new PDO("sqlsrv:Server=".HOST.";Database=".DB, USER, PASSWORD);//5.6版本pdo连接sqlsrv
  8. $result=$pdo->exec($sql);
  9. $id= $pdo->lastInsertId(); */
  10. $dir="../Log";
  11. if (!is_dir($dir)){
  12. mkdir($dir);
  13. }
  14. $token_file=$dir.'/access_token';
  15. if (file_exists($token_file) && time()-filemtime($token_file)<4800){
  16. write_log("qrcode--文件中读取的token:".file_get_contents($token_file));
  17. $access_token= file_get_contents($token_file);
  18. }else{
  19. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
  20. $result=http_request($url);
  21. if(!$result){
  22. write_log("qrcode-获取token出错");
  23. echo "qrcode-获取token出错";
  24. exit;
  25. }
  26. $result_obj=json_decode($result);
  27. file_put_contents($token_file, $result_obj->access_token);
  28. write_log("qrcode-url获取的token:".$result_obj->access_token);
  29. $access_token= $result_obj->access_token;
  30. }
  31. $getOpenidListUrl="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token;
  32. $getOpenidList=http_request($getOpenidListUrl);
  33. echo $getOpenidList;
  34. print_r(json_decode($getOpenidList,true));exit;
  35. if ($getOpenidList){
  36. write_log($getOpenidList);
  37. $openidList_obj=json_decode($getOpenidList);
  38. $ticket=$openidList_obj->ticket;
  39. write_log("qrcode.php-- $ticketUrl url获取ticket:".$ticket);
  40. }
  41. $qrcodeUrl="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
  42. write_log("$qrcodeUrl".$qrcodeUrl);
  43. $imageInfo=downloadImageFromWeiXin($qrcodeUrl);
  44. $dir="./Images";
  45. if (!is_dir($dir)){
  46. mkdir($dir);
  47. }
  48. $filename=$dir."/qrcode"."-".$id."-".time().".jpg";
  49. $local_file=fopen($filename, 'w');
  50. if ($local_file !==false){
  51. if(fwrite($local_file, $imageInfo["body"]) !== false){
  52. fclose($local_file);
  53. /* $filePath=dirname($_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'])."/".$filename;
  54. downfile($filePath); */
  55. }
  56. }
  57. header("Location: $qrcodeUrl");
  58. exit;