1 Star 1 Fork 1

dbj151 / weiphp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test.php 15.95 KB
一键复制 编辑 原始数据 按行查看 历史
Dave 提交于 2018-02-11 18:19 . fdsa
<?php
header("Content-type: text/html; charset=utf-8");
class Wxdhl{
public $time = 0;
public $access_token;
public function index(){
// $user_info = getWeixinUserInfo();
define("APPID","wxcc8d1ac2b7e86c10");
define("APPSECRET","27bfe0fb0e443e79efc5914ea369f73f");
define("TOKEN","cherish");
$this->valid();
}
public function valid() {
// $this->checkSignature();
$this->responseMsg();
}
public function responseMsg() {
//创建菜单
$this->create_menu();
//获取code
// get post data, May be due to the different environments
$postStr = file_get_contents("php://input"); //代替
// extract post data
if (! empty ( $postStr )) {
//将接收的消息处理返回一个对象
$postObj = simplexml_load_string ( $postStr, 'SimpleXMLElement', LIBXML_NOCDATA );
//从消息对象中获取消息的类型 text image location voice vodeo link
$msgType = $postObj->MsgType;
//消息类型分离, 通过$msgType类型作为判断, 每个方法都需要将对象$postObj传入
switch ($msgType)
{
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 = "unknown msg type: ".$msgType; //未知的消息类型
break;
}
//输出消息给微信
echo $result;
}else {
//如果没有消息则输出空,并退出
echo "";
exit;
}
}
/**
* CURL请求
* @param $url
* @return mixed|string
*/
function https_request($url,$post_data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_URL, $url);
if($post_data){
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data );
}else{
curl_setopt($curl, CURLOPT_POSTFIELDS);
}
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
/**
* 配置微信服务器验证token
* */
function checkSignature() {
if (! empty ( $_GET ['echostr'] ) && ! empty ( $_GET ["signature"] ) && ! empty ( $_GET ["nonce"] )) {
$signature = $_GET ["signature"];
$timestamp = $_GET ["timestamp"];
$nonce = $_GET ["nonce"];
$tmpArr = array (
TOKEN,
$timestamp,
$nonce
);
sort ( $tmpArr, SORT_STRING );
$tmpStr = sha1 ( implode ( $tmpArr ) );
if ($tmpStr == $signature) {
echo $_GET ["echostr"];
}
exit ();
}
}
//接收事件
private function receiveEvent($object){
$eventType = strtolower(trim($object->Event));//接收事件类型
if($eventType == "click"){
$result = '';
$eventKey = strtolower(trim($object->EventKey));
if($eventKey == 'sign'){
$newsArray = array();
$newsArray[] = array(
'Title' => $eventKey.'图文消息',
'Description' => '这是一个图文消息哦!',
'PicUrl' => 'http://xsfr.mgtvshop.com/Public/Home/images/small_bag.jpg',
'Url' => 'http://www.baidu.com'
);
$result = $this->transmitNews($object,$newsArray);
}elseif($eventKey == 'omg'){
file_put_contents('./Runtime/object.txt',json_encode($object));
$FromUserName = $object->FromUserName;//openid
if(!isset($FromUserName)){
$FromUserName = '';
}
$content = $FromUserName."不要点人家嘛";
$result = $this->transmitText($object,$content);
}elseif ($eventKey == 'userinfo'){
$openid = $object->FromUserName;
$json_data = $this->get_php_file('access_token.json');
$data = json_decode($json_data);
$access_token = $data->access_token->access_token;
if(time() > $data->expires_time){
$access_token = $this->get_access_token();
}
$result_json = $this->userinfo($access_token,$openid);
$result_data = json_decode($result_json);
if($result_data->sex == 1){
$result_data->sex = '男';
}else{
$result_data->sex = '女';
}
$content = "昵称:".$result_data->nickname
."\r\n性别:".$result_data->sex
."\r\n地址:".$result_data->country.$result_data->province.$result_data->city
."\r\nopenid:".$result_data->openid;
;
$result = $this->transmitText($object,$content);
}else{
$content = $eventKey;
$result = $this->transmitText($object,$content);
}
return $result;
}
}
/**
* 获取用户信息
* @access_token 全局access_token
* @openid 用户openid
* 返回数据类型 json
* */
public function userinfo($access_token,$openid){
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid;
$result = $this->https_request($url);
return $result;
}
//接收文本消息
private function receiveText($object)
{
//从接收到的消息中获取用户输入的文本内容, 作为一个查询的关键字, 使用trim()函数去两边的空格
$keyword = trim($object->Content);
//自动回复模式
if (strstr($keyword, "文本")){
$content = "这是个文本消息";
}else if(strstr($keyword, "获取code")){
$content = file_get_contents(RUNTIME_PATH.'code.txt');
}else if(strstr($keyword, "H5支付")){
$content = "请在外部浏览器打开此链接:http://xsfr.mgtvshop.com/index.php?s=/w0/Home/weixinPay/pay";
}else if (strstr($keyword, "单图文")){
$content = array();
$content[] = array("Title"=>"这里是单图文标题", "Description"=>"单图文内容", "PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/2j8mJHm8CogqL5ZSDErOzeiaGyWIibNrwrVibuKUibkqMjicCmjTjNMYic8vwv3zMPNfichUwLQp35apGhiciatcv0j6xwA/0", "Url" =>"http://mp.weixin.qq.com/s?__biz=MjM5NDAxMDEyMg==&mid=201222165&idx=1&sn=68b6c2a79e1e33c5228fff3cb1761587#rd");
}else if (strstr($keyword, "图文") || strstr($keyword, "多图文")){
$content = array();
$content[] = array("Title"=>"多图文1标题", "Description"=>"这里是描述", "PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/2j8mJHm8CogqL5ZSDErOzeiaGyWIibNrwrVibuKUibkqMjicCmjTjNMYic8vwv3zMPNfichUwLQp35apGhiciatcv0j6xwA/0", "Url" =>"http://www.baidu.com");
$content[] = array("Title"=>"多图文2标题", "Description"=>"这里是描述", "PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/2j8mJHm8CogqL5ZSDErOzeiaGyWIibNrwrVibuKUibkqMjicCmjTjNMYic8vwv3zMPNfichUwLQp35apGhiciatcv0j6xwA/0", "Url" =>"http://www.baidu.com");
$content[] = array("Title"=>"多图文3标题", "Description"=>"这里是描述", "PicUrl"=>"http://mmbiz.qpic.cn/mmbiz/2j8mJHm8CogqL5ZSDErOzeiaGyWIibNrwrVibuKUibkqMjicCmjTjNMYic8vwv3zMPNfichUwLQp35apGhiciatcv0j6xwA/0", "Url" =>"http://www.baidu.com");
}else if (strstr($keyword, "音乐")){
$content = array();
$content = array("Title"=>"别看我只是一只羊", "Description"=>"歌手:喜洋洋和灰太狼", "MusicUrl"=>"http://www.kugou.com/song/foez22.html?frombaidu?frombaidu#hash=0A4D3971C000D9862C171E3D9403DBC5&album_id=0", "HQMusicUrl"=>"http://www.kugou.com/song/foez22.html?frombaidu?frombaidu#hash=0A4D3971C000D9862C171E3D9403DBC5&album_id=0");
}else{
$content = date("Y-m-d H:i:s",time())."\n青青大草原";
}
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 = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$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 = "未开启语音识别功能或者识别内容为空";
$result = $this->transmitText($object, $content);
}
*/
//如果开启语言识别功能, 就可以使用这个
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, "Title"=>"this is a test", "Description"=>"pai pai");
$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)
{
$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 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>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
</Video>";
$item_str = sprintf($itemTpl, $videoArray['MediaId'], $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 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)
{
$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;
}
}
$obj = new Wxdhl();
$obj->index();
?>
PHP
1
https://gitee.com/dbj151/weiphp.git
git@gitee.com:dbj151/weiphp.git
dbj151
weiphp
weiphp
master

搜索帮助