一、Introduction
微信公众号是一个平台。目前,提供了4类帐号,订阅号、服务号、小程序、企业微信。具体区别在官网有说明。
二、使用接口测试号开发
1. 申请接口测试号
平台提供了接口测试号。到公众平台开发者文档,可以申请测试号开发。申请后提供appID和appsecret等,以供开发使用。
2. access_token
access_token是公众号的全局唯一接口调用凭证,调用接口时都需使用access_token,以提高安全性。access_token的请求方式,通过客户端,或通过企业服务器。获取方式三种,
第一种,通过浏览器访问url,https请求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
第二种,通过微信平台获取,在开发者文档获取access_token,点击使用网页调试工具调试该接口;
第三种,编写php程序来获取,使用cURL函数库,client url library。使用curl,先在php.ini开启curl的扩展。
<?php
/**
* 使用curl函数
*/
function_request($curl, $https=true, $method='get', $data=null)
{
//初始化
$ch = curl_init();
//设置url
curl_setopt($ch, CURLOPT_URL, $curl);
//设置不需要头信息
curl_setopt($ch, CURLOPT_HEADER,false);
//至获取页面内容,但不输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
if($https)
{
//设置不做服务器认证
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
//不做客户端认证
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
}
if($method == 'post')
{
//设置请求方式为post
curl_setopt($ch,CURLOPT_POST, true);
//设置请求数据
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
}
//执行并接收输出
$str = curl_exec($ch);
//释放资源
curl_close($ch);
return $str;
}
echo _request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx49119aeb8923abfd&secret=96e13016d1cb24026545e5ed7c133fac");
/**
* 获取access_token
*/
function_getAccesstoken()
{
$file = './accessToken.txt';
//判断文件是否存在
if(file_exists($file))
{
$content =file_get_contents($file);
//json解码文件的内容
$content =json_decode($content);
//判断是否过期
if(time() - filemtime($file)< $content->expires_in)
{
return$content->access_token;
}
}
$content =_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx49119aeb8923abfd&secret=96e13016d1cb24026545e5ed7c133fac");
file_put_contents($file, $content);
$content = json_decode($content);
return $content->access_token;
}
echo_getAccesstoken();
3. 使用php封装一个获取access_token的工具类
<?php
/**
* 定义微信工具类
*/
class WeChat
{
//appid
private $_appid;
//appsecret
private $_appsecret;
//token
private $_token;
/**********************************************************/
/**
* 构造函数
*/
public function __construct($appid,$appsecret, $token)
{
$this->_appid = $appid;
$this->_appsecret =$appsecret;
$this->_token = $token;
}
/**
* 使用curl函数抓取url内容
*/
private function _request($curl,$https=true, $method='get', $data=null)
{
//初始化
$ch = curl_init();
//设置url
curl_setopt($ch, CURLOPT_URL,$curl);
//设置不需要头信息
curl_setopt($ch,CURLOPT_HEADER, false);
//至获取页面内容,但不输出
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
if($https)
{
//设置不做服务器认证
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
//不做客户端认证
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
}
if($method == 'post')
{
//设置请求方式为post
curl_setopt($ch,CURLOPT_POST, true);
//设置请求数据
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
}
//执行并接收输出
$str = curl_exec($ch);
//释放资源
curl_close($ch);
return $str;
}
/**
* 获取access_token
*/
private function _getAccesstoken()
{
$file = './accessToken.txt';
//判断文件是否存在
if(file_exists($file))
{
$content =file_get_contents($file);
//json解码文件的内容
$content =json_decode($content);
//判断是否过期
if(time() -filemtime($file) < $content->expires_in)
{
return $content->access_token;
}
}
$content =_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret);
file_put_contents($file,$content);
$content = json_decode($content);
return$content->access_token;
}
}
4. 生成带参数的二维码
QR Code,Quick Response Code。用于用户渠道推广分析和用户帐号绑定等场景。分为临时二维码和永久二维码。获取的步骤,创建二维码ticket,凭借ticket到指定url获取二维码。
获取ticket
在工具类中添加获取ticket的方法
/**
* 获取ticket
* expires_seconds,int,二维码的有效期,单位s;
* type,str,二维码的类型,永久或临时;
* scene,int,使用二维码场景编号;
*/
public function_getTicket($expires_seconds = 604800, $type = "temp", $scene = 1)
{
if($type == "temp")
{
$data ='{"expire_seconds": '.$expires_seconds.', "action_name":"QR_STR_SCENE", "action_info": {"scene":{"scene_str": "'.$scene.'"}}}';
return$this->_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->_getAccesstoken(),true, "post", $data);
}
else
{
$data ='{"action_name": "QR_LIMIT_STR_SCENE","action_info": {"scene": {"scene_str":"'.$scene.'"}}}';
return$this->_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->_getAccesstoken(),true, "post", $data);
}
}
通过ticket获取二维码
在类中添加获取二维码的方法
/**
* 获取二维码的方法
*/
public function_getQRCode($expires_seconds = '604800', $type = 'temp', $scene = 1)
{
$content =$this->_getTicket($expires_seconds, $type, $scene);
$content =json_decode($content);
$ticket =$content->ticket;
$image =$this->_request("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket));
$file ="./".date("Y-m-d").'_'.$type.$scene.".jpg";
file_put_contents($file,$image);
return $image;
}
测试获取二维码的方法
<?php
define("APPID","wx49119aeb8923abfd");
define("APPSECRET","96e13016d1cb24026545e5ed7c133fac");
define("TOKEN","");
require'./Wechat.class.php';
header('Content-type:image/jpg');
$wechat = newWeChat(APPID, APPSECRET, TOKEN);
// echo $wechat->_getTicket($expires_seconds= 604800, $type = "yongjiu", $scene = 2);
echo$wechat->_getQRCode(604800, 'temp', 5);
5. 接口配置
接入校验,就是微信服务器地址是否正确。在接收消息时,要验证消息真实性。
平台官网提供了验证的样本代码,可以修改token为自己需要的。
<?php
/**
* wechat php test
* update time: 20141008
*/
//define your token
define("TOKEN"