WeChat public platform development and Wechat public platform development

Source: Internet
Author: User
Tags openid

Public platform development, letter public platform development

1. Configure public platform Communication

First, you have a service number or enterprise number and have become a developer.

In this case, you can obtain the logon username and password of the public platform, as well as appid and appsecret (a total of four parameters)

Download wx_sample.php

The Code verifies the communication between the web site and the public platform.

The Code is as follows:

<? 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 ($ postStr )){

$ 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 ($ tmpArr );
$ TmpStr = implode ($ tmpArr );
$ TmpStr = sha1 ($ tmpStr );

If ($ tmpStr = $ signature ){
Return true;
} Else {
Return false;
}
}
}

?>

In development settings, configure url, token, and random string parameters and enable them:

The url is the url of wx_sample.php that can be accessed in the web site production environment.

The token is the token value defined in The wx_sanple.php file.

Noncestr generated by the system

 

2. Obtain code, access_token, and user information with openid on the web page.

First, set the authorized domain name to the domain name of the web site in the interface permission.

Get the code through appid:

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 ");
}

Then, obtain access_token, openid, and user information through code, persist user information, and log on using cookies:

Public function actionRegister (){
$ Params = Yii: $ app-> params;
$ Hostname = $ params ["hostname"];
$ Appid = $ params ["appid"];
$ Appsecret = $ params ["appsecret"];

$ Code = Yii: $ app-> request-> get ('code ');

$ Url = 'https: // api.weixin.qq.com/sns/oauth2/access_token? Appid = '. $ appid.' & secret = '. $ appsecret.' & code = '. $ code.' & grant_type = authorization_code ';
$ Json = $ this-> curl_get ($ url );
$ 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 ($ url)
{
$ Host = parse_url ($ url, PHP_URL_HOST );
$ Ch = curl_init ($ url );
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 ($ ch );
Curl_close ($ ch );
Return $ str;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.