{"id":96,"date":"2016-05-12T23:10:20","date_gmt":"2016-05-12T15:10:20","guid":{"rendered":"http:\/\/www.sunpengcheng.com\/?p=96"},"modified":"2016-05-12T23:11:14","modified_gmt":"2016-05-12T15:11:14","slug":"getting-started-with-wechat","status":"publish","type":"post","link":"https:\/\/www.sunpengcheng.com\/index.php\/2016\/05\/12\/getting-started-with-wechat\/","title":{"rendered":"Getting started with Wechat"},"content":{"rendered":"<p>This week is about to end,it didn&#8217;t receive any email from DHL customers until now,so i am a little bit not used it.<\/p>\n<p>The good news is I can spend more time to study some courses,such as Android or Wechat.i finished the <em>WeChat Public Platform Development Best Practice<\/em>,know more about Wechat developement and take some notes below.<\/p>\n<p><strong>How to create a Wechat test account?<\/strong><br \/>\nJust go to <a href=\"http:\/\/mp.weixin.qq.com\/debug\/cgi-bin\/sandbox?t=sandbox\/login\">http:\/\/mp.weixin.qq.com\/debug\/cgi-bin\/sandbox?t=sandbox\/login<\/a> and click the login button,then use Scan QR Code functionality on you mobile phone to scan the QR on the desktop,it&#8217;s very easy.<\/p>\n<p><strong>How to set up URL &amp; Token with your own server?<\/strong><br \/>\nFirst,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:<br \/>\n[php]<br \/>\n\/*<br \/>\n\tWebtribe http:\/\/www.webtribe.com.cn<br \/>\n\tPengcheng 2016-05-12<br \/>\n*\/<\/p>\n<p>define(&#8220;TOKEN&#8221;, &#8220;webtribe&#8221;);<br \/>\n$wechatObj = new wechatCallbackapiTest();<br \/>\nif (isset($_GET[&#8216;echostr&#8217;])) {<br \/>\n\t$wechatObj->valid();<br \/>\n}else{<br \/>\n\t$wechatObj->responseMsg();<br \/>\n}<\/p>\n<p>class wechatCallbackapiTest<br \/>\n{<br \/>\n\tpublic function valid()<br \/>\n\t{<br \/>\n\t\t$echoStr = $_GET[&#8220;echostr&#8221;];<br \/>\n\t\tif($this->checkSignature()){<br \/>\n\t\t\theader(&#8216;content-type:text&#8217;);<br \/>\n\t\t\techo $echoStr;<br \/>\n\t\t\texit;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tprivate function checkSignature()<br \/>\n\t{<br \/>\n\t\t$signature = $_GET[&#8220;signature&#8221;];<br \/>\n\t\t$timestamp = $_GET[&#8220;timestamp&#8221;];<br \/>\n\t\t$nonce = $_GET[&#8220;nonce&#8221;];<\/p>\n<p>\t\t$token = TOKEN;<br \/>\n\t\t$tmpArr = array($token, $timestamp, $nonce);<br \/>\n\t\tsort($tmpArr, SORT_STRING);<br \/>\n\t\t$tmpStr = implode( $tmpArr );<br \/>\n\t\t$tmpStr = sha1( $tmpStr );<\/p>\n<p>\t\tif( $tmpStr == $signature ){<br \/>\n\t\t\treturn true;<br \/>\n\t\t}else{<br \/>\n\t\t\treturn false;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tpublic function responseMsg()<br \/>\n\t{<br \/>\n\t\t$postStr = $GLOBALS[&#8220;HTTP_RAW_POST_DATA&#8221;];<\/p>\n<p>\t\tif (!empty($postStr)){<br \/>\n\t\t\t$postObj = simplexml_load_string($postStr, &#8216;SimpleXMLElement&#8217;, LIBXML_NOCDATA);<br \/>\n\t\t\t$fromUsername = $postObj->FromUserName;<br \/>\n\t\t\t$toUsername = $postObj->ToUserName;<br \/>\n\t\t\t$keyword = trim($postObj->Content);<br \/>\n\t\t\t$time = time();<br \/>\n\t\t\t$textTpl = &#8220;<xml><br \/>\n\t\t\t\t\t\t<ToUserName><![CDATA[%s]]><\/ToUserName><br \/>\n\t\t\t\t\t\t<FromUserName><![CDATA[%s]]><\/FromUserName><br \/>\n\t\t\t\t\t\t<CreateTime>%s<\/CreateTime><br \/>\n\t\t\t\t\t\t<MsgType><![CDATA[%s]]><\/MsgType><br \/>\n\t\t\t\t\t\t<Content><![CDATA[%s]]><\/Content><br \/>\n\t\t\t\t\t\t<FuncFlag>0<\/FuncFlag><br \/>\n\t\t\t\t\t\t<\/xml>&#8220;;<br \/>\n\t\t\tif($keyword == &#8220;?&#8221; || $keyword == &#8220;\uff1f&#8221;)<br \/>\n\t\t\t{<br \/>\n\t\t\t\t$msgType = &#8220;text&#8221;;<br \/>\n\t\t\t\t$contentStr = date(&#8220;Y-m-d H:i:s&#8221;,time());<br \/>\n\t\t\t\t$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);<br \/>\n\t\t\t\techo $resultStr;<br \/>\n\t\t\t}<br \/>\n\t\t}else{<br \/>\n\t\t\techo &#8220;&#8221;;<br \/>\n\t\t\texit;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/php]<\/p>\n<p><strong>How to get the Access Token?<\/strong><br \/>\nJust request <a href=\"https:\/\/api.weixin.qq.com\/cgi-bin\/token\">https:\/\/api.weixin.qq.com\/cgi-bin\/token<\/a> 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&amp;appid=$appid&amp;secret=$appsecret,ok,let me share the codes to you.<br \/>\n[php]<br \/>\n$appid = &#8220;wx97fxxxxxxxx9bbda&#8221;;<br \/>\n$appsecret = &#8220;3efd14bcxxxxxxx38ade06cf697fc40&#8221;;<br \/>\n$url = &#8220;https:\/\/api.weixin.qq.com\/cgi-bin\/token?grant_type=client_credential&amp;appid=$appid&amp;secret=$appsecret&#8221;;<\/p>\n<p>$ch = curl_init();<br \/>\ncurl_setopt($ch, CURLOPT_URL, $url);<br \/>\ncurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);<br \/>\ncurl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);<br \/>\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br \/>\n$output = curl_exec($ch);<br \/>\ncurl_close($ch);<br \/>\n$jsoninfo = json_decode($output, true);<br \/>\n$access_token = $jsoninfo[&#8220;access_token&#8221;];<br \/>\necho $access_token;<br \/>\nexit;<br \/>\n[\/php]<\/p>\n<p>i have not finished wechat developement,will write more when i get more,so stay tuned!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week is about to end,it didn&#8217;t receive any e<\/p>\n<p><a class=\"moretag\" href=\"https:\/\/www.sunpengcheng.com\/index.php\/2016\/05\/12\/getting-started-with-wechat\/\">Continue Reading&hellip;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[33,32,31],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-wechat","tag-appid","tag-token","tag-wechat"],"_links":{"self":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":4,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":100,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/posts\/96\/revisions\/100"}],"wp:attachment":[{"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sunpengcheng.com\/index.php\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}