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;
    }
}

希望有幫助 ~

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