| 
 微信公众平台初始用 
发布时间:2015年1月22日 作者:未知 查看次数:1890 
微信公众平台初始用今天测试微信php自动回复功能,从微信开发者文档下载的wx_sample.php修改后却始终不能成功执行,代码如下: <?php /** * wechat php test */ 
 //define your token define("TOKEN", "weixin2015"); $wechatObj = new wechatCallbackapiTest(); //$wechatObj->valid(); $wechatObj->responseMsg(); 
 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)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> 
 if(!empty( $keyword )) { $msgType = "text"; $contentStr = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; exit; }else{ echo "Input something..."; } 
 }else { echo ""; exit; } } 
 private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } 
 $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; 
 $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); 
 if( $tmpStr == $signature ){ return true; }else{ return false; } } } 
 ?> ---------------------------------------------------- ---------------------------------------------------- class wechatCallbackapiTest         //valid signature , option     public function responseMsg()         }else { ?> 可发现回复的中文却是乱码。 file_get_contents("php://input")和$GLOBALS["HTTP_RAW_POST_DATA"]的区别吗? ---------------------------------------------------- http://blog.csdn.net/molaifeng/article/details/40107813 最近在开发微信接口,又学到了一些新的技术点,今天就把学到的关于接收数据的技术点给简单的罗列下。 
 
 
 public function __construct($token, $wxuser = ''){
        $this -> auth($token, $wxuser) || exit;
        if(IS_GET){
            echo($_GET['echostr']);
            exit;
        }else{
            $xml = file_get_contents("php://input");
            $xml = new SimpleXMLElement($xml);
            $xml || exit;
            foreach ($xml as $key => $value){
                $this -> data[$key] = strval($value);
            }
        }
    }
 上述代码是截取的一个片段,意思为把接收到的微信传过来的xml解析为数组。其中有一处file_get_contents('php://input'),后经查证,微信给开发者账号填写的url发送的是xml数据,但PHP默认只识别application/x-www.form-urlencoded标准的数据类型,对text/xml的内容无法解析为$_POST数组,因此会保留原型,可以交给file_get_contents(‘php://input’)接收,也可以用$GLOBALS['HTTP_RAW_POST_DATA']。 如,传过来的xml为 
 <xml> <ToUserName><![CDATA[ebyben1413005325]]></ToUserName> <FromUserName><![CDATA[ga_6281708af4c6]]></FromUserName> <CreateTime>1413341614</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[首页]]></Content> <MsgId>1234567890123456</MsgId> 
 
 Array
(
    [ToUserName] => ebyben1413005325
    [FromUserName] => ga_6281708af4c6
    [CreateTime] => 1413341614
    [MsgType] => text
    [Content] => 首页
    [MsgId] => 1234567890123456
)
 php://input 允许读取 POST 的原始数据。和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小,并且不需要任何特殊的 php.ini 设置。同时两者皆不能用于接收enctype="multipart/form-data"形式的数据。 最后再说下$_SERVER['REQUEST_METHOD']这个变量,用过ThinkPHP的朋友应该在代码中使用类似IS_GET、IS_AJAX这种代码吧,追溯下源码,就可以看到 
 define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']);
define('IS_GET',        REQUEST_METHOD =='GET' ? true : false);
define('IS_POST',       REQUEST_METHOD =='POST' ? true : false);
define('IS_PUT',        REQUEST_METHOD =='PUT' ? true : false);
define('IS_DELETE',     REQUEST_METHOD =='DELETE' ? true : false);
define('IS_AJAX',       ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') || !empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')])) ? true : false);
 
  | 
|
	
  |