Get the applet parameters carried by two-dimensional code

  Today, a small program to get basic skills, is to generate a small program with parameters of two-dimensional codes, two-dimensional code applets and we usually do not cover direct link address is a difference between the two-dimensional code generated URL nature, but since no people know the address of the applet stored it, so, you want to generate direct swept away into the applet parameters and carry, you have to be in accordance with the provisions of the people.

  Project requirements: user two-dimensional code promotion, each promoting a new user, the user needs to promote a sum of promotion costs, therefore, to bring two-dimensional code in the current user id.

  As a white, needs to hear this I thought super simple, a link address parameter + Well, then, immediately started to write a random URLs, followed by a spell parameters, generate two-dimensional code, get! Waiting for the front to give me the address of the applet, then it. But the URL is hidden applet, get less, emmmm, okay, Who we dish it, I did not think of it then find a way, wielding magic - Baidu, Baidu about it, find https: // blog.csdn.net/weixin_39927850/article/details/81038024 , this is the original address. Micro letter officially open a small program to create three-dimensional code interfaces, one of which is to generate two-dimensional code, there is a sunflower-shaped applet code, I am here to generate two-dimensional code with php. Just two: 1. Obtain Access_token micro-channel document has developed an interface request: make your own applets to obtain APPID and APPSECRET

  

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

 

 2. Then look at yourself stitching path applet:path"=>"pages/index/index?id=123","width"=> 150

Complete code:

<?php
header('content-type:text/html;charset=utf-8');
//配置APPID、APPSECRET
$APPID = "APPID"; 
$APPSECRET =  "APPSECRET"; 
//获取access_token
$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";

//缓存access_token
 session_start();
 $_SESSION['access_token'] = "";
 $_SESSION['expires_in'] = 0;

 $ACCESS_TOKEN = "";
 if(!isset($_SESSION['access_token']) || (isset($_SESSION['expires_in']) && time() > $_SESSION['expires_in']))
 {

     $json = httpRequest( $access_token );
     $json = json_decode($json,true); 
     // var_dump($json);
     $_SESSION['access_token'] = $json['access_token']; 
     $ _SESSION [ ' expires_in ' ] = Time () + 7200 ; 
     $ ACCESS_TOKEN = $ JSON [ " the access_token " ]; 
 } 
 the else { 

     $ ACCESS_TOKEN = $ _SESSION [ " the access_token " ]; 
 } 

// build the two-dimensional code request parameter
 // path is a two-dimensional code scanning to jump a small program path, can take parameters? the above mentioned id = xxx
 // width is the width of the two-dimensional code 
$ qcode = " https://api.weixin.qq.com/cgi-bin/ wxaapp / createwxaqrcode the access_token = $ ACCESS_TOKEN? " ; 
$ param = json_encode (Array ( " path" => " Pages and the / index / index the above mentioned id = 123? " , " Width " => 150 )); // Here is the path applet is home ah or other pages 

// POST parameter 
$ result = httpRequest ($ qcode, param $, " the POST " );
 // generates two-dimensional code 
file_put_contents ( " qrcode.png " , $ Result); 
$ base64_image = " Data: Image / JPEG; Base64, " .base64_encode ($ Result); 

// the request sent to the micro-channel two-dimensional code server exchange 
  function httpRequest (URL $, $ Data = '' , $ Method = ' the GET '){
    $curl = curl_init();  
    curl_setopt($curl, CURLOPT_URL, $url);  
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);  
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);  
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);  
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);  
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);  
    if($method=='POST')
    {
        curl_setopt($curl, CURLOPT_POST, 1); 
        if ($data != '')
        {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  
        }
    }

    curl_setopt($curl, CURLOPT_TIMEOUT, 30);  
    curl_setopt($curl, CURLOPT_HEADER, 0);  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    $result = curl_exec($curl);  
    curl_close($curl);  
    return $result;
  } 

?>

Through the above code can obtain two-dimensional code is a small program with parameters, and only need to create a file Qrcode.php use, and then introduced it.

Then I will make this code a little more fitting change their code logic, I wrote this code function.php file

function qrcod($pid,$code){
    header('content-type:text/html;charset=utf-8');
    //配置APPID、APPSECRET
    $APPID = "wx6cafbcf25cde43af"; 
    $APPSECRET =  "6a66232d8e8aa7707560c88357602755"; 
    //获取access_token
    $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";

    //缓存access_token
    // session_start();
     $_SESSION['access_token'] = "";
     $_SESSION['expires_in'] = 0;

     $ACCESS_TOKEN = "";
     if(!isset($_SESSION['access_token']) || (isset($_SESSION['expires_in']) && time() > $_SESSION['expires_in']))
     {

         $json = httpRequest( $access_token );
         $json = json_decode($json,true); 
         // var_dump($json);
         $_SESSION['access_token'] = $json['access_token'];
         $_SESSION[ 'expires_in'] = Time (+7200) ;
          $ ACCESS_TOKEN = $ JSON [ "the access_token" ]; 
     } 
     the else { 

         $ ACCESS_TOKEN =   $ _SESSION [ "the access_token" ]; 
     } 

    // build the two-dimensional code request parameter 
    // path is scan the QR code to jump a small program path, you can take parameters? the above mentioned id = xxx 
    // width is the width of the two-dimensional code 
    $ qcode = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode? = the access_token $ ACCESS_TOKEN " ;
     IF (! empty ( $ pid )) { // personal 
         $ param = json_encode ( Array("path"=>"pages/tabbar/Home/Home?pid=$pid","width"=> 300));
          $img = "qrcodes/" . $pid  . '.jpg';  //就这个uid
    }else{ //网吧
         $param = json_encode(array("path"=>"pages/tabbar/Home/Home?code=$code","width"=> 300));
          $img = "qrcodes/" . $code  . '.jpg';  //就这个uid
    }
    

    //POST参数
    $result = httpRequest( $qcode, $param,"POST");
    //生成二维码
    
     $url = ROOT_PATH . '/' . $img;
     file_put_contents($url, $result);
    $a=array();
    $a['qrcode'] = "https://peiwan.cyk.ink" . '/' . $img;
    $base64_image ="data:image/jpeg;base64,".base64_encode( $result );
    return $a;
}

 

// send the request to the server in exchange for the micro-channel two-dimensional code 
    function httpRequest ( $ URL , $ Data = '', $ Method = 'the GET' ) {
         $ curl = curl_init ();   
        curl_setopt ( $ curl , CURLOPT_URL to, $ URL );   
        curl_setopt ( $ curl , CURLOPT_SSL_VERIFYPEER, 0 );   
        curl_setopt ( $ curl , CURLOPT_SSL_VERIFYHOST, 0 );   
        curl_setopt ( $ curl , CURLOPT_USERAGENT, $ _SERVER [ 'the HTTP_USER_AGENT' ]);   
        curl_setopt ( $ curl, CURLOPT_FOLLOWLOCATION, 1);  
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);  
        if($method=='POST')
        {
            curl_setopt($curl, CURLOPT_POST, 1); 
            if ($data != '')
            {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  
            }
        }

        curl_setopt($curl, CURLOPT_TIMEOUT, 30);  
        curl_setopt($curl, CURLOPT_HEADER, 0);  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
        $result = curl_exec($curl);  
        curl_close($curl);  
        return $result;
} 

In this way, I do not care what the use of this code is in place, only you need to call qrcod method on it,

 

 

Guess you like

Origin www.cnblogs.com/cyk2/p/11918438.html