微信公众号开发起步教程

286 次查看 0 条评论

1.申请测试号

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

2.新建入口文件index.php,代码如下:

<?php

//加载通用函数
require_once './function.php';

//微信配置信息
define('AppId','wx3285bb6628046044');
define('AppSecret','f25691ac37b051b46ffbc6b906301456');
define('EncodingAESKey','PLLTc1f2N50dALWOeQm7UBJzgEl8hMdrLu4ih5Iz482');
define('Token','token');

$wechat = new wechat();

//微信公众号安全验证(配置服务器URL时需要验证)
//$wechat->checkSignature();

//获取access_token
//$token = $wechat->getAccessToken();

/**
 * 微信公众平台开发类
 */
class wechat
{
    private $_appid;
    private $_appsecret;
    private $_EncodingAESKey;
    private $_token;

    /**
     * 通过构造函数赋值配置信息
     */
    public function __construct(){
        $this->_appid          = AppId;
        $this->_appsecret      = AppSecret;
        $this->_EncodingAESKey = EncodingAESKey;
        $this->_token          = Token;
    }

    /**
     * 验证签名,即验证消息的确来自微信服务器(接入微信平台时验证)
     * 实现步骤:
     * 1)将token、timestamp、nonce三个参数进行字典序排序
     * 2)将三个参数字符串拼接成一个字符串进行sha1加密
     * 3)开发者获得加密后的字符串与signature对比,标识该请求来源于微信
     * 4)若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败
     */
    public function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $tmpArr = array($this->_token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        if( $tmpStr === $signature )
        {
            exit($_GET["echostr"]);//验证通过后原样返回echostr给微信服务器
        }
    }

    /**
     * 获取access_token(接口调用凭证)
     * 已经实现了缓存access_token(没有有效的缓存才向微信服务器请求)
     */
    public function getAccessToken()
    {
        $cache = cache($this->_appsecret);
        $data = json_decode($cache);
        if( isset($data->expire_time) ? ($data->expire_time < time()) : true )
        {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->_appid}&secret={$this->_appsecret}";
            $res = json_decode(http_get($url));
            $access_token = $res->access_token;
            $data = array(
                'access_token' => $res->access_token,
                'expire_time'  => time() + 7000
            );
            cache($this->_appsecret,$data);
        }else
        {
            $access_token = isset($data->access_token) ? $data->access_token : false;
        }
        return $access_token;
    }

}

3.新建封装通用函数文件function.php,代码如下:

<?php
/**
 * 公共函数
 */

 /**
  * curl http get 请求
  */
 function http_get($url)
 {
    $oCurl = curl_init();
    if(stripos($url,"https://")!==FALSE)
    {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
    }
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($oCurl, CURLOPT_VERBOSE, 1);
    $sContent = curl_exec($oCurl);
    curl_close($oCurl);

    return $sContent;
 }


/**
 * POST 请求
 * @param string $url
 * @param array $param
 * @param boolean $post_file 是否文件上传
 * @return string content
 */
function http_post($url,$param,$post_file=false)
{
    $oCurl = curl_init();

    if(stripos($url,"https://")!==FALSE)
    {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }
    if(PHP_VERSION_ID >= 50500 && class_exists('\CURLFile'))
    {
        $is_curlFile = true;
    }else
    {
        $is_curlFile = false;
        if (defined('CURLOPT_SAFE_UPLOAD'))
        {
            curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
        }
    }

    if($post_file)
    {
        if($is_curlFile)
        {
            foreach ($param as $key => $val)
            {
                if(isset($val["tmp_name"]))
                {
                    $param[$key] = new \CURLFile(realpath($val["tmp_name"]),$val["type"],$val["name"]);
                }else if(substr($val, 0, 1) == '@')
                {
                    $param[$key] = new \CURLFile(realpath(substr($val,1)));
                }
            }
        }
        $strPOST = $param;
    }else
    {
        $strPOST = json_encode($param);
    }

    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($oCurl, CURLOPT_POST,true);
    curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
    curl_setopt($oCurl, CURLOPT_VERBOSE, 1);

    $sContent = curl_exec($oCurl);

    curl_close($oCurl);

    return $sContent;
}

/**
 * 浏览器友好的变量输出
 * @param mixed $var 变量
 * @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
 * @param string $label 标签 默认为空
 * @param boolean $strict 是否严谨 默认为true
 * @return void|string
 */
function dump($var, $echo=true, $label=null, $strict=true)
{
    $label = ($label === null) ? '' : rtrim($label) . ' ';
    if (!$strict)
    {
        if (ini_get('html_errors'))
        {
            $output = print_r($var, true);
            $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
        }else
        {
            $output = $label . print_r($var, true);
        }
    } else
    {
        ob_start();
        var_dump($var);
        $output = ob_get_clean();
        if (!extension_loaded('xdebug'))
        {
            $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
            $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
        }
    }
    if ($echo)
    {
        echo($output);
        return null;
    }else
    {
         return $output;
    }
}

/**
 * 简单php缓存函数
 * @param  [string] $key 缓存键名
 * @param  [string|array] $value 缓存值
 * @param  [int] $express 缓存有效时间(单位秒)
 * @return
 */
function cache($key='',$value='',$expire=0)
{
    $path = './cache/';//缓存目录
    $filename = $path.md5($key).'.php';//缓存文件路径
    switch(func_num_args())
    {
        case 1:
            //获取指定键名的缓存
            if(file_exists($filename))
            {
                return trim(substr(file_get_contents($filename), 15));
            }else
            {
                return false;
            }
            break;
        case 2:
            //设置缓存(设置成功返回true否则返回false)
            switch(gettype($value))
            {
                case 'string':
                case 'integer':
                case 'double':
                    $content = $value;
                    break;
                case 'array':
                    $content = json_encode($value);
                    break;
                default:
                    return false;
            }
            $fp = fopen($filename,'w');
            $res = fwrite($fp, "<?php exit();?>" . $content);//fwrite()返回写入的字符数,出现错误时则返回false
            fclose($fp);
            $res = ($res !== false) ? true : false;
            return $res;
            break;
        default:
            exit('cache param error');
    }
}

/**
 * 获取客户端Ip
 */
function getClientIp()
{
    if (getenv('HTTP_CLIENT_IP'))
    {
        $ip = getenv('HTTP_CLIENT_IP');
    }
    elseif (getenv('HTTP_X_FORWARDED_FOR'))
    {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_X_FORWARDED'))
    {
        $ip = getenv('HTTP_X_FORWARDED');
    }
    elseif (getenv('HTTP_FORWARDED_FOR'))
    {
        $ip = getenv('HTTP_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_FORWARDED'))
    {
        $ip = getenv('HTTP_FORWARDED');
    }
    else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

/**
 * 将数组转换成对象
 * @param [array] $arr 需要转换为对象的数组
 * @return 传入数组则返回转换后的对象,否则返回布尔值false
 */
function arrayToObject($arr)
{
    if(is_array($arr))
    {
        $obj = json_encode($arr);
        $obj = json_decode($obj);
    }else {
        $obj = false;
    }
    return $obj;
}

/**
 * 让火狐友好输出json数据
 * 不能有其他额外字符等输出,否则显示会异常
 */
function echoJson($json='')
{
    if(!is_null(json_decode($json)))
    {
        header('Content-type: application/json');
        echo $json;
    }else {
        echo 'Not valid json formatted data';
    }
}

学习示例:https://github.com/k649781645/wechat.git

Other 微信 , 公众号

暂无评论,快来抢沙发。