//
//
// %s
//
//
// 0
// ";
// if(!empty( $keyword ))
// {
// $msgType = "text";
// $contentStr = "Welcome to wechat world!";
// $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
// echo $resultStr;
// }else{
// echo "Input something...";
// }
//
// }else {
// echo "";
// exit;
// }
// }
public function getaccesstoken()
{
$access_token = S('access_token');
if(!empty($access_token)){
return $access_token;
}else{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->appsecret;;
$ret = $this->curlget($url);
$ret = json_decode($ret,true);
S('access_token',$ret['access_token'],7100);
return $ret['access_token'];
}
}
//根据openid拉取用户信息
public function userinfo($openid){
$access_token = $this->getaccesstoken();
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$userinfo = json_decode($this->curlget($url),true);
return $userinfo;
}
function p($arr){
echo "";
print_r($arr);
}
function curlget($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
public function jingmo($url,$code,$state){
if($code){
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->appsecret."&code=".$code."&grant_type=authorization_code";
$arrtemp = json_decode($this->curlget($url),true);
$ret = $this->getuserinfo($arrtemp['openid'],$arrtemp['access_token']);
return $ret;
}else{
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".$url."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect";
header("Location: $url");
}
}
public function getuserinfo($openid,$user_access_token){
$userurl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$user_access_token."&openid=".$openid."&lang=zh_CN";
$userinfo = json_decode($this->curlget($userurl),true);
return $userinfo;
}
public function getjsapi_ticket(){
$js_ticket = S("js_ticket");
if(!empty($js_ticket)){
return $js_ticket;
}else{
$access_token = $this->getaccesstoken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi";
$res = json_decode($this->curlget($url),true);
S("js_ticket",$res['ticket'],7100);
return $res['ticket'];
}
}
public function getjssign(){
echo "";
$js_ticket = $this->getjsapi_ticket();
$time = time();
$arr = array(
'noncestr'=>md5($time),
'jsapi_ticket'=>$js_ticket,
'timestamp'=>$time,
'url'=>'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'],
);
$this->p($arr);
ksort($arr);
$string="";
$i=1;
foreach($arr as $key=>$val){
if($i==1){
$string.=$key."=".$val;
}else{
$string.="&".$key."=".$val;
}
$i++;
echo $i;
}
// echo $string;
$signature =strtolower($string);
$signature = sha1($string);
$jsconfig = array(
'debug'=>true,
'appId'=>$this->appid,
'timestamp'=>$time,
'nonceStr'=>md5($time),
'signature'=>$signature,
' jsApiList'=>"['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ']",
);
$json = json_encode($jsconfig);
return $json;
}
//生成大写签名
function createsign($arr,$biaozhi){
ksort($arr);
$string="";
$i=1;
foreach($arr as $key=>$val){
if($i==1){
$string.=$key."=".$val;
}else{
$string.="&".$key."=".$val;
}
$i++;
}
$signtemp = "$string&key=" . $this->wechatkey;
$sign = strtoupper(MD5($signtemp));
$arr[$biaozhi] = $sign;
//$json = json_encode($arr);
return $arr;
}
//支付
//发起支付
public function sendpay($openid, $title, $out_trade_no, $total_fee, $notify_url)
{
$time = time();
$arr = array(
'appid' => $this->appid,
'mch_id' => $this->mchid,
'nonce_str' => md5($time),
'body' => "kjhk",
'out_trade_no' => $out_trade_no,
'total_fee' => $total_fee,
'spbill_create_ip' => "127.0.0.1",
'notify_url' => $notify_url,
'trade_type' => "JSAPI",
'openid' => $openid,
);
$biaozhi ='sign';
$arr = $this->createsign($arr,$biaozhi);
$xml = "";
foreach ($arr as $key=>$val) {
$xml.="".$val."".$key.">";
}
$xml.="";
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$resxml = $this->postCurlTransfer($url,$xml);
$res = simplexml_load_string($resxml);
$cutime = time();
$jsapiarr=array(
'appId'=>$this->appid,
'timeStamp'=>"$cutime",
'nonceStr'=>md5($cutime),
'package'=>"prepay_id=".$res->prepay_id,
'signType'=>"MD5",
);
$biaozhi = 'paySign';
$jsapi = $this->createsign($jsapiarr,$biaozhi);
// $jsapijson = json_encode($jsapi);
return $jsapi;
}
public function getpaysign(){
$time = time();
$arr = array(
'appid'=>$this->appid,
'mch_id'=>$this->mchid,
'nonce_str'=>md5($time),
'body'=>"sdf",
'out_trade_no'=>"2016".$time,
'total_fee'=>1,
'spbill_create_ip'=>"127.0.0.1",
'notify_url'=>"http://www.baidu.com",
'trade_type'=>"JSAPI",
);
ksort($arr);
$string="";
$i=1;
foreach($arr as $key=>$val){
if($i==1){
$string.=$key."=".$val;
}else{
$string.="&".$key."=".$val;
}
$i++;
}
$string.="&key=".$this->wechatkey;
$sign = strtoupper(md5($string));
$arr['sign'] = $sign;
return $arr;
}
function Post($curlPost,$url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
$return_str = curl_exec($curl);
curl_close($curl);
return $return_str;
}
private function postCurlTransfer($url, $data)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$str = curl_exec($curl);
curl_close($curl);
return $str;
}
//发送模板消息
public function sendtpl($openid, $url, $template_id, $content, $topcolor="#FF0000")
{
$arr = array(
'touser' => $openid,
'template_id' => $template_id,
'url' => $url,
'topcolor' => $topcolor,
'data' => $content,
);
$arrjson = json_encode($arr);
$accesstoken = $this->getAccessToken();
$sendurl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$accesstoken;
return $this->postCurlTransfer($sendurl, $arrjson);
}
}