微信公众平台开发样例

responseMsg(); } class weChat { public function responseMsg() { $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 = " %s 0 "; if (!empty($keyword)) { $msgType = "text"; $contentStr = simsimi($keyword); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } else { echo "Input something..."; } } else { echo ""; exit; } } } function simsimi($keyword) { header("Content-type: text/html; charset=utf-8"); $content = file_get_contents("http://www.tuling123.com/openapi/api?info=".$keyword."&key=6c2cfaf7a7f088e843b550b0c5b89c26"); $data = json_decode($content); return $data->text; } ?>