评论

PHP单文件获取小程序二维码

PHP单文件版,获取小程序内访问页面的二维码。

<?php
class RoutineCode{
		/**
     * 获取小程序内访问页面的二维码
     * @param string $path
     * @param int $width
     * @return mixed
     */
    public static function getPages($path = '',$width = 430){
        $accessToken = self::get_access_token();
        $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$accessToken;
        $data['path'] = $path;
        $data['width'] = $width;
        return self::curlPost($url,json_encode($data));
    }
	
	/**
     * 获取access_token access_token保存在文件中
     * @return mixed
     */
    public static function get_access_token(){
        $data = json_decode(file_get_contents("access_token.json"));
		if ($data->expire_time < time()) {
			$res = self::getAccessToken();
			$access_token = $res->access_token;
			if ($access_token) {
				$data->expire_time = time() + 7000;
				$data->access_token = $access_token;
				$fp = fopen("access_token.json", "w");
				fwrite($fp, json_encode($data));
				fclose($fp);
			}
		}else {
			$access_token = $data->access_token;
		}
		return $access_token;
    }
	
	/**
     * @param string $routineAppId
     * @param string $routineAppSecret
     * @return mixed
     */
    public static function getAccessToken(){
        $routineAppId = 'routineAppId';
        $routineAppSecret = 'routineAppSecret';
        $url  ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$routineAppId."&secret=".$routineAppSecret;
        return json_decode(self::curlGet($url),true);
    }
	
	/**
     * curl  get方式
     * @param string $url
     * @param array $options
     * @return mixed
     */
    public static function curlGet($url = '', $options = array())
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
	
	/**
     * curl post
     * @param string $url
     * @param string $postData
     * @param array $options
     * @return mixed
     */
    public static function curlPost($url = '', $postData = '', $options = array())
    {
        if (is_array($postData)) {
            $postData = http_build_query($postData);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https请求 不验证证书和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}
?>

点赞 3
收藏
评论

1 个评论

  • Admin ²º²⁴
    Admin ²º²⁴
    2020-02-21

    收藏,很好。

    直接拿来可以用。

    php是世界上最好的语言。

    虽然俺用php只用了5年多点。

    现在基本没碰php了。

    2020-02-21
    赞同
    回复 2
    • 答题小程序
      答题小程序
      2020-02-21
      拜见前辈!!!
      2020-02-21
      回复
    • Admin ²º²⁴
      Admin ²º²⁴
      2020-02-21回复答题小程序
      老大你千万没这么说。我得向你学习。php我是半道出家。只懂皮毛。
      2020-02-21
      回复
登录 后发表内容