首页 > 代码库 > 微信开发
微信开发
一、打通微信与服务器之间的通讯
https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
编辑index.php
<?php define(‘TOKEN‘,‘yida‘); $obj = new weixin(); if(!isset($_GET[‘echostr‘])) { $obj->receive(); }else{ $obj->checkSignature(); } class weixin{ /** * 验证服务器与微信间的通讯 * @return [type] [description] */ public 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 ){ echo $_GET[‘echostr‘]; }else{ return false; } } }
二、接收微信发送的消息及记录日志
/** * 接收微信的文本信息 * @return [type] [description] */ public function receive(){ //微信接收的全局函数 $obj = $GLOBALS[‘HTTP_RAW_POST_DATA‘]; //将xml转换为对象(LIBXML_NOCDATA 是忽略SimpleXMLElement类中的CDATA) $xmlobj = simplexml_load_file($obj,‘SimpleXMLELement‘,LIBXML_NOCDATA); $this->addlog("接收:".$obj); } /** * 记录日志 * @param [type] $content [要记录的内容] */ private function addlog($content){ $logsize = 100000; $log = "log.txt"; //判断文件是否存在及文件大小 if(file_exists($log) && filesize($log)>$logsize){ unlink($log); } //追加的方式写入 file_put_contents($log,date(‘H:i:s‘)." ".$content.‘\n‘,FILE_APPEND); }
三、回复微信的发送信息
/** * 接收微信的文本信息 * @return [type] [description] */ public function receive(){ //微信接收的全局函数 $obj = $GLOBALS[‘HTTP_RAW_POST_DATA‘]; //将xml转换为对象(LIBXML_NOCDATA 是忽略SimpleXMLElement类中的CDATA) $xmlobj = simplexml_load_string($obj,‘SimpleXMLElement‘,LIBXML_NOCDATA); $this->addlog("接收:".$xmlobj); if(!empty($xmlobj)){ switch (trim( $xmlobj->MsgType )){ case ‘text‘: $result = $this->receiveText($xmlobj); if(!empty($result)){ echo $result; }else{ $xml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($xml,$xmlobj->FromUserName,$xmlobj->ToUserName,time(),$xmlobj->MsgType,‘没有这条消息‘); echo $result; }
break; } } } /** * 回复微信内容 * @param [type] $xmlobj [description] * @return [type] [description] */ private function receiveText($xmlobj){ $content = trim($xmlobj->Content); if(strstr($content,‘你好‘)){ $xml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($xml,$xmlobj->FromUserName,$xmlobj->ToUserName,time(),$xmlobj->MsgType,‘hello‘); return $result; } }
四、微信回复单图文消息
/** * 回复微信内容 * @param [type] $xmlobj [description] * @return [type] [description] */ private function receiveText($xmlobj){ $content = trim($xmlobj->Content); if(strstr($content,‘你好‘)){ $xml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($xml,$xmlobj->FromUserName,$xmlobj->ToUserName,time(),$xmlobj->MsgType,‘hello‘); }else if(strstr($content,‘单图文‘)){ $result = $this->receiveImage($xmlobj); } return $result; } /** * 发送单图文消息 * @param [type] $xmlobj [description] * @return [type] [description] */ private function receiveImage($xmlobj){ $xml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>1</ArticleCount> <Articles> <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> </Articles> </xml> "; $result = sprintf($xml,$xmlobj->FromUserName,$xmlobj->ToUserName,time(),‘news‘,‘学习微信开发‘,‘新版《规定》明确将各类新媒体纳入管理范畴, 指出通过互联网站、应用程序、论坛、博客、微博客、公众账号、即时通信工具、网络直播等形式向社会公众提供互联网新闻信息服务‘,‘http://cms-bucket.nosdn.127.net/53164193c75f4fee90992cbe9c17086c20170503095700.jpeg?imageView&thumbnail=380y187&quality=85‘,‘http://www.baidu.com‘); return $result; }
五、微信回复多图文消息
/** * 回复微信内容 * @param [type] $xmlobj [description] * @return [type] [description] */ private function receiveText($xmlobj){ $content = trim($xmlobj->Content); if(strstr($content,‘你好‘)){ $xml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($xml,$xmlobj->FromUserName,$xmlobj->ToUserName,time(),$xmlobj->MsgType,‘hello‘); }else if(strstr($content,‘单图文‘)){ $result = $this->receiveImage($xmlobj); }else if(strstr($content,‘多图文‘)){ $result = $this->receiveImages($xmlobj); } return $result; } /** * 发送多图文消息 * @param [type] $xmlobj [description] * @return [type] [description] */ private function receiveImages($xmlobj){ $content = array(); $content[] =array( ‘Title‘=>‘学习微信开发‘, ‘Description‘=>‘学习微信开发‘,‘新版《规定》明确将各类新媒体纳入管理范畴, 指出通过互联网站、应用程序、论坛、博客、微博客、公众账号、即时通信工具、网络直播等形式向社会公众提供互联网新闻信息服务‘, ‘PicUrl‘ => ‘http://cms-bucket.nosdn.127.net/53164193c75f4fee90992cbe9c17086c20170503095700.jpeg?imageView&thumbnail=380y187&quality=85‘, ‘Url‘ =>‘http://www.baidu.com‘ ); $content[] =array( ‘Title‘=>‘学习微信开发‘, ‘Description‘=>‘学习微信开发‘,‘新版《规定》明确将各类新媒体纳入管理范畴, 指出通过互联网站、应用程序、论坛、博客、微博客、公众账号、即时通信工具、网络直播等形式向社会公众提供互联网新闻信息服务‘, ‘PicUrl‘ => ‘http://cms-bucket.nosdn.127.net/53164193c75f4fee90992cbe9c17086c20170503095700.jpeg?imageView&thumbnail=380y187&quality=85‘, ‘Url‘ =>‘http://www.baidu.com‘ ); $content[] =array( ‘Title‘=>‘学习微信开发‘, ‘Description‘=>‘学习微信开发‘,‘新版《规定》明确将各类新媒体纳入管理范畴, 指出通过互联网站、应用程序、论坛、博客、微博客、公众账号、即时通信工具、网络直播等形式向社会公众提供互联网新闻信息服务‘, ‘PicUrl‘ => ‘http://cms-bucket.nosdn.127.net/53164193c75f4fee90992cbe9c17086c20170503095700.jpeg?imageView&thumbnail=380y187&quality=85‘, ‘Url‘ =>‘http://www.baidu.com‘ ); $str = ‘<item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item>‘; $news = ""; foreach ($content as $newArray) { $news .= sprintf($str,$newArray[‘Title‘],$newArray[‘Description‘],$newArray[‘PicUrl‘],$newArray[‘Url‘]); } $xml = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <ArticleCount>%s</ArticleCount> <Articles> $news </Articles> </xml> "; $result = sprintf($xml,$xmlobj->FromUserName,$xmlobj->ToUserName,time(),‘news‘,count($content)); return $result; }
微信开发
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。