搜索
首页后端开发php教程微信开发 - syntax error, unexpected T_AS in example.php on line 211?

放到SAE中 ,请问这是怎么回事? 出错地方如下:

<code>$foreach($arr_item as $item) {
        $item_str .=sprintf($itemTpl,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']);
    }
</code>

代码贴到下面,望各位大神解决这个问题!

<code><?php //define your token 
define("TOKEN", "weixin");//这里填写的是你在微信上设置的TOKEN,但是必须保证与微信公众平台 接口配置信息一致 
$wechatObj = new wechatCallbackapiTest(); 
$wechatObj->valid();//这里是测试网站配置信息和开发的是否一致。

$wechatObj->responseMsg();//新增加这一项,目的是调用responseMsg()这个功能。



class wechatCallbackapiTest 
{

//若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,否则接入失败。

public function valid() 
    { 
        $echoStr = $_GET["echostr"]; 


        //alid signature , option 
        if($this->checkSignature()){ 
        echo $echoStr; 
        exit; 
        } 
    }

 public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            switch ($RX_TYPE)
            {
                case "text":
                    $resultStr = $this->receiveText($postObj);
                    break;
                case "image":
                    $resultStr = $this->receiveImage($postObj);
                    break;
                case "location":
                    $resultStr = $this->receiveLocation($postObj);
                    break;
                case "voice":
                    $resultStr = $this->receiveVoice($postObj);
                    break;
                case "video":
                    $resultStr = $this->receiveVideo($postObj);
                    break;
                case "link":
                    $resultStr = $this->receiveLink($postObj);
                    break;
                case "event":
                    $resultStr = $this->receiveEvent($postObj);
                    break;
                default:
                    $resultStr = "unknow msg type: ".$RX_TYPE;
                    break;
            }
            echo $resultStr;
        }else {
            echo "";
            exit;
        }
    }
    /*文本消息->1*/
    private function receiveText($object){
        $keyword = trim($object->Content);
        if(!empty($keyword)){
         $url = "http://www.tuling123.com/openapi/api?key=a2fcd4181d8aa942ca2e078b1c479684&info=".$keyword;
         $data = file_get_contents($url);
         $text =json_decode($data,ture);
         $content = $text['text'];
         $result =$this->transmitText($object,$content);
        }
        else if($keyword=="图文"||$keyword=="单图文"){
            $content = array();
            $content[]=array("Title"=>"单图文标题",
                             "Descrption"=>"单图文内容",
                             "PicUrl"=>"http://v1.qzone.cc/pic/201602/12/15/12/56bd85d5cb22d067.jpg%21600x600.jpg",
                             "Url"=>"http://m.cnblogs.com/?u=txw1958");
            $result = $this->transmitNews($object,$content);
        }
        else if($keyword=="多图文"){
            $content = array();
            $content[]=array("Title"=>"多图文标题1",
                             "Descrption"=>"图文内容",
                             "PicUrl"=>"http://v1.qzone.cc/pic/201602/12/15/12/56bd85d5cb22d067.jpg%21600x600.jpg",
                             "Url"=>"http://m.cnblogs.com/?u=txw1958");
            $content[]=array("Title"=>"多图文标题2",
                             "Descrption"=>"图文内容",
                             "PicUrl"=>"http://v1.qzone.cc/pic/201602/12/15/12/56bd85d5cb22d067.jpg%21600x600.jpg",
                             "Url"=>"http://m.cnblogs.com/?u=txw1958");
            $content[]=array("Title"=>"多图文标题3",
                             "Descrption"=>"图文内容",
                             "PicUrl"=>"http://v1.qzone.cc/pic/201602/12/15/12/56bd85d5cb22d067.jpg%21600x600.jpg",
                             "Url"=>"http://m.cnblogs.com/?u=txw1958");
            $result = $this->transmitNews($object,$content);
        }else if($keyword=="音乐"){
            $content = array("Title"=>"七月上",
                             "Descrption"=>"jam",
                             "MusicUrl"=>"http://sc1.111ttt.com/2015/5/12/28/105281351446.mp3",
                             "HQMusicUrl"=>"http://sc1.111ttt.com/2015/5/12/28/105281351446.mp3");
            $result = $this->transmitMusic($object,$content);
        }
        return $result;
    }
    /*图片消息->1*/
    private function receiveImage($object){
            $content=array("MediaId"=>$object->MediaId);
            $result = $this->transmitImage($object,$content);
            return $result;
    }
    /*回复语音消息->1*/
    private function receiveVoice($object){
            $content=array("MediaId"=>$object->MediaId);
            $result =$this->transmitVoice($object,$content);
            return $result;
    }
    /*回复视频消息->*/
    private function receiveVideo($object){
        $content=array("MediaId"=>$object->MediaId,
                        "ThumbMediaId"=>$object->ThumbMediaId,
                        "Title"=>"",
                        "Descrption"=>"");
        $result=$this->transmitVideo($object,$content);
        return $result;
    }
    /*回复文本消息->xml*/
    private function transmitText($object,$content){
        $textTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    <content></content>
                    </xml>";
        $result = sprintf($textTpl,$object->FromUserName,$object->ToUserName,time(),$content);
        return $result;
    }
    /*回复图片消息xml*/
    private function transmitImage($object,$imageArray){
        $itemTpl = "<image>
                    <mediaid></mediaid>
                    </image>";
        $item_str = sprintf($itemTpl,$imageArray['MediaId']);
        $textTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    $item_str
                    </xml>";
        $result = sprintf($textTpl,$object->FromUserName,$object->ToUserName,time());
        return $result;
    }
    /*回复语音消息xml*/
    private function transmitVoice($object,$voiceArray){
        $itemTpl = "<voice>
                    <mediaid></mediaid>
                    </voice>";
        $item_str = sprintf($itemTpl,$voiceArray['MediaId']);
        $textTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    $item_str
                    </xml>";
        $result = sprintf($textTpl,$object->FromUserName,$object->ToUserName,time());
        return $result;
    }
    /*回复视频消息Xml*/
    private function transmitVideo($object,$videoArray){
        $itemTpl = "<video>
                    <mediaid></mediaid>
                    <thumbmediaid></thumbmediaid>
                    <title></title>
                    <descrption></descrption>
                    </video>";
        $item_str = sprintf($itemTpl,$videoArray['MediaId'],$videoArray['ThumbMediaId'],$videoArray['Title'],$videoArray['Descrption']);
        $textTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    $item_str
                    </xml>";
        $result = sprintf($textTpl,$object->FromUserName,$object->ToUserName,time());
        return $result;
    }
    /*回复图文消息xml*/
    private function transmitNews($object,$arr_item){
        if(!is_array($arr_item))
            return;
        $itemTpl = "<item>
                    <title></title> 
                    <description></description>
                    <picurl></picurl>
                    <url></url>
                    </item>";
        $item_str = "";
        $foreach($arr_item as $item) {
            $item_str .=sprintf($itemTpl,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']);
        }
        $newsTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    <articlecount>%s</articlecount>
                    <articles>
                    $item_str
                    </articles>
                    </xml>";
        $result = sprintf($newsTpl,$object->FromUserName,$object->ToUserName,time(),count($arr_item));
        return $result;
    }
    /*回复音乐xml*/
    private function transmitMusic($object,$musicArray){
        $itemTpl = "<music>
                    <title></title>
                    <description></description>
                    <musicurl></musicurl>
                    <hqmusicurl></hqmusicurl>
                    
                    </music>";
        $item_str = sprintf($itemTpl,$musicArray['Title'],$musicArray['Description'],$musicArray['MusicUrl'],$musicArray['HQMusicUrl']);
        $textTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    $item_str
                    </xml>";
        $result=sprintf($textTpl,$object->FromUserName,$object->ToUserName,time());
        return $result;
    }



// 开发者通过检验signature对请求进行校验 
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; 
            } 
        } 
} 


?> </code>

回复内容:

放到SAE中 ,请问这是怎么回事? 出错地方如下:

<code>$foreach($arr_item as $item) {
        $item_str .=sprintf($itemTpl,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']);
    }
</code>

代码贴到下面,望各位大神解决这个问题!

<code><?php //define your token 
define("TOKEN", "weixin");//这里填写的是你在微信上设置的TOKEN,但是必须保证与微信公众平台 接口配置信息一致 
$wechatObj = new wechatCallbackapiTest(); 
$wechatObj->valid();//这里是测试网站配置信息和开发的是否一致。

$wechatObj->responseMsg();//新增加这一项,目的是调用responseMsg()这个功能。



class wechatCallbackapiTest 
{

//若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,否则接入失败。

public function valid() 
    { 
        $echoStr = $_GET["echostr"]; 


        //alid signature , option 
        if($this->checkSignature()){ 
        echo $echoStr; 
        exit; 
        } 
    }

 public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            switch ($RX_TYPE)
            {
                case "text":
                    $resultStr = $this->receiveText($postObj);
                    break;
                case "image":
                    $resultStr = $this->receiveImage($postObj);
                    break;
                case "location":
                    $resultStr = $this->receiveLocation($postObj);
                    break;
                case "voice":
                    $resultStr = $this->receiveVoice($postObj);
                    break;
                case "video":
                    $resultStr = $this->receiveVideo($postObj);
                    break;
                case "link":
                    $resultStr = $this->receiveLink($postObj);
                    break;
                case "event":
                    $resultStr = $this->receiveEvent($postObj);
                    break;
                default:
                    $resultStr = "unknow msg type: ".$RX_TYPE;
                    break;
            }
            echo $resultStr;
        }else {
            echo "";
            exit;
        }
    }
    /*文本消息->1*/
    private function receiveText($object){
        $keyword = trim($object->Content);
        if(!empty($keyword)){
         $url = "http://www.tuling123.com/openapi/api?key=a2fcd4181d8aa942ca2e078b1c479684&info=".$keyword;
         $data = file_get_contents($url);
         $text =json_decode($data,ture);
         $content = $text['text'];
         $result =$this->transmitText($object,$content);
        }
        else if($keyword=="图文"||$keyword=="单图文"){
            $content = array();
            $content[]=array("Title"=>"单图文标题",
                             "Descrption"=>"单图文内容",
                             "PicUrl"=>"http://v1.qzone.cc/pic/201602/12/15/12/56bd85d5cb22d067.jpg%21600x600.jpg",
                             "Url"=>"http://m.cnblogs.com/?u=txw1958");
            $result = $this->transmitNews($object,$content);
        }
        else if($keyword=="多图文"){
            $content = array();
            $content[]=array("Title"=>"多图文标题1",
                             "Descrption"=>"图文内容",
                             "PicUrl"=>"http://v1.qzone.cc/pic/201602/12/15/12/56bd85d5cb22d067.jpg%21600x600.jpg",
                             "Url"=>"http://m.cnblogs.com/?u=txw1958");
            $content[]=array("Title"=>"多图文标题2",
                             "Descrption"=>"图文内容",
                             "PicUrl"=>"http://v1.qzone.cc/pic/201602/12/15/12/56bd85d5cb22d067.jpg%21600x600.jpg",
                             "Url"=>"http://m.cnblogs.com/?u=txw1958");
            $content[]=array("Title"=>"多图文标题3",
                             "Descrption"=>"图文内容",
                             "PicUrl"=>"http://v1.qzone.cc/pic/201602/12/15/12/56bd85d5cb22d067.jpg%21600x600.jpg",
                             "Url"=>"http://m.cnblogs.com/?u=txw1958");
            $result = $this->transmitNews($object,$content);
        }else if($keyword=="音乐"){
            $content = array("Title"=>"七月上",
                             "Descrption"=>"jam",
                             "MusicUrl"=>"http://sc1.111ttt.com/2015/5/12/28/105281351446.mp3",
                             "HQMusicUrl"=>"http://sc1.111ttt.com/2015/5/12/28/105281351446.mp3");
            $result = $this->transmitMusic($object,$content);
        }
        return $result;
    }
    /*图片消息->1*/
    private function receiveImage($object){
            $content=array("MediaId"=>$object->MediaId);
            $result = $this->transmitImage($object,$content);
            return $result;
    }
    /*回复语音消息->1*/
    private function receiveVoice($object){
            $content=array("MediaId"=>$object->MediaId);
            $result =$this->transmitVoice($object,$content);
            return $result;
    }
    /*回复视频消息->*/
    private function receiveVideo($object){
        $content=array("MediaId"=>$object->MediaId,
                        "ThumbMediaId"=>$object->ThumbMediaId,
                        "Title"=>"",
                        "Descrption"=>"");
        $result=$this->transmitVideo($object,$content);
        return $result;
    }
    /*回复文本消息->xml*/
    private function transmitText($object,$content){
        $textTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    <content></content>
                    </xml>";
        $result = sprintf($textTpl,$object->FromUserName,$object->ToUserName,time(),$content);
        return $result;
    }
    /*回复图片消息xml*/
    private function transmitImage($object,$imageArray){
        $itemTpl = "<image>
                    <mediaid></mediaid>
                    </image>";
        $item_str = sprintf($itemTpl,$imageArray['MediaId']);
        $textTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    $item_str
                    </xml>";
        $result = sprintf($textTpl,$object->FromUserName,$object->ToUserName,time());
        return $result;
    }
    /*回复语音消息xml*/
    private function transmitVoice($object,$voiceArray){
        $itemTpl = "<voice>
                    <mediaid></mediaid>
                    </voice>";
        $item_str = sprintf($itemTpl,$voiceArray['MediaId']);
        $textTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    $item_str
                    </xml>";
        $result = sprintf($textTpl,$object->FromUserName,$object->ToUserName,time());
        return $result;
    }
    /*回复视频消息Xml*/
    private function transmitVideo($object,$videoArray){
        $itemTpl = "<video>
                    <mediaid></mediaid>
                    <thumbmediaid></thumbmediaid>
                    <title></title>
                    <descrption></descrption>
                    </video>";
        $item_str = sprintf($itemTpl,$videoArray['MediaId'],$videoArray['ThumbMediaId'],$videoArray['Title'],$videoArray['Descrption']);
        $textTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    $item_str
                    </xml>";
        $result = sprintf($textTpl,$object->FromUserName,$object->ToUserName,time());
        return $result;
    }
    /*回复图文消息xml*/
    private function transmitNews($object,$arr_item){
        if(!is_array($arr_item))
            return;
        $itemTpl = "<item>
                    <title></title> 
                    <description></description>
                    <picurl></picurl>
                    <url></url>
                    </item>";
        $item_str = "";
        $foreach($arr_item as $item) {
            $item_str .=sprintf($itemTpl,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']);
        }
        $newsTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    <articlecount>%s</articlecount>
                    <articles>
                    $item_str
                    </articles>
                    </xml>";
        $result = sprintf($newsTpl,$object->FromUserName,$object->ToUserName,time(),count($arr_item));
        return $result;
    }
    /*回复音乐xml*/
    private function transmitMusic($object,$musicArray){
        $itemTpl = "<music>
                    <title></title>
                    <description></description>
                    <musicurl></musicurl>
                    <hqmusicurl></hqmusicurl>
                    
                    </music>";
        $item_str = sprintf($itemTpl,$musicArray['Title'],$musicArray['Description'],$musicArray['MusicUrl'],$musicArray['HQMusicUrl']);
        $textTpl = "<xml>
                    <tousername></tousername>
                    <fromusername></fromusername>
                    <createtime>%s</createtime>
                    <msgtype></msgtype>
                    $item_str
                    </xml>";
        $result=sprintf($textTpl,$object->FromUserName,$object->ToUserName,time());
        return $result;
    }



// 开发者通过检验signature对请求进行校验 
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; 
            } 
        } 
} 


?> </code>

foreach不是变量,为什么前面有个$

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
优化PHP代码:减少内存使用和执行时间优化PHP代码:减少内存使用和执行时间May 10, 2025 am 12:04 AM

TOOPTIMIZEPHPCODEFORDUSEMEMORYUSAGEAGEAGEAGEAGEAGEANDEXECUTITIEM,关注台词:1)USEREEREFERESCENCENCINCOPYINSTEADOFCOPYINGINATATASTRUCTURESTROUCTURESTOREDUCEMORYCONSUMPTION.2)杠杆phphppphpphp'sbuilt intimpunctionslikearray_mapforfunctionslikearray_mapforfforfforfforfasterapasterexecution.3)

PHP电子邮件:分步发送指南PHP电子邮件:分步发送指南May 09, 2025 am 12:14 AM

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自动化notifications andMarketingCampaigns.1)设置设置yourphpenvironcormentswironmentswithaweberswithawebserverserverserverandphp,确保themailfunctionisenabled.2)useabasicscruct

如何通过PHP发送电子邮件:示例和代码如何通过PHP发送电子邮件:示例和代码May 09, 2025 am 12:13 AM

发送电子邮件的最佳方法是使用PHPMailer库。1)使用mail()函数简单但不可靠,可能导致邮件进入垃圾邮件或无法送达。2)PHPMailer提供更好的控制和可靠性,支持HTML邮件、附件和SMTP认证。3)确保正确配置SMTP设置并使用加密(如STARTTLS或SSL/TLS)以增强安全性。4)对于大量邮件,考虑使用邮件队列系统来优化性能。

高级PHP电子邮件:自定义标题和功能高级PHP电子邮件:自定义标题和功能May 09, 2025 am 12:13 AM

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP发送电子邮件的指南使用PHP和SMTP发送电子邮件的指南May 09, 2025 am 12:06 AM

使用PHP和SMTP发送邮件可以通过PHPMailer库实现。1)安装并配置PHPMailer,2)设置SMTP服务器细节,3)定义邮件内容,4)发送邮件并处理错误。使用此方法可以确保邮件的可靠性和安全性。

使用PHP发送电子邮件的最佳方法是什么?使用PHP发送电子邮件的最佳方法是什么?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

PHP中依赖注入的最佳实践PHP中依赖注入的最佳实践May 08, 2025 am 12:21 AM

使用依赖注入(DI)的原因是它促进了代码的松耦合、可测试性和可维护性。1)使用构造函数注入依赖,2)避免使用服务定位器,3)利用依赖注入容器管理依赖,4)通过注入依赖提高测试性,5)避免过度注入依赖,6)考虑DI对性能的影响。

PHP性能调整技巧和技巧PHP性能调整技巧和技巧May 08, 2025 am 12:20 AM

phperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovesponsemetimes.2)优化

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具