菜单

微信徒人平台开拓

2019年9月25日 - Php

1.布置微信徒人平台通讯

第一已经颇具服务号或公司号并已经化为开拓者

那会儿能够获得微信民众平台登入客户名和密码以及appid和appsecret

下载wx_sample.php

该代码实现了注明web网址与微信徒人平台北间的通讯

代码如下:

<?php
/**
* wechat php test
*/

//define your token
define(“TOKEN”, “weixin”);
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET[“echostr”];

//valid signature , option
if($this->checkSignature{
echo $echoStr;
exit;
}
}

public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS[“HTTP_RAW_POST_DATA”];

//extract post data
if (!empty){

$postObj = simplexml_load_string($postStr, ‘SimpleXMLElement’,
LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = “<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>”;
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;
}
}

private function checkSignature()
{
$signature = $_GET[“signature”];
$timestamp = $_GET[“timestamp”];
$nonce = $_GET[“nonce”];

$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort;
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}

?>

在微信支付设置中布署url,token和任意字符串等多少个参数并启用:

url即为在web网站生产条件能访谈到该wx_sample.php的url

token即为该wx_sanple.php中定义的token的值

noncestr由系统生成

  1. 在web网页获取code和access_token及带openid的顾客音讯

先在接口权限中安装授权域名叫该web网址的域名

通过appid获取code:

public function actionLogin(){

$params = Yii::$app->params;
$hostname = $params[“hostname”];
$appid = $params[“appid”];
$appsecret = $params[“appsecret”];
$redirect_uri = “http://manhua.51haowenzhang.com/customer/register“;
$url =
https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect\_uri='.$redirect\_uri.'&response\_type=code&scope=snsapi\_userinfo&state=manhua\#wechat\_redirect‘;
Header(“Location: $url”);
}

再通过code获取access_token和openid及用户消息,并将客户消息长久化,并利用cookie登陆:

public function actionRegister(){
$params = Yii::$app->params;
$hostname = $params[“hostname”];
$appid = $params[“appid”];
$appsecret = $params[“appsecret”];

$code = Yii::$app->request->get;

$url =
https://api.weixin.qq.com/sns/oauth2/access\_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant\_type=authorization\_code‘;
$json = $this->curl_get;
$token = json_decode($json,true);
$access_token = $token[‘access_token’];
$openid = $token[‘openid’];

$customer =
Customer::find()->where(array(“openid”=>$openid))->one();
if($customer == null){
$customerinfo_url =
https://api.weixin.qq.com/sns/userinfo?access\_token='.$access\_token.'&openid='.$openid.'&lang=zh\_CN
‘;
$customerinfo_json = $this->curl_get($customerinfo_url);
$customerinfo = json_decode($customerinfo_json, true);

$customer = new Customer();
$customer->name = $customerinfo[‘nickname’];
$customer->headimg = $customerinfo[‘headimgurl’];
$customer->sex = $customerinfo[‘sex’];
$customer->openid = $customerinfo[‘openid’];
$customer->address =
$customerinfo[‘country’].$customerinfo[‘province’].$customerinfo[‘city’];
$customer->created_at = time();
$customer->updated_at = time();
$customer->is_del = 0;
$customer->money = 0;
$customer->save();
}

setCookie(“is_login”, 1, time()+3600*24*30*12, “/”);
setCookie(“customer_id”, $customer->id, time()+3600*24*30*12,
“/”);

$redirect_url = $hostname;
Header(“Location: $redirect_url”);
}

public function curl_get
{
$host = parse_url($url, PHP_URL_HOST);
$ch = curl_init;
curl_setopt($ch,CURLOPT_USERAGENT,’Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.2)’);
curl_setopt($ch,CURLOPT_REFERER, $host);
curl_setopt($ch,CURLOPT_TIMEOUT, 15);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$str = curl_exec;
curl_close;
return $str;
}

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图