獲取小程序任意頁面的小程序碼

先獲取access_token

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的密鑰

使用raw方式,測試用postman
請求

body填json:
(scene)是參數

{
“path”: “pages/index/index”,
“scene”: “”
}

POST請求:

  • url (access_token)替換自己的

https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=10_d4P_drI7q30_vBzhFH4gW3F2GwIeysenTyAmqPjqC6dn1HWqaNt9xZEO6L2J2YXXVuZNQx8SN_w0HZywGeHHc3wj_V-ozy-ocb-NxLrupfVj-7wde3zNXA_Gv0sY8TEgetIyVikSXNkkz2FsWIVjAIAPBO

#####這只是其中的一種請求獲取的方式,更多的方式查看
小程序獲取二維碼


#上面的是通過postman來獲取小程序碼,下面的是通過代碼獲取

public function getWxappQrCode($id)
    {
        $appid='xxxx';
        $secert='xxxxxxx';
        //處理獲取二維碼
        $tokenurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secert";
        $tokeninfo = $this->curlget($tokenurl);
        $access_token = $tokeninfo['access_token'];

        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";
        $url = $url.$access_token;

        $postparam = array(
            'scene'=>$id
        );

        $qrdata = $this->getcomponent2($url,json_encode($postparam));
        $qCodePath = $qrdata;

        $qCodeImg = imagecreatefromstring($qCodePath);

//        list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
//        list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodeImg);
        // imagecopymerge使用註解
        $filename = time().rand(100,900).".jpg";

        $path =PATH_UPLOAD."qrcode";
        if (file_exists($path))
        {
            imagejpeg($qCodeImg,$path."/".$filename);
            $p = URL."/www/data/upload/diyqr/";
            $fn = $filename;

            $data['img'] = $p.$fn;
            ob_end_clean();
            header("content-disposition:attachment;filename=wxqrcode.png");
            header("content-length:" . filesize($path.'/'.$filename));
            readfile($path.'/'.$filename);
        }
        //保存文件
        imagedestroy($qCodeImg);
    }


        //保存文件
        imagedestroy($qCodeImg);
    }


/**
     * get請求
     * @param $url
     * @return mixed
     */
    public function curlget($url){
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        $response = curl_exec($ch);
        curl_close( $ch );
        $rest = json_decode($response,true);
        return $rest;
    }

public function getcomponent2($url,$postdata){

        $ch = curl_init(); //用curl發送數據給api
        // curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        curl_setopt($ch, CURLOPT_HTTPHEADER,0);
        $response = curl_exec($ch);
        curl_close( $ch );
        $rest = json_decode($response,true);
        return $response;
    }

個人博客

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