A5下载站:努力做内容最丰富最安全的下载站! 网站地图最新更新下载排行专题软件发布

热门软件

地铁跑酷

冒险迷岛

全民迷宫

连连消大作战

小河狸创客

阿里健康医鹿

支付宝app

番薯小说

MOMO陌陌

虾米音乐app

位置导航:A5下载 > 源码技巧 > 父类数据

微信公众平台通用接口API(PHP版)

时间:2015-03-28 10:05来源:a5源码作者:zhao浏览:422
目前好多同事已经开始新的工作,微信帮助同事们重写了一下PHP下面的微信API接口……

目前好多同事已经开始新的工作,微信帮助同事们重写了一下PHP下面的微信API接口

 

<?php

/********************************************************

*

* @author Kyler You <QQ:2444756311>

*

* @link http://mp.weixin.qq.com/wiki/home/index.html

*

* @version 2.0.1

*

* @uses $wxApi = new WxApi();

*

* @package 微信API接口 陆续会继续进行更新

*

********************************************************/

class WxApi {

const appId = "";

const appSecret = "";

public function __construct(){

}

/****************************************************

*

* 微信提交API方法,返回微信指定JSON

*

****************************************************/

public function wxHttpsRequest($url,$data = null){

$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)){

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

}

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($curl);

curl_close($curl);

return $output;

}

/****************************************************

*

* 微信获取AccessToken 返回指定微信公众号的at信息

*

****************************************************/

public function wxAccessToken($appId = NULL , $appSecret = NULL){

$appId = is_null($appId) ? self::appId : $appId;

$appSecret = is_null($appSecret) ? self::appSecret : $appSecret;

//echo $appId,$appSecret;

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;

$result = $this->wxHttpsRequest($url);

//print_r($result);

$jsoninfo = json_decode($result, true);

$access_token = $jsoninfo["access_token"];

return $access_token;

}

/****************************************************

*

* 微信通过OPENID获取用户信息,返回数组

*

****************************************************/

public function wxGetUser($openId){

$wxAccessToken = $this->wxAccessToken();

$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$wxAccessToken."&openid=".$openId."&lang=zh_CN";

$result = $this->wxHttpsRequest($url);

$jsoninfo = json_decode($result, true);

return $jsoninfo;

}

/****************************************************

*

* 微信通过指定模板信息发送给指定用户,发送完成后返回指定JSON数据

*

****************************************************/

public function wxSendTemplate($jsonData){

$wxAccessToken = $this->wxAccessToken();

$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$wxAccessToken;

$result = $this->wxHttpsRequest($url,$jsonData);

return $result;

}

/****************************************************

*

* 微信设置OAUTH跳转URL,返回字符串信息 - SCOPE = snsapi_base //验证时不返回确认页面,只能获取OPENID

*

****************************************************/

public function wxOauthBase($redirectUrl,$state = "",$appId = NULL){

$appId = is_null($appId) ? self::appId : $appId;

$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_base&state=".$state."#wechat_redirect";

return $url;

}

/****************************************************

*

* 微信设置OAUTH跳转URL,返回字符串信息 - SCOPE = snsapi_userinfo //获取用户完整信息

*

****************************************************/

public function wxOauthUserinfo($redirectUrl,$state = "",$appId = NULL){

$appId = is_null($appId) ? self::appId : $appId;

$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirectUrl."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect";

return $url;

}

/****************************************************

*

* 微信OAUTH跳转指定URL

*

****************************************************/

public function wxHeader($url){

header("location:".$url);

}

/****************************************************

*

* 微信通过OAUTH返回页面中获取AT信息

*

****************************************************/

public function wxOauthAccessToken($code,$appId = NULL , $appSecret = NULL){

$appId = is_null($appId) ? self::appId : $appId;

$appSecret = is_null($appSecret) ? self::appSecret : $appSecret;

$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appId."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code";

$result = $this->wxHttpsRequest($url);

//print_r($result);

$jsoninfo = json_decode($result, true);

//$access_token = $jsoninfo["access_token"];

return $jsoninfo;

}

/****************************************************

*

* 微信通过OAUTH的Access_Token的信息获取当前用户信息 // 只执行在snsapi_userinfo模式运行

*

****************************************************/

public function wxOauthUser($OauthAT,$openId){

$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$OauthAT."&openid=".$openId."&lang=zh_CN";

$result = $this->wxHttpsRequest($url);

$jsoninfo = json_decode($result, true);

return $jsoninfo;

}

}