Micro-credit public platform development interface PHP SDK full version _android

Source: Internet
Author: User
Tags cdata sprintf

The code is as follows:

Update log:

2013-01-01 version 1.0
2014-03-15 increase in image, video, voice content reply
2014-04-09 Add menu link Events
The judgment method of revising text reply in 2014-04-10

Copy Code code as follows:



<?php


/*


Square Times Studio


CopyRight 2014 All Rights Reserved


*/

Define ("TOKEN", "Weixin");

$WECHATOBJ = new Wechatcallbackapitest ();
if (!isset ($_get[' echostr ')) {
$WECHATOBJ->responsemsg ();
}else{
$WECHATOBJ->valid ();
}

Class Wechatcallbackapitest
{
   //Authentication message
    Public Function valid ()
    {
        $echoStr = $_get["Echostr"];
        if ($this->checksignature ()) {
             Echo $echoStr;
            exit;
       }
   }

   /Check signature
    private 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) {
             return true;
       }else{
             return false;
       }
   }

Response message
Public Function responsemsg ()
{
$POSTSTR = $GLOBALS ["Http_raw_post_data"];
if (!empty ($POSTSTR)) {
$this->logger ("R". $postStr);
$POSTOBJ = simplexml_load_string ($postStr, ' simplexmlelement ', libxml_nocdata);
$RX _type = Trim ($postObj->msgtype);

Switch ($RX _type)


{


Case "Event":


$result = $this->receiveevent ($POSTOBJ);


Break


Case "Text":


$result = $this->receivetext ($POSTOBJ);


Break


Case "image":


$result = $this->receiveimage ($POSTOBJ);


Break


Case "Location":


$result = $this->receivelocation ($POSTOBJ);


Break


Case "Voice":


$result = $this->receivevoice ($POSTOBJ);


Break


Case "Video":


$result = $this->receivevideo ($POSTOBJ);


Break


Case "link":


$result = $this->receivelink ($POSTOBJ);


Break


Default


$result = "Unknow msg type:". $RX _type;


Break


}


$this->logger ("T". $result);


echo $result;


}else {


echo "";


Exit


}


}

Receive event messages


Private Function Receiveevent ($object)


{


$content = "";


Switch ($object->event)


{


Case "Subscribe":


$content = "Welcome attention to Square Times Studio";


$content. = (!empty ($object->eventkey))? ("\ n from two-dimensional code scene". Str_replace ("Qrscene_", "", $object->eventkey)): "";


Break


Case "Unsubscribe":


$content = "Cancellation of concern";


Break


Case "SCAN":


$content = "Scan Scene". $object->eventkey;


Break


Case "click":


Switch ($object->eventkey)


{


Case "Company":


$content = "Square times Studios provide Internet-related products and services." ";


Break


Default


$content = "click menu:". $object->eventkey;


Break


}


Break


Case "LOCATION":


$content = "Upload location: Latitude". $object->latitude. " Longitude ". $object->longitude;


Break


Case "VIEW":


$content = "Jump link". $object->eventkey;


Break


Default


$content = "Receive a new event:". $object->event;


Break


}


$result = $this->transmittext ($object, $content);


return $result;


}

Receive text messages


Private Function Receivetext ($object)


{


Switch ($object->content)


{


Case "Text":


$content = "This is a text message";


Break


Case "graphic":


Case "Single graphic":


$content = Array ();


$content [] = Array ("title" => "Single text caption", "Description" => "single text Content", "Picurl" => "yun_qi_img/cartoon.jpg", "Url" = > "http://m.cnblogs.com/?u=txw1958");


Break


Case "multi-text":


$content = Array ();


$content [] = Array ("title" => "multiple Text 1 titles", "Description" => "", "Picurl" => "yun_qi_img/cartoon.jpg", "Url" => " http://m.cnblogs.com/?u=txw1958 ");


$content [] = Array ("title" => "multiple graphics and Text 2 titles", "Description" => "", "Picurl =>" yun_qi_img/ F3529822720e0cf3ac9f1ada0846f21fbe09aaa3.gif "," Url "=>" http://m.cnblogs.com/?u=txw1958 ");


$content [] = Array ("title" => "multiple graphics and Text 3 titles", "Description" => "", "Picurl =>" yun_qi_img/ 18cb0a46f21fbe090d338acc6a600c338644adfd.gif "," Url "=>" http://m.cnblogs.com/?u=txw1958 ");


Break


Case "Music":


$content = Array ("Title" => "the most dazzling national wind", "Description" => "singer: Phoenix Legend", "Musicurl =>" http://121.199.4.61/music/ Zxmzf.mp3 "," Hqmusicurl "=>" Http://121.199.4.61/music/zxmzf.mp3 ");


Break


Default


$content = Date ("Y-m-d h:i:s", Time ());


Break


}


if (Is_array ($content)) {


if (Isset ($content [0][' Picurl '])) {


$result = $this->transmitnews ($object, $content);


}else if (isset ($content [' Musicurl '])) {


$result = $this->transmitmusic ($object, $content);


}


}else{


$result = $this->transmittext ($object, $content);


}


return $result;


}

Private Function Receiveimage ($object)
{
$content = Array ("MediaId" => $object->mediaid);
$result = $this->transmitimage ($object, $content);
return $result;
}

    Private Function ReceiveLocation ($object)
    {
         $content = "You are sending the position, Latitude is:". $object->location_x. " ; The longitude is: ". $object->location_y." ; The zoom level is: ". $object->scale." The location is: ". $object->label;
        $result = $this->transmittext ($object, $content);
        return $result;
   }

Private Function Receivevoice ($object)
{
if (Isset ($object->recognition) &&!empty ($object->recognition)) {
$content = "What you have just said is:". $object->recognition;
$result = $this->transmittext ($object, $content);
}else{
$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" => "", " Description "=>");
$result = $this->transmitvideo ($object, $content);
return $result;
}

Private Function Receivelink ($object)
{
$content = "You are sending a link with the title:". $object->title. " The content is: ". $object->description." , the link address is: ". $object->url;
$result = $this->transmittext ($object, $content);
return $result;
}

Private Function Transmittext ($object, $content)
{
$TEXTTPL = "<xml>
<tousername><! [cdata[%s]]></tousername>
<fromusername><! [cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><! [cdata[text]]></msgtype>
<content><! [cdata[%s]]></content>
</xml> ";
$result = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time (), $content);
return $result;
}

Private Function Transmitimage ($object, $imageArray)
{
$ITEMTPL = "<Image>
<mediaid><! [cdata[%s]]></mediaid>
</Image> ";

$item _str = sprintf ($ITEMTPL, $imageArray [' MediaId ']);

$TEXTTPL = "<xml>
<tousername><! [cdata[%s]]></tousername>
<fromusername><! [cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><! [cdata[image]]></msgtype>
$item _str
</xml> ";

$result = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time ());
return $result;
}

Private Function Transmitvoice ($object, $voiceArray)
{
$ITEMTPL = "<Voice>
<mediaid><! [cdata[%s]]></mediaid>
</Voice> ";

$item _str = sprintf ($ITEMTPL, $voiceArray [' MediaId ']);

$TEXTTPL = "<xml>
<tousername><! [cdata[%s]]></tousername>
<fromusername><! [cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><! [cdata[voice]]></msgtype>
$item _str
</xml> ";

$result = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time ());
return $result;
}

Private Function Transmitvideo ($object, $videoArray)
{
$ITEMTPL = "<Video>
<mediaid><! [cdata[%s]]></mediaid>
<thumbmediaid><! [cdata[%s]]></thumbmediaid>
<title><! [cdata[%s]]></title>
<description><! [cdata[%s]]></description>
</Video> ";

$item _str = sprintf ($ITEMTPL, $videoArray [' MediaId '], $videoArray [' Thumbmediaid '], $videoArray [' Title '], $videoArray [' Description ']);

$TEXTTPL = "<xml>
<tousername><! [cdata[%s]]></tousername>
<fromusername><! [cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><! [cdata[video]]></msgtype>
$item _str
</xml> ";

$result = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time ());
return $result;
}

Private Function Transmitnews ($object, $newsArray)


{


if (!is_array ($newsArray)) {


Return


}


$ITEMTPL = "&lt;item&gt;


&lt;title&gt;&lt;! [cdata[%s]]&gt;&lt;/title&gt;


&lt;description&gt;&lt;! [cdata[%s]]&gt;&lt;/description&gt;


&lt;picurl&gt;&lt;! [cdata[%s]]&gt;&lt;/picurl&gt;


&lt;url&gt;&lt;! [cdata[%s]]&gt;&lt;/url&gt;


&lt;/item&gt;


";


$item _str = "";


foreach ($newsArray as $item) {


$item _str. = sprintf ($ITEMTPL, $item [' Title '], $item [' Description '], $item [' Picurl '], $item [' Url ']);


}


$NEWSTPL = "&lt;xml&gt;


&lt;tousername&gt;&lt;! [cdata[%s]]&gt;&lt;/tousername&gt;


&lt;fromusername&gt;&lt;! [cdata[%s]]&gt;&lt;/fromusername&gt;


&lt;CreateTime&gt;%s&lt;/CreateTime&gt;


&lt;msgtype&gt;&lt;! [cdata[news]]&gt;&lt;/msgtype&gt;


&lt;content&gt;&lt;! [cdata[]]&gt;&lt;/content&gt;


&lt;ArticleCount&gt;%s&lt;/ArticleCount&gt;


&lt;Articles&gt;


$item _str&lt;/articles&gt;


&lt;/xml&gt; ";

$result = sprintf ($NEWSTPL, $object->fromusername, $object->tousername, Time (), Count ($newsArray));
return $result;
}

Private Function Transmitmusic ($object, $musicArray)
{
$ITEMTPL = "<Music>
<title><! [cdata[%s]]></title>
<description><! [cdata[%s]]></description>
<musicurl><! [cdata[%s]]></musicurl>
</Music> ";

$item _str = sprintf ($ITEMTPL, $musicArray [' Title '], $musicArray [' Description '], $musicArray [' Musicurl '], $musicArray [' Hqmusicurl ']);

$TEXTTPL = "<xml>
<tousername><! [cdata[%s]]></tousername>
<fromusername><! [cdata[%s]]></fromusername>
<CreateTime>%s</CreateTime>
<msgtype><! [cdata[music]]></msgtype>
$item _str
</xml> ";

$result = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time ());
return $result;
}

Private Function Logger ($log _content)


{


if (Isset ($_server[' http_appname ')) {//sae


Sae_set_display_errors (FALSE);


Sae_debug ($log _content);


Sae_set_display_errors (TRUE);


}else if ($_server[' remote_addr ']!= "127.0.0.1") {//local


$max _size = 10000;


$log _filename = "Log.xml";


if (file_exists ($log _filename) and (ABS (FileSize ($log _filename)) &gt; $max _size)) {unlink ($log _filename);}


File_put_contents ($log _filename, date (' H:i:s '). " ". $log _content." \ r \ n ", file_append);


}


}


}


?&gt;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.