新浪开发者与微信相结合的成果
前段时间内微信公众账号挺火的,我也试着注册了一个,然后利用手中的资源开发了一个关于
自己学校的百事通。
1、关于微信公众账号怎么与自己的服务器(这里使用sae)建立连接,大家在微信公众账号上可以看到帮助文档
我们微信发出消息首先是先发给微信所处服务器,然后微信又通过以下代码与SAE建立连接
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; } }
1、当收到一个消息时,我用以下代码对其进行解析并回复
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 WISCNU!我爱华师\n1、查电话请输入[2012*****奥巴马电话]数字为你自己学号\n2、查课表直接输入模糊课程名或者模糊名字\n"; $db_op=new database("3306","localhost","root","123456","caosl","ccc","GBK"); if (!$db_op->conn) { die('Could not connect: ');// . mysql_error()); } $pos=stripos($keyword,"电话"); $pos1=stripos($keyword,"天气"); $pos2=stripos($keyword,"中通"); $stuid=mb_substr($keyword,0,10); $city=mb_substr($keyword,0,$pos1); $postid=mb_substr($keyword,0,$pos2); $name=mb_substr($keyword,10,$pos-10); if($pos>0) { $sql = "select * from telphone where name like '%".$name."%'"; $sql1 = "select * from telphone where stuid='".$stuid."'"; $result1=$db_op->query($sql1); if(mysql_num_rows($result1)>0){ $result=$db_op->query($sql); $contentStr=''; while($row=$db_op->fetch_array($result)) { $contentStr="ta的电话是".$row["telnum"]; } } } else if($pos1>0) { $cookie_jar = tempnam('/tmp','cookie'); $post_data = array( 'city' => $city, 'dpc' =>'1', ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://php.weather.sina.com.cn/search.php'); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); $output = curl_exec($ch); if ($output === FALSE) { echo "cURL Error: " . curl_error($ch); } $output=mb_convert_encoding($output, "UTF-8", "gb2312"); preg_match_all("|<li\s*.*>\s*.*<\/li>|U",$output,$arr); // $contentStr=''; $span1=mb_substr($arr[0][10],stripos($arr[0][10],'>')+1,stripos($arr[0][10],'span></')); $span2=mb_substr($arr[0][11],stripos($arr[0][11],'>'),stripos($arr[0][11],'</')); $span3=mb_substr($arr[0][12],stripos($arr[0][12],'>'),stripos($arr[0][12],'</')); $span4=mb_substr($arr[0][13],stripos($arr[0][13],'>')+1,stripos($arr[0][13],'span></')); $span5=mb_substr($arr[0][14],stripos($arr[0][14],'>'),stripos($arr[0][14],'</')); $span6=mb_substr($arr[0][15],stripos($arr[0][15],'>'),stripos($arr[0][15],'</')); preg_match_all("|>\s*.*<|U",$span1,$a); preg_match_all("|>\s*.*<|U",$span4,$b); $contentStr='白天天气'." ".mb_substr($a[0][0],1,strlen($a[0][0])-2)." ".mb_substr($a[0][2],1,strlen($a[0][2])-2)." ".mb_substr($a[0][4],1,strlen($a[0][4])-2)." ".$span2.$span3."\n"; $contentStr=$contentStr.'夜间天气'.mb_substr($b[0][0],1,strlen($b[0][0])-2).mb_substr($b[0][2],1,strlen($b[0][2])-2).mb_substr($b[0][4],1,strlen($b[0][4])-2).$span5.$span6."\n"; //print_r($arr); curl_close($ch); } else if($pos2>0) { $cookie_jar = tempnam('/tmp','cookie'); $post_data = array( 'txtbill' => $postid, ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.zto.cn/bill.aspx'); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); $output = curl_exec($ch); if ($output === FALSE) { echo "cURL Error: " . curl_error($ch); } $output=mb_convert_encoding($output, "UTF-8", "gb2312"); $string = preg_replace( "/(<[^>]*>)/", ' ', $output); $string = preg_replace ( "/\n(\s*\n)+/", ' ', $string ); $contentStr=mb_substr($string,stripos($string,'时间'),stripos($string,'alimama_pid')-stripos($string,'时间')); curl_close($ch); } else{ $sql = "select distinct * from classinfo where classname like '%".$keyword."%' or classstu like '%".$keyword."%'";
$result=$db_op->query($sql); if(mysql_num_rows($result)>0){ $contentStr=''; $index=0; while($row=$db_op->fetch_array($result)) { $index+=1; $contentStr=$contentStr.$index.'-'.$row["classtime"].'|'.$row["classroom"].'|'.$row["classname"]."\n".$row["classstu"]."\n"; } } } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } }
为了安全起见,查电话需要输入本人学号,但是查课程可以不用输入学号