1.注册微信公众号,新浪云
2.新浪云-->云应用-->创建新应用-->运行环境(PHP5.3),其他的自己填写,选择空应用-->创建成功,选择SVN-->创建一个新版本(输入的密码是一开始的登录的密码),--->上传代码包(wx_sample_20140819.zip)
3.登录微信公众号平台--->开发者工具--->公众平台测试账号--->扫描二维码 能够进入测试--->退出 回到开发者工具 ---->进入开发者文档 --->开发者开发 获取access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。
4.在数据库 613 中添加一个weixin表 id int(11) , token( text(513)) time(varchar)
5.在6.20weixin的文件目录之下新建一个sql.php 用于连接数据库
<?php
header("ContentType:text/html;charset:utf-8");
date_default_timezone_set("PRC");
mysql_connect("localhost","root","");
mysql_selected_db("613");
mysql_query("set names utf8");
?>
6. (1) 新建一个token.php
<?php
//引入sql.php
include_once "sql.php";
//在微信测试号管理中 获取测试号信息
$appid="wxec8ef1335c373a48";
$appsecret="aee043aa4ab7f2956b5cf33b82a08301";
//进入微信开发者文档 --->开始开发 ---->获取access_token (请求方式是get)
$api="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
//打开这个文件
$json=file_get_contents($api);
echo $json;
?>
实现的效果分析:
//正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效
在最后 echo phpinfo();
弹出一个关于php信息的一个东西 cmd+f 搜索 curl (输出)
这种的输出方法不是官方推荐的
(2) 对上边的代码进行改进 判断仓库里面是否有token 没有的话直接在数据库插入
<?php
//先引入一个获get()的方法
function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
//这个代码用来验证tooken是否验证失败 由于在本地测试故注释掉
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
//封装一个getToken()的方法
function getToken(){
global $appid;
global $appsecret;
//把上面的$api删除 在这个里面在设置
$api="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
$str=httpGet($api);
$arr=json_decode($str,true);
$accessToken=$arr["access_token"];
return $accessToken;
}
$sql="select * from weixin";
$result=mysql_query($sql);
if(mysql_num_rows($result)>0){
//判断数据库里面是否有token
//存在token 看是否过期 过期了重新获取 没过期直接用
$row=mysql_fetch_assoc($result);
$time=$row["time"];
if($nowtime-$time > 7200){
//过期了
$token=getToken();
$time=time();
$sql="update weixin set token='{$token}',time='{$time}' ";
mysql_query($sql);
}else{
//没过期
$token=$row["token"];
}
}else{
//没有参数的情况下 插入参数
$token=getToken();
$time=time();
$sql="insert into weixin(id,token,time) values(null,'{$token}','{$time}')";
mysql_query($sql);
}
//打开数据库 看有没插入
//封装一个获取IP的函数
function getIP($tooken){
$api="https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={$tooken}";
$json=httpGet($api);
$arr=json_decond($json,true);
return $arr["ip_list"];
}
//得到用户信息列表
function getUserList($tooken,$nextOpenId=false){
if($nextOpenId){
$api="https://api.weixin.qq.com/cgi-bin/user/get?access_token={$tooken}&next_openid={$nextOpenId}";
}else{
$api="https://api.weixin.qq.com/cgi-bin/user/get?access_token={$tooken}";
}
$json=httpGet($api);
echo json;
}
//获取用户详细信息
function getuserInfo($token,$userOpenId){
$api="https://api.weixin.qq.com/cgi-bin/user/info?access_token={$tooken}&openid={$userOpenId}&lang=zh_CN ";
$json=httpGet($api);
$arr=json_decond($json,true);
return $arr;
}
//把微信的头像弄出来
$userinfo=getUserInfo($token,"owmmfweV87fJCEm5F2tag-5osZjU");
echo "<img src=' ".$userinfo["headimgurl"]." '>";
//post请求URL 长连接转化成短连接
//https://api.weixin.qq.com/cgi-bin/shorturl?access_token=ACCESS_TOKEN
function longToShort($tooken,$long_url){
$api="https://api.weixin.qq.com/cgi-bin/shorturl?access_token={$tooken}";
//{\"action\":\"long2short\",\"long_url\":\"http://wap.koudaitong.com/v2/showcase/goods?alias=128wi9shh&spm=h56083&redirect_count=1\"}
$data='{"action":"long2short","long_url":"'.$long_url.'"}';//别忘了后边加上0
$json=httpPost($data, $api);
return json_decode($json,TRUE);
}
$arr=longToShort($tooken, "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433600&token=&lang=zh_CN");
//print_r($arr); //Array ( [errcode] => 0 [errmsg] => ok [short_url] => http://w.url.cn/s/AeqxJeY ) http://w.url.cn/s/AeqxJeY
echo $arr["short_url"]; //http://w.url.cn/s/ATOUUdV
?>