美文网首页
php 生成小程序二维码

php 生成小程序二维码

作者: 昱弟丶 | 来源:发表于2019-01-22 11:18 被阅读0次
//获取access_token
    public function gettoken()
    {
        $APPID = "wx**************";
        $APPSECRET =  "*******************************";
        $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;
        $json = $this->httpRequest($access_token);
        return  json_decode($json,true);
    }
    //curl
    public function httpRequest($url, $data='', $method='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;
    }

    //生成二维码
    public function qrcode($shopid,$userid)
    {
        $ACCESS_TOKEN = $this->gettoken();
        //dump($ACCESS_TOKEN);die;
        $puthc = '/pages/product/detail?pro_id='.$shopid.'&userid='.$userid;//小程序的路径 可以带参数
        $qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$ACCESS_TOKEN['access_token'];
        $param = json_encode(array("path"=>$puthc,"width"=> 150));
        $result = $this->httpRequest( $qcode,$param,"POST");
        //dump($result);die;
        $puth = "public/qrcode/".time().rand(100000,999999).'.png';
        //$base64_image ="data:image/jpeg;base64,".base64_encode($result);
        file_put_contents($puth,$result);
        return $puth;
    }

相关文章

网友评论

      本文标题:php 生成小程序二维码

      本文链接:https://www.haomeiwen.com/subject/rakljqtx.html