php 微信开发回复消息

a.php
回复消息
<?php
file_put_contents('post.log',$GLOBALS['HTTP_RAW_POST_DATA']);//测试是否  接受post提交过来的数据写如到文件
$msg=$GLOBALS['HTTP_RAW_POST_DATA'];
$msgstr=(array)simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
//对象强制转换数组(array)simplexml_load_string();
//写日志文件
//file_put_contents(time().rand(100,999).".log",file_get_contents("php://input"));
$com=com($msgstr);
$mp='http://59.110.153.61/wx/ll.mp3';
if($msgstr['MsgType']=='text'){
    //文本信息
    echo "<xml>
        $com
        <MsgType><![CDATA[text]]></MsgType>
        <Content><![CDATA[你好]]></Content>
        </xml>";exit;
}elseif($msgstr['MsgType']=='image'){
    //         {$msgstr['MediaId']}
//        <MediaId><![CDATA[{$msgstr['MediaId']}]]></MediaId>
    //        <MediaId><![CDATA[eYctzRtkNaSYYSjb-MdjBi_Fqehd3mwmWfU83RjGCIlJ_Xqt8TKKA7yMDXEmsbB1]]></MediaId>

    //图片信息
    echo "<xml>
        $com
        <MsgType><![CDATA[image]]></MsgType>
         <Image>
        <MediaId><![CDATA[8kQ2AH6Fgy88-Bf8_hG0i9_96DLbMSZp7aRpJuTHD1o]]></MediaId>
        </Image>
        </xml>";exit;
}elseif($msgstr['MsgType']=='voice'){
    //语音信息
    echo "<xml>
        $com
        <MsgType><![CDATA[voice]]></MsgType>
        <Voice>
        <MediaId><![CDATA[{$msgstr['MediaId']}]]></MediaId>
        </Voice>
    </xml>";exit;
}elseif($msgstr['MsgType']=='video'){
   //回复视频
    echo "<xml>
        $com
        <MsgType><![CDATA[video]]></MsgType>
        <Video>
        <MediaId><![CDATA[1VkhJyhTjbIz9ZL-7EPCMsLgS05FlGX86iOFgI_Bm7tTmGUqOpqQAXdn5VEUedAy]]></MediaId>
        <Title><![CDATA[不容易阿]]></Title>
        <Description><![CDATA[总算出来了]]></Description>
        </Video>
        </xml>";exit;

}elseif($msgstr['MsgType']=='file'){
    //回复音乐
    $musicTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>
</xml>";
    $msgType = "music";
    $time = time();

    $title = "断桥残雪";
    $description = "送给大家希望喜欢.";
    $musicUrl = "http://59.110.153.61/wx/ll.mp3";
    $resultStr = sprintf($musicTpl, $msgstr['FromUserName'], $msgstr['ToUserName'], $time, $msgType, $title, $description, $musicUrl, $musicUrl);
    echo $resultStr;
}

//公共头部
function com($msgstr){
    $str="
        <ToUserName><![CDATA[{$msgstr['FromUserName']}]]></ToUserName>
        <FromUserName><![CDATA[{$msgstr['ToUserName']}]]></FromUserName>
        <CreateTime>".time()."</CreateTime>";
    return $str;
}


die;
//var_dump(file_get_contents('http://59.110.153.61/wx/wx.php?signature=074bb9248377cde0218070a179879ee5550fa4f4&echostr=17014199334721273851&timestamp=1503374267&nonce=3278934949'));die;
/*var_dump(file_get_contents('http://taoo.ngrok.cc/wx/weixin.php?signature=24e2057c4840d2af899657a1ab494af80d161af9&echostr=457723958504712317&timestamp=1503316092&nonce=2054069204'));
die;*/
//file_put_contents("1.txt",json_encode($_GET));//必须手动配置成功是自动把值写入到1.txt里面,否则文件值为空
define("TOKEN","weixin1");//必须是字母加数字
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];

$tmpArr = array(TOKEN,$timestamp,$nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
//file_put_contents("2.txt", $tmpStr);//写入字符串
    if($tmpStr == $signature){
        echo $_GET['echostr'];
        exit;
    }else{
        echo 0;
    }
?>

b.php
自定义菜单,新增临时素材
<?php


$token=@file_get_contents('token.log');
//print_r($token);die;
if($token){
    $token=json_decode($token,true);

    if(time()-$token['time']>7100){

        $token=gettoken();
    }
}else{
    $token=gettoken();
}

//新增永久素材  //如果是语音type=voiceimage可以。/视频=video
//$sucai="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token['access_token']}&type=image";
//$sucai="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token['access_token']}&type=video&title=VIDEO_TITLE&introduction=INTRODUCTION";
$sucai="https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token['access_token']}&type=video";//临时的素材可以永久的不行
//图片地址
//$img=dirname ( __FILE__ ).'b.jpg';
//视频地址
$img='1.mp4';
$res=curla($sucai,$img);
//file_put_contents('img'.time().'.log',file_get_contents($sucai));
var_dump($res);die;
function curla($sucai,$img){
    $ch = curl_init();
    //判断curl类的扩展是否存在可用
    if(class_exists('CURLFile')){
        //存在就new
        curl_setopt($ch,CURLOPT_SAFE_UPLOAD,true);
        $data=array('media'=>new \CURLFile(realpath($img)));//必须加上realpath()否则读取不到文件的路径
//        $data['title']='123456';
    }else{
        //不存在
        if(defined('CURLOPT_SAFE_UPLOAD')){
            curl_setopt($ch,CURLOPT_SAFE_UPLOAD,false);
        }
        //realpath读取文件的所在的地址
        $data=array('media'=>"@".realpath($img));
    }
    curl_setopt($ch, CURLOPT_URL, $sucai);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //请求https协议时必须加上重点ssl安全验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    // post数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // post的变量
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

//新增永久素材结束




//echo $token['access_token'];
//$menurl="https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$token['access_token']}";
//
//$post_data=' {
//     "button":[
//     {
//          "type":"click",
//          "name":"今日歌曲",
//          "key":"V1001_TODAY_MUSIC"
//      },
//      {
//           "name":"菜单",
//           "sub_button":[
//           {
//               "type":"view",
//               "name":"搜索",
//               "url":"http://www.soso.com/"
//            },
//            {
//               "type":"click",
//               "name":"赞一下我们",
//               "key":"V1001_GOOD"
//            }]
//       }]
// }';
//
//$output=curla($menurl,$post_data);
//print_r($output);
//function curla($menurl,$post_data){
//    $ch = curl_init();
//    curl_setopt($ch, CURLOPT_URL, $menurl);
//    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//    //请求https协议时必须加上重点ssl安全验证
//    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//
//    // post数据
//    curl_setopt($ch, CURLOPT_POST, 1);
//    // post的变量
//    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//
//    $output = curl_exec($ch);
//    curl_close($ch);
//    return $output;
//}

function gettoken(){
    //公网
//$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxb80a467fde37f031&secret=53ec3e82276778ffb67bde51f1f8be58';
//海涛
$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx7a0e406a86abd173&secret=2341de9161e535c6ffe7580bfb876fff';
$token=file_get_contents($url);
$token=json_decode($token,true);
$token['time']=time();
file_put_contents("token.log",json_encode($token));
return $token;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值