微信公众平台开发接口PHP SDK完整版(转载)

<?php/*    方倍工作室 http://www.fangbei.org/    CopyRight 2015 All Rights Reserved*/

define("TOKEN", "weixin");

$wechatObj = new wechatCallbackapiTest();if (!isset($_GET[‘echostr‘])) {    $wechatObj->responseMsg();}else{    $wechatObj->valid();}

class wechatCallbackapiTest{    //验证签名    public function valid()    {        $echoStr = $_GET["echostr"];        $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 $echoStr;            exit;        }    }

    //响应消息    public function responseMsg()    {        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];        if (!empty($postStr)){            $this->logger("R \r\n".$postStr);            $postObj = simplexml_load_string($postStr, ‘SimpleXMLElement‘, LIBXML_NOCDATA);            $RX_TYPE = trim($postObj->MsgType);

            if (($postObj->MsgType == "event") && ($postObj->Event == "subscribe" || $postObj->Event == "unsubscribe")){                //过滤关注和取消关注事件            }else{                            }                        //消息类型分离            switch ($RX_TYPE)            {                case "event":                    $result = $this->receiveEvent($postObj);                    break;                case "text":                   if (strstr($postObj->Content, "第三方")){                        $result = $this->relayPart3("http://www.fangbei.org/test.php".‘?‘.$_SERVER[‘QUERY_STRING‘], $postStr);                    }else{                        $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 = "unknown msg type: ".$RX_TYPE;                    break;            }            $this->logger("T \r\n".$result);            echo $result;        }else {            echo "";            exit;        }    }

    //接收事件消息    private function receiveEvent($object)    {        $content = "";        switch ($object->Event)        {            case "subscribe":                $content = "欢迎关注方倍工作室 ";                $content .= (!empty($object->EventKey))?("\n来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):"";                break;            case "unsubscribe":                $content = "取消关注";                break;            case "CLICK":                switch ($object->EventKey)                {                    case "COMPANY":                        $content = array();                        $content[] = array("Title"=>"方倍工作室", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");                        break;                    default:                        $content = "点击菜单:".$object->EventKey;                        break;                }                break;            case "VIEW":                $content = "跳转链接 ".$object->EventKey;                break;            case "SCAN":                $content = "扫描场景 ".$object->EventKey;                break;            case "LOCATION":                $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;                break;            case "scancode_waitmsg":                if ($object->ScanCodeInfo->ScanType == "qrcode"){                    $content = "扫码带提示:类型 二维码 结果:".$object->ScanCodeInfo->ScanResult;                }else if ($object->ScanCodeInfo->ScanType == "barcode"){                    $codeinfo = explode(",",strval($object->ScanCodeInfo->ScanResult));                    $codeValue = $codeinfo[1];                    $content = "扫码带提示:类型 条形码 结果:".$codeValue;                }else{                    $content = "扫码带提示:类型 ".$object->ScanCodeInfo->ScanType." 结果:".$object->ScanCodeInfo->ScanResult;                }                break;            case "scancode_push":                $content = "扫码推事件";                break;            case "pic_sysphoto":                $content = "系统拍照";                break;            case "pic_weixin":                $content = "相册发图:数量 ".$object->SendPicsInfo->Count;                break;            case "pic_photo_or_album":                $content = "拍照或者相册:数量 ".$object->SendPicsInfo->Count;                break;            case "location_select":                $content = "发送位置:标签 ".$object->SendLocationInfo->Label;                break;            default:                $content = "receive a new event: ".$object->Event;                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 receiveText($object)    {        $keyword = trim($object->Content);        //多客服人工回复模式        if (strstr($keyword, "请问在吗") || strstr($keyword, "在线客服")){            $result = $this->transmitService($object);            return $result;        }

        //自动回复模式        if (strstr($keyword, "文本")){            $content = "这是个文本消息";        }else if (strstr($keyword, "表情")){            $content = "中国:".$this->bytes_to_emoji(0x1F1E8).$this->bytes_to_emoji(0x1F1F3)."\n仙人掌:".$this->bytes_to_emoji(0x1F335);        }else if (strstr($keyword, "单图文")){            $content = array();            $content[] = array("Title"=>"单图文标题",  "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");        }else if (strstr($keyword, "图文") || strstr($keyword, "多图文")){            $content = array();            $content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");            $content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");            $content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");        }else if (strstr($keyword, "音乐")){            $content = array();            $content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");         }else{            $content = date("Y-m-d H:i:s",time())."\nOpenID:".$object->FromUserName."\n技术支持 方倍工作室";        }

        if(is_array($content)){            if (isset($content[0])){                $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 = "你发送的是位置,经度为:".$object->Location_Y.";纬度为:".$object->Location_X.";缩放级别为:".$object->Scale.";位置为:".$object->Label;        $result = $this->transmitText($object, $content);        return $result;    }

    //接收语音消息    private function receiveVoice($object)    {        if (isset($object->Recognition) && !empty($object->Recognition)){            $content = "你刚才说的是:".$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 = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;        $result = $this->transmitText($object, $content);        return $result;    }

    //回复文本消息    private function transmitText($object, $content)    {        if (!isset($content) || empty($content)){            return "";        }

        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[text]]></MsgType>    <Content><![CDATA[%s]]></Content></xml>";        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);

        return $result;    }

    //回复图文消息    private function transmitNews($object, $newsArray)    {        if(!is_array($newsArray)){            return "";        }        $itemTpl = "        <item>            <Title><![CDATA[%s]]></Title>            <Description><![CDATA[%s]]></Description>            <PicUrl><![CDATA[%s]]></PicUrl>            <Url><![CDATA[%s]]></Url>        </item>";        $item_str = "";        foreach ($newsArray as $item){            $item_str .= sprintf($itemTpl, $item[‘Title‘], $item[‘Description‘], $item[‘PicUrl‘], $item[‘Url‘]);        }        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[news]]></MsgType>    <ArticleCount>%s</ArticleCount>    <Articles>$item_str    </Articles></xml>";

        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));        return $result;    }

    //回复音乐消息    private function transmitMusic($object, $musicArray)    {        if(!is_array($musicArray)){            return "";        }        $itemTpl = "<Music>        <Title><![CDATA[%s]]></Title>        <Description><![CDATA[%s]]></Description>        <MusicUrl><![CDATA[%s]]></MusicUrl>        <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>    </Music>";

        $item_str = sprintf($itemTpl, $musicArray[‘Title‘], $musicArray[‘Description‘], $musicArray[‘MusicUrl‘], $musicArray[‘HQMusicUrl‘]);

        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[music]]></MsgType>    $item_str</xml>";

        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());        return $result;    }

    //回复图片消息    private function transmitImage($object, $imageArray)    {        $itemTpl = "<Image>        <MediaId><![CDATA[%s]]></MediaId>    </Image>";

        $item_str = sprintf($itemTpl, $imageArray[‘MediaId‘]);

        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[image]]></MsgType>    $item_str</xml>";

        $result = sprintf($xmlTpl, $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‘]);        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[voice]]></MsgType>    $item_str</xml>";

        $result = sprintf($xmlTpl, $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‘]);

        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[video]]></MsgType>    $item_str</xml>";

        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());        return $result;    }

    //回复多客服消息    private function transmitService($object)    {        $xmlTpl = "<xml>    <ToUserName><![CDATA[%s]]></ToUserName>    <FromUserName><![CDATA[%s]]></FromUserName>    <CreateTime>%s</CreateTime>    <MsgType><![CDATA[transfer_customer_service]]></MsgType></xml>";        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());        return $result;    }

    //回复第三方接口消息    private function relayPart3($url, $rawData)    {        $headers = array("Content-Type: text/xml; charset=utf-8");        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);        curl_setopt($ch, CURLOPT_POST, 1);        curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);        $output = curl_exec($ch);        curl_close($ch);        return $output;    }

    //字节转Emoji表情    function bytes_to_emoji($cp)    {        if ($cp > 0x10000){       # 4 bytes            return chr(0xF0 | (($cp & 0x1C0000) >> 18)).chr(0x80 | (($cp & 0x3F000) >> 12)).chr(0x80 | (($cp & 0xFC0) >> 6)).chr(0x80 | ($cp & 0x3F));        }else if ($cp > 0x800){   # 3 bytes            return chr(0xE0 | (($cp & 0xF000) >> 12)).chr(0x80 | (($cp & 0xFC0) >> 6)).chr(0x80 | ($cp & 0x3F));        }else if ($cp > 0x80){    # 2 bytes            return chr(0xC0 | (($cp & 0x7C0) >> 6)).chr(0x80 | ($cp & 0x3F));        }else{                    # 1 byte            return chr($cp);        }    }

    //日志记录    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 = 1000000;            $log_filename = "log.xml";            if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}            file_put_contents($log_filename, date(‘Y-m-d H:i:s‘)." ".$log_content."\r\n", FILE_APPEND);        }    }}?>
时间: 2024-09-30 06:28:55

微信公众平台开发接口PHP SDK完整版(转载)的相关文章

微信公众平台开发接口PHP SDK完整版

原文:微信公众平台开发接口PHP SDK完整版 官方提供的SDK只有一个文本消息功能,我们将所有消息的消息类型及事件响应都整理了进来,并且加入日志记录,代码如下: 更新日志: 2013-01-01 版本1.02014-03-15 增加图片.视频.语音的内容回复2014-04-09 增加菜单链接事件2014-04-10 修改文本回复的判定方法2014-05-20 增加高级群发消息通知事件2014-05-26 增加多客服消息及多客服的判定方法2014-05-27 修改自动回复判定方式2014-06-

微信公众平台开发接口PHP SDK完整版-方倍工作室

<?php /* 方倍工作室 http://www.fangbei.org/ CopyRight 2015 All Rights Reserved */ define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (!isset($_GET['echostr'])) { $wechatObj->responseMsg(); }else{ $wechatObj->va

微信公众平台开发接口PHP SDK

以前没接触过微信公众平台开发,前几天刚找到实习公司就要求我做一个微信公众平台的应用,于是乎开始学习微信公众平台开发接口的调用,看开发文档之后还是不知道从何入手,只好上网找入门资料,终于在方倍工作室找到了完整的SDK,理解了思路之后其实也挺简单的,无非就是进行用户.微信服务器.开发者服务器三者之间的通信.下面是我参考方倍工作室SDK之后之间写的一个SDK,主要是响应文本消息和CLICK按钮消息,获取用户基本信息,当然并没有包含所有的消息类型和事件.更完整的SDK请参考方倍工作室,链接:http:/

.Net实现微信公众平台开发接口(二) 之 “获取access_token”

access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token. 接口调用请求说明 http请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET 参数说明 参数 是否必须 说明 grant_type 是 获取access_token填写client_credential appid 是 第三方用

.Net实现微信公众平台开发接口(三) 之 “信息回复”

对于每一个POST请求,开发者在响应包(Get)中返回特定XML结构,对该消息进行响应(现支持回复文本.图片.图文.语音.视频.音乐).请注意,回复图片等多媒体消息时需要预先上传多媒体文件到微信服务器,只支持认证服务号. 今天说说下面三个 1.关注回复 2.自动回复 3.关键字回复 一.关注回复

微信公众平台开发(97) 图文消息

关键字:微信公众平台 开发模式 图文消息作者:方倍工作室原文:http://www.cnblogs.com/txw1958/p/weixin-97-news.html 在这篇微信公众平台开发教程中,我们将介绍如何灵活的使用图文消息功能.我们将只介绍被动回复消息中的图文消息,发送客服消息及高级群发消息接口的发送的图文消息与本文介绍的图文消息的各情况基本一致. 本文分为以下四个部分: 图文消息的定义 图文消息的实现 图文消息的类型 图文消息的回复 一.图文消息的定义 在微信公众平台消息中,发送被动响

微信公众平台开发教程--方培工作室,PHP语言版本

准备工作 微信公众平台的注册 介绍如何注册一个微信公众账号. 入门教程 微信公众平台开发入门教程 内容:1.申请SAE作为服务器; 2.启用开发模式; 3.微信公众平台PHP SDK; 4.接收发送消息类型解析; 5.微信公众平台开发模式原理; 6.快速开发天气预报功能. 入门教程是下面所有教程的基础. 基础接口 微信公众平台开发(2) 天气预报 介绍了使用中国天气网气象数据,实现微信上的天气预报功能. 微信公众平台开发(3) 中英翻译 介绍了使用有道翻译的接口,在微信公众平台上,开发中英互译的

微信公众平台开发最佳实践(第2版)

<微信公众平台开发最佳实践 第2版>微信公众平台开发经典之作全新改版,精心挑选最经典的商业项目开发,成千上万人次微信公众平台开发者从中受益 前言 出版说明 自从方倍工作室在博客园推出微信公众平台开发系列教程后,受到广大微信开发人员及爱好者的热情关注,相关文章的日访问量高达2万人次,而<微信公众平台开发入门教程>的阅读量超过50万,博客访问量总计超过500万,成为微信公众平台开发更新最快,传播最广.受众最多.资料最全的博客,很多博文被许多有影响力的网站转载,并被各大搜索引擎收录且排名

微信公众账号开发接口实现 - java servlet

原文:微信公众账号开发接口实现 - java servlet 源代码下载地址:http://www.zuidaima.com/share/1550463697898496.htm 微信公众平台开发接口验证token.收取.回复消息实现 其实只需要两个类:DeiponseMessage.java.Wechat.java 项目截图 缺少的jar包下载列表: http://mirrors.ibiblio.org/pub/mirrors/maven2/commons-beanutils/commons-