php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
主要介绍了php官方微信接口大全,包括微信支付、微信红包、微信摇一摇、微信小店,感兴趣的小伙伴们可以参考一下
微信入口绑定,微信事件处理,微信API全部操作包含在这些文件中。
内容有:微信摇一摇接口/微信多客服接口/微信支付接口/微信红包接口/微信卡券接口/微信小店接口/JSAPI
<?php
class WxApi {
const appId = "";
const appSecret = "";
const mchid = ""; //商户号
const privatekey = ""; //私钥
public $parameters = array();
public $jsApiTicket = NULL;
public $jsApiTime = NULL;
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;
}
/****************************************************
* 微信带证书提交数据 - 微信红包使用
****************************************************/
public function wxHttpsRequestPem($url, $vars, $second=30,$aHeader=array()){
$ch = curl_init();
//超时时间
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//这里设置代理,如果有的话
//curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//以下两种方式需选择一种
//第一种方法,cert 与 key 分别属于两个.pem文件
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/apiclient_cert.pem');
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/apiclient_key.pem');
curl_setopt($ch,CURLOPT_CAINFO,'PEM');
curl_setopt($ch,CURLOPT_CAINFO,getcwd().'/rootca.pem');
//第二种方式,两个文件合成一个.pem文件
//curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error";
curl_close($ch);
return false;
}
}
/****************************************************
* 微信获取AccessToken 返回指定微信公众号的at信息
****************************************************/
public function wxAccessToken($appId = NULL , $appSecret = NULL){
$appId = is_null($appId) ? self::appId : $appId;
$appSecret = is_null($appSecret) ? self::appSecret : $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;
}
/****************************************************
* 微信获取ApiTicket 返回指定微信公众号的at信息
****************************************************/
public function wxJsApiTicket($appId = NULL , $appSecret = NULL){
$appId = is_null($appId) ? self::appId : $appId;
$appSecret = is_null($appSecret) ? self::appSecret : $appSecret;
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$this->wxAccessToken();
$result = $this->wxHttpsRequest($url);
$jsoninfo = json_decode($result, true);
$ticket = $jsoninfo['ticket'];
//echo $ticket . "<br />";
return $ticket;
}
public function wxVerifyJsApiTicket($appId = NULL , $appSecret = NULL){
if(!empty($this->jsApiTime) && intval($this->jsApiTime) > time() && !empty($this->jsApiTicket)){
$ticket = $this->jsApiTicket;