php公衆號帶參數二維碼生成

                  TP5公衆號帶參數二維碼生成

php代碼示例(已測試):

//二維碼type
    public function qrcode($id,$type='1'){
        if($type == 1){
          $para='uid,'.$id;
        }else{
          $para='container,'.$id;
        }
        $arr=['action_name'=>'QR_LIMIT_STR_SCENE','action_info'=>['scene'=>['scene_str'=>$para]]];
        $data=json_encode($arr);
        $url='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.getAccessToken();
        $result=CURLSend($url,'post',$data);
        $results=json_decode($result,true);
        $qecodeUrl='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.UrlEncode($results['ticket']);
        $res = $this->uploadImg($qecodeUrl);
        return $res;
    }
    
   public function uploadImg($img){
        $path = DOC_ROOT.'upload/qrcode';
        if(!file_exists($path)) {
            mkdir($path, 0777, true);
        }
        ob_clean();
        ob_start();
        $ext="/".time().".png";
        readfile($img);     //讀取圖片
        $img=ob_get_contents(); //得到緩衝區中保存的圖片
        ob_end_clean();     //清空緩衝區
        $fp = fopen($path.$ext,'w');    //寫入圖片
        if(fwrite($fp,$img)){
            fclose($fp);
            return IMGS.'upload/qrcode'.$ext;
        }
        return false;
    }

//獲取access_token,微信調用接口憑據(使用接口需要用到)
//access_token獲取頻次2000/天,2小時內有效
function getAccessToken(){
    $config=db('public_wx_config')->field('appid,appsecret,expires_in,access_token')->where('id',1)->find();
    $appid=$config['appid'];
    $secret=$config['appsecret'];
    if($config['expires_in']< time()){
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
        $tmp=CURLSend($url,'get');
        $obj=json_decode($tmp);
        $dty=(time()+$obj->expires_in);
        $data['access_token']=$obj->access_token;
        $data['expires_in']=$dty;
        db('public_wx_config')->where('id',1)->update($data);
        return $obj->access_token;
    }else {
        return  $config['access_token'];
    }
}

 /*
   *模擬瀏覽器發送
   */
 function CURLSend($url,$method = 'get', $data = ''){
   $ch = curl_init();//初始化
   $headers = array('Accept-Charset: utf-8');
   //設置URL和相應的選項
   curl_setopt($ch, CURLOPT_URL, $url);//指定請求的URL
   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));//提交方式
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//不驗證SSL
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//不驗證SSL
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//設置HTTP頭字段的數組
   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible;MSIE5.01;Windows NT 5.0)');//頭的字符串
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_AUTOREFERER, 1);//自動設置header中的Referer:信息
   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//提交數值
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//是否輸出到屏幕上,true不直接輸出
   $temp = curl_exec($ch);//執行並獲取結果
   curl_close($ch);
   return $temp;//return 返回值
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章