移植微信公众平台DEMO修改,挂在新浪微博的私信聊天机器人。测试微博@海经院论坛 微信公众平台SDK for Go ?php//接口要求返回的字符串需要是utf8编码。header( 'Content-type: text/html; charset=utf-8' );//加载SDKrequire_once 'CallbackSDK.php';//设置a
移植微信公众平台DEMO 修改,挂在新浪微博的私信聊天机器人。测试微博@海经院论坛
微信公众平台SDK for Go
setAppSecret(APP_SECRET); //签名验证 $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; if (!$call_back_SDK->checkSignature($signature, $timestamp, $nonce)) { die("check signature error"); } //首次验证url时会有'echostr'参数,后续推送消息时不再有'echostr'字段 //若存在'echostr'说明是首次验证,则返回'echostr'的内容。 if (isset($_GET["echostr"])) { die($_GET["echostr"]); } //处理开放平台推送来的消息,首先获取推送来的数据. $post_msg_str = $call_back_SDK->getPostMsgStr(); /** * 设置接口默认返回值为空字符串。 * 请注意数据编码类型。接口要求返回的字符串需要是utf8编码 * 需要说明的是开放平台判断推送成功的标志是接口返回的http状态码, * 只要应用的接口返回的状态为200就会认为消息推送成功,如果http状态码不为200则会重试,共重试3次。 */ $str_return = ''; if (!empty($post_msg_str)) { //sender_id为发送回复消息的uid,即蓝v自己 $sender_id = $post_msg_str['receiver_id']; //receiver_id为接收回复消息的uid,即蓝v的粉丝 $receiver_id = $post_msg_str['sender_id']; //回复text类型的消息示例。 $keyword= $post_msg_str['text']; //图灵API $apiKey = "*****************"; $apiURL = "http://www.tuling123.com/openapi/api?key=KEY&info=INFO"; // 设置报文头, 构建请求报文 $reqInfo = $keyword; $url = str_replace("INFO", $reqInfo, str_replace("KEY", $apiKey, $apiURL)); $res =file_get_contents($url); $result = json_decode($res); $jiaoyan=$result->{'code'}; switch($jiaoyan){ case "200000": $data_type = "text"; $wz=$result->{'text'}; $lj=$result->{'url'}; $xx=$wz.$lj; $data = $call_back_SDK->textData("$xx"); break; case "302000": //$data_type = "text"; //$lb=$result->{'list'}; //$xx=var_export ($lb, TRUE); //$data = $call_back_SDK->textData("$xx"); $data_type = "articles"; $length = count($result['list']) > 9 ? 9 :count($result['list']); for($i= 0;$i< $length;$i++){ $articles [$i] = array ( 'display_name' => $result['list'][$i]['article'], 'summary' => $result['list'][$i]['article'], 'image' => $result['list'][$i]['icon'], 'url' => $result['list'][$i]['detailurl'] ); } $data = $call_back_SDK->articleData($articles); break; case "40001": $data_type = "text"; $xx="(>﹏<)我累了,需要休息一下。"; $data = $call_back_SDK->textData("$xx"); break; case "40002": $data_type = "text"; $xx="(>﹏<)我累了,需要休息一下。"; $data = $call_back_SDK->textData("$xx"); break; case "40003": $data_type = "text"; $xx="(>﹏<)我累了,需要休息一下。"; $data = $call_back_SDK->textData("$xx"); break; case "40004": $data_type = "text"; $xx="(>﹏<)我累了,需要休息一下。"; $data = $call_back_SDK->textData("$xx"); break; case "40005": $data_type = "text"; $xx="(>﹏<)我累了,需要休息一下。"; $data = $call_back_SDK->textData("$xx"); break; case "40006": $data_type = "text"; $xx="(>﹏<)我累了,需要休息一下。"; $data = $call_back_SDK->textData("$xx"); break; case "40007": $data_type = "text"; $xx="(>﹏<)我累了,需要休息一下。"; $data = $call_back_SDK->textData("$xx"); break; default: $data_type = "text"; $xx=$result->{'text'}; $data = $call_back_SDK->textData("$xx"); } $str_return = $call_back_SDK->buildReplyMsg($receiver_id, $sender_id, $data, $data_type); } echo json_encode($str_return);
登录后复制
app_secret = $app_secret; } /** * 获取推送来的的数据 * 必须使用 $GLOBALS['HTTP_RAW_POST_DATA']方法获取post过来的原始数据来解析. * @return mixed */ public function getPostMsgStr() { return json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true); } /** * 验证签名 * @param $signature * @param $timestamp * @param $nonce * @return bool */ function checkSignature($signature, $timestamp, $nonce) { $tmpArr = array($this->app_secret, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = sha1(implode($tmpArr)); if ($tmpStr == $signature) { return true; } else { return false; } } /** * 组装返回数据 * @param $receiver_id * @param $sender_id * @param $data * @param $type * @return array */ function buildReplyMsg($receiver_id, $sender_id, $data, $type) { return $msg = array( "sender_id" => $sender_id, "receiver_id" => $receiver_id, "type" => $type, //data字段需要进行urlencode编码 "data" => urlencode(json_encode($data)) ); } /** * 生成text类型的回复消息内容 * @param $text * @return array */ function textData($text) { return $data = array("text" => $text); } /** * 生成article类型的回复消息内容 * @param $article * @return array */ function articleData($articles) { return $data = array( 'articles' => $articles ); } /** * 生成position类型的回复消息内容 * @param $longitude * @param $latitude * @return array */ function positionData($longitude, $latitude) { return $data = array( "longitude" => $longitude, "latitude" => $latitude ); } }
登录后复制