|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- //调用
- require_once '../Common/Config.php';
- require_once '../Common/Function.php';
- if (!empty($_POST)){
- if( $_POST['media_id']=="" || $_POST['url']=="" ){
- echo "不能为空,填写真确的参数";
- exit;
- }
- $media_id=$_POST['media_id'];
- $Rurl=$_POST['url'];
- $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();
- $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("qrcode--文件中读取的token:".file_get_contents($token_file));
- $access_token= file_get_contents($token_file);
- }else{
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
- $result=http_request($url);
- if(!$result){
- write_log("qrcode-获取token出错");
- echo "qrcode-获取token出错";
- exit;
- }
-
- $result_obj=json_decode($result);
- file_put_contents($token_file, $result_obj->access_token);
- write_log("qrcode-url获取的token:".$result_obj->access_token);
- $access_token= $result_obj->access_token;
- }
- $qrcode='{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": '.$id.'}}}';
- $ticketUrl="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
- $resultTicket=http_request($ticketUrl,$qrcode);
- if ($resultTicket){
- write_log($resultTicket);
- $ticket_obj=json_decode($resultTicket);
- $ticket=$ticket_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;
- }
- ?>
- <html>
- <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
- <table>
- <tr><td align="right">media_id:</td><td><input type="text" name="media_id" value=""/></td></tr>
- <tr><td align="right">跳转url:</td><td><input type="text" name="url" value="" /></td></tr>
-
- </table>
- <input type="submit" value="提交" />
-
-
-
-
- </form>
- </html>
|