安卓接入微信php处理,PHP对接微信公众号实现简单自动回复

//  -----可配置区域开始-------------

//微信令牌,请与微信公众号后台同步

define("TOKEN", "weixin");

//解析地址

define("API", "你的解析地址");

//显示数量

define("NUM", "5");

//公众号名称

define("TITLE", "橙子影视");

//默认图片

define("PIC", "默认图片地址");

//留言本地址

define("BOOK", "留言本地址");

//  -----可配置区域结束-------------

$wechatObj = new wechatCallbackapiTest();

$wechatObj->valid();

class wechatCallbackapiTest

{

public function CheckUrl($url)

{

return preg_match('/(http|https|ftp):\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is',$url);

}

public function valid()

{

$echoStr = $_GET["echostr"];

if($echoStr==""){

$this->responseMsg();

}elseif($this->checkSignature()){

echo $echoStr;

exit;

}

}

public function responseMsg(){

$postStr = addslashes(file_get_contents('php://input'));     if (empty($postStr)){exit("你好!请关注【".TITLE."】微信公众号获取服务");}

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

$fromUsername = $postObj->FromUserName;

$toUsername = $postObj->ToUserName;

$time=$postObj->CreateTime;

$keyword = trim($postObj->Content);

$event = $postObj->Event;

$textTpl = "%s";

$newsTpl = "%s1";

switch($postObj->MsgType){

case 'event':

if($event == 'subscribe'){

//关注后的回复

$contentStr = "欢迎关注".TITLE."\r\n本公众号提供在线影视观看,免广告看VIP视频,持续关注,精彩多多。\r\n输入格式:\r\n    1.输入电影名,如: 西游记 即可在线观看!\r\n2.输入视频网址,支持爱奇艺,优酷,腾讯等主流视频网站免VIP播放。\r\n3.回复数字:\r\n 【0】 显示帮助\r\n 【1】打开留言板";

$msgType = 'text';

$textTpl = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $textTpl;

break;

}

case 'text':

//输入文字

if(preg_match('/[\x{4e00}-\x{9fa5}]+/u',$keyword))

{

$result = file_get_contents(API."/api.php?tp=json&wd=".$keyword);

$result= json_decode($result,true);

if($result &&  $result["success"]){

$txt .="恭喜,成功找到视频,请点击播放:\r\n\n";

$i=1;

foreach( $result["info"] as $row){

$title = $row['title'];

$url=API."/?wd=".urlencode($title)."&id=".$row['id']."&flag=".$row['flag']."&type=".$row['type'];

$url=self::getContentLink($url);

$pic= $row['pic'];

$txt .= "·".urldecode($title)."\r\n\n";

$i++;

if($i>NUM){break;}

}

$contentStr = $txt.'【更多】...';

$msgType = 'text';

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

} else{

//没有查找到的时候的回复

$title = "资源未找到,点击图片留言反馈! ";

$des1 ="";

//图片地址

$picUrl1 =PIC;

//跳转链接

$url=self::getContentLink(BOOK);

$resultStr= sprintf($newsTpl, $fromUsername, $toUsername, $time, $title, $des1, $picUrl1, $url) ;

echo $resultStr;

}

//其他

}else if($this->CheckUrl($keyword)){

$title = '点击开始播放';

$des1 ="";

//图片地址

$picUrl1 =PIC;

//跳转链接

$url=API."/?url=".$keyword."";

$url=self::getContentLink($url);

$resultStr= sprintf($newsTpl, $fromUsername, $toUsername, $time, $title, $des1, $picUrl1, $url) ;

echo $resultStr;

}else if($keyword=="0"){

$contentStr = "欢迎关注".TITLE."\r\n本公众号提供在线影视观看,免广告看VIP视频,持续关注,精彩多多。\r\n输入格式:\r\n    1.输入电影名,如: 西游记 即可在线观看!\r\n2.输入视频网址,支持爱奇艺,优酷,腾讯等主流视频网站免VIP播放。\r\n3.回复数字:\r\n 【0】 显示帮助\r\n 【1】打开留言板";

$msgType = 'text';

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

}else if($keyword=="1" ){

$title = '点击打开留言板';

$des1 ="";

//图片地址

$picUrl1 =PIC;

//跳转链接

$url=self::getContentLink(BOOK);

$resultStr= sprintf($newsTpl, $fromUsername, $toUsername, $time, $title, $des1, $picUrl1, $url) ;

echo $resultStr;

}else{

$contentStr = "输入格式:\r\n    1.输入电影名,如: 西游记 即可在线观看!\r\n2.输入视频网址,支持爱奇艺,优酷,腾讯等主流视频网站免VIP播放。\r\n 3.回复数字:\r\n 【0】 显示帮助\r\n 【1】打开留言板  ";

$msgType = 'text';

$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);

echo $resultStr;

};

break;

default:

break;

}// switch end

}

private function checkSignature(){

// you must define TOKEN by yourself

if (!defined("TOKEN")) {

throw new Exception('TOKEN is not defined!');

}

$signature = $_GET["signature"];

$timestamp = $_GET["timestamp"];

$nonce = $_GET["nonce"];

$token = TOKEN;

$tmpArr = array($token, $timestamp, $nonce);

// use SORT_STRING rule

sort($tmpArr, SORT_STRING);

$tmpStr = implode( $tmpArr );

$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){

return true;

}else{

return false;

}

}

private function getContentLink($url){

$api="http://api.t.sina.com.cn/short_url/shorten.json?source=2815391962&url_long=";

$result = file_get_contents($api.$url);

$result= json_decode($result,true);

return $result[0]["url_short"];

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值