php生成小程序二维码与小程序码

class Lib_WxMiniCodeClient
{

    const API_GET_CODE = 'https://api.weixin.qq.com/**/***';                     // 获取code的url
    const API_GET_CODEUNLIMIT = 'https://api.weixin.qq.com/***/***';             // 获取codeunlimit的url
    const API_CREATE_QRCODE = 'https://api.weixin.qq.com/cgi-bin/***/***';       // 获取qrcode

    protected $accessToken = '';

    public function __construct($accessToken = null)
    {
        $this->accessToken = $accessToken ? $accessToken : '';
    }

    /**
     * 生成小程序码 - 数量较少的业务场景
     * @param string $path
     * @param int $width
     * @param bool $autoColor
     * @param array $lineColor
     * @param bool $isHyaline
     * @return array|mixed
     */
    public function getcode($path = '', $width = 430, $autoColor = false, $lineColor = [], $isHyaline = false)
    {
        $lineColor = $lineColor ? $lineColor : ["r"=>"0","g"=>"0","b"=>0];
        $params = [
            'path' => $path,
            'width' => intval($width),
            'auto_color' => $autoColor,
            'line_color' => $lineColor,
            'is_hyaline' => $isHyaline
        ];
        return $this->httpPost(self::API_GET_CODE, $params);
    }

    /**
     * 生成小程序码 - 需要的码数量极多的业务场景
     * @param string $scene
     * @param string $page
     * @param int $width
     * @param bool $autoColor
     * @param array $lineColor
     * @param bool $isHyaline
     * @return array|mixed
     */
    public function getcodeunlimit($scene = '', $page = '', $width = 430, $autoColor = false, $lineColor = [], $isHyaline = false)
    {
        $lineColor = $lineColor ? $lineColor : ["r"=>"0","g"=>"0","b"=>"0"];
        $params = [
            'scene' => $scene,
            'page' => $page,
            'width' => intval($width),
            'auto_color' => $autoColor,
            'line_color' => $lineColor,
            'is_hyaline' => $isHyaline
        ];

        return $this->httpPost(self::API_GET_CODEUNLIMIT, $params);
    }

    /**
     * 生成小程序二维码
     * @param string $path
     * @param int $width
     * @return array|mixed
     */
    public function createqrcode($path = '', $width = 430)
    {
        $params = [
            'path' => $path,
            'width' => intval($width)
        ];

        return $this->httpPost(self::API_CREATE_QRCODE, $params);
    }

    public function httpPost($url, $data = array())
    {

        $url = $url . '?access_token=' . $this->accessToken;

        return $this->curlRaw($url, $data);
    }

    /**
     * @param $url
     * @param $data
     * @param string $method
     * @param bool $json
     * @return array|mixed
     */
    public function curlRaw($url, $data, $json = true)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

        if(!empty($data)){
            if($json && is_array($data)){
                $data = json_encode($data, JSON_UNESCAPED_UNICODE); // 不对中文进行转码, 针对微信某些接口更新字段有中文字符个数限制(比如更新卡劵接口)
            }
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            if($json){
                curl_setopt($curl, CURLOPT_HEADER, 0);
                $header = array(
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length:' . strlen($data)
                );
                curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
            }
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        $errorno = curl_errno($curl);
        if ($errorno) {
            return array('errorno' => false, 'errmsg' => $errorno);
        }
        curl_close($curl);
        return $res;
    }
}

希望有帮助 ~

发布了54 篇原创文章 · 获赞 209 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_34284638/article/details/102746361