获取小程序任意页面的小程序码

先获取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);
    }


    

/**
     * 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){
        $header[] = "Content-type: text/xml"; // 改为数组解决
        $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,$header);
        $response = curl_exec($ch);
        curl_close( $ch );
        $rest = json_decode($response,true);
        return $response;
    }

个人博客

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、开启公众号开发者模式 公众平台的技术文档目的为了简明扼要的交代接口的使用,语句难免苦涩难懂,甚至对于不同的读者...
    good7758阅读 1,550评论 0 1
  • 开发前首先我们要知道一些概念 各公众号区别:1、订阅号:为媒体和个人提供一种信息传播方式,主要偏于为用户传达资讯(...
    CoderZS阅读 3,233评论 1 19
  • 光亮的不止星星 还有你追逐的目光 温热的不止太阳 还有你坚定的理想 人海如潮 你只专注自己的方向 漫漫长路 与孤独...
    南方姑娘思小鱼阅读 412评论 5 5
  • 什么是正则表达式? 正则表达式,又称正规表示法、常规表示法(英语:Regular Expression,在代码中常...
    Mg明明就是你阅读 2,471评论 0 7
  • 33期8班8组1号-金子-毕业感言 小伙伴们,你真的毕业了吗?无论如何14天理财训练营可是要结束了。看到班...
    忆遥呀阅读 204评论 3 9