Getting started with Wechat

This week is about to end,it didn’t receive any email from DHL customers until now,so i am a little bit not used it.

The good news is I can spend more time to study some courses,such as Android or Wechat.i finished the WeChat Public Platform Development Best Practice,know more about Wechat developement and take some notes below.

How to create a Wechat test account?
Just go to http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login and click the login button,then use Scan QR Code functionality on you mobile phone to scan the QR on the desktop,it’s very easy.

How to set up URL & Token with your own server?
First,we should have our own server,and set up a can-access website as wechat developement project,like this http://wx.webtribe.com.cn.I set the URL to http://wx.webtribe.com.cn/api.php,then set the Token to webtribe,so the codes in api.php are:
[php]
/*
Webtribe http://www.webtribe.com.cn
Pengcheng 2016-05-12
*/

define(“TOKEN”, “webtribe”);
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET[‘echostr’])) {
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}

class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET[“echostr”];
if($this->checkSignature()){
header(‘content-type:text’);
echo $echoStr;
exit;
}
}

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

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

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

public function responseMsg()
{
$postStr = $GLOBALS[“HTTP_RAW_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 = “


%s


0
“;
if($keyword == “?” || $keyword == “?”)
{
$msgType = “text”;
$contentStr = date(“Y-m-d H:i:s”,time());
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
}else{
echo “”;
exit;
}
}
}
[/php]

How to get the Access Token?
Just request https://api.weixin.qq.com/cgi-bin/token with your appid and secret of you wechat test account,the final URL maybe is https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret,ok,let me share the codes to you.
[php]
$appid = “wx97fxxxxxxxx9bbda”;
$appsecret = “3efd14bcxxxxxxx38ade06cf697fc40”;
$url = “https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret”;

$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);
$output = curl_exec($ch);
curl_close($ch);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo[“access_token”];
echo $access_token;
exit;
[/php]

i have not finished wechat developement,will write more when i get more,so stay tuned!