php 微信公众号开发之 二 消息自动回复

MsgType) == 'event') {
        //如果是关注 subscribe事件
		
        if (strtolower($postObj->Event) == 'subscribe') {
            $toUser = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $time = time();
            $msgType = 'text';
            $content = '欢迎关注我的微信公众号: 微信公众号。';
			
			
			
 
             //修改为
              if (is_utf8($content)) {
                $content = $content;
              } else {
               $content = iconv('gb2312', 'UTF-8//IGNORE', $content);
              }
			
            $template = "
                            
                            
                            %s
                            
                            
                            ";
            $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
            echo $info;
        }
		
		
		
		
    }
	
	
	
	    if (strtolower($postObj->MsgType) == 'text') {
        //如果是关注 subscribe事件
		
       // if (strtolower($postObj->Event) == 'subscribe') {
            $toUser = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $time = time();
            $msgType = 'text';
			$keyword = trim($postObj->Content);
			
			
			
			if ($keyword=='图片'){
			 
 		 
			 
            $arr = array(  
                array(  
                    'title'=>'微信公众号',  
                    'description'=>"微信公众号的官方网站。",  
                    'picUrl'=>'http://wwwmages/logo.jpg',  
                    'url'=>'http://www.baidu.com',  
                ),  
            );    
            $template = "  
                          
                          
                        %s  
                          
                        ".count($arr)."  
                        ";  
            foreach($arr as $k=>$v){  
                $template .="  
                            <![CDATA[".$v['title']."]]>   
                              
                              
                              
                            ";  
            }  
            $template .="  
                         ";  
            echo sprintf($template, $toUser, $fromUser, time(), 'news');  
			
	        }
			
			
			
			
		    if ($keyword=='多图片'){
			 
			 
			 
			$arr = array();
			$arr[] = array("title"=>"司", "Description"=>"", "picUrl"=>"http://778.jpg", "url" =>"http://51822.html");
			$arr[] = array("title"=>"成", "Description"=>"", "picUrl"=>"http://833_28919.jpg", "url" =>"http://61929.html");
			$arr[] = array("title"=>"办", "Description"=>"", "picUrl"=>"http://8.jpg", "url" =>"http://161207.html");
			 
			 
             
            $template = "  
                          
                          
                        %s  
                          
                        ".count($arr)."  
                        ";  
            foreach($arr as $k=>$v){  
                $template .="  
                            <![CDATA[".$v['title']."]]>   
                              
                              
                              
                            ";  
            }  
            $template .="  
                         ";  
            echo sprintf($template, $toUser, $fromUser, time(), 'news');  
			
	        }
			
			
			if ($keyword=='小苹果'){
 
							
  $tetle = '小苹果';
  $des = '这是一首非常好听的歌曲';
  $MusicUrl = 'http://g.mp3';
  
  
                  $textTpl = "
				  < ![CDATA[%s] ]>
				  < ![CDATA[%s] ]>
				  %s
				  < ![CDATA[music] ]>
				  
				  < ![CDATA[$tetle] ]>
				  < ![CDATA[$des] ]>
				  < ![CDATA[$MusicUrl] ]>
				  < ![CDATA[$MusicUrl] ]>
				  < ![CDATA[media_id] ]>
				  
				  ";
                      $msgType = "music";
                 
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time);
                    echo $resultStr;
                 
  
 // $resultStr = sprintf($Musictpl, $fromUsername, $toUsername, $time, $title, $decription, $music_url, $music_url);
 // echo $resultStr;
			
			}
			
			
			
			
			
			
			
            $content = '你输入的信息是:['.$keyword.'] 当前时间是:'.date("Y-w-d h:i:sa");
			
			
			
 
             //修改为
              if (is_utf8($content)) {
                $content = $content;
              } else {
               $content = iconv('gb2312', 'UTF-8//IGNORE', $content);
              }
			
            $template = "
                            
                            
                            %s
                            
                            
                            ";
            $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
            echo $info;
       // }
		
		
		
		
    }
	
	
	
	
	
	
	
	
}


// 开发者模式绑定校验
function bindServerCheck($token) {
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];
    $tmpArr = array(
        $token,
        $timestamp,
        $nonce
    );
    sort($tmpArr);
    $tmpStr = implode($tmpArr);
    $tmpStr = sha1($tmpStr);
    if ($tmpStr == $signature) {
        return true;
    } else {
        return false;
    }
}




  function to_utf8($in) 
{ 
        if (is_array($in)) { 
            foreach ($in as $key => $value) { 
                $out[to_utf8($key)] = to_utf8($value); 
            } 
        } elseif(is_string($in)) { 
            if(mb_detect_encoding($in) != "UTF-8") 
                return utf8_encode($in); 
            else 
                return $in; 
        } else { 
            return $in; 
        } 
        return $out; 
} 




 function is_utf8($str)
{
  return preg_match('//u', $str);
}

 function music($url,$tetle,$des) {
        $MusicUrl=$url;
        $tetle=$tetle;
        $des=$des;
                $textTpl = " 
                            
                            
                            %s
                            
                             
                             <![CDATA[$tetle]]>
                             
                             
                             
                             
                             0
                             ";
                      $msgType = "music";
                 
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    echo $resultStr;
					}

?>

你可能感兴趣的:(PHP基础技术)