微信公众号聊天机器人

来源:https://www.sucaihuo.com/php/1234.html 2017-01-10 21:13浏览(4115) 收藏

微信公众号自动聊天机器人,PHP微信公众号apikey
微信公众号聊天机器人
分类:PHP > 插件 难易:入门级
下载资源 下载积分: 80 积分

配一下自己的百度apikey就能用了,演示公众号EvilSon

//接收用户发过来的消息
   $xml = $GLOBALS["HTTP_RAW_POST_DATA"];
   $obj = simplexml_load_string($xml);
   
 //文本模板
   $textTpl = "<xml>
					<ToUserName><![CDATA[%s]]></ToUserName>
					<FromUserName><![CDATA[%s]]></FromUserName>
					<CreateTime>%s</CreateTime>
					<MsgType><![CDATA[%s]]></MsgType>
					<Content><![CDATA[%s]]></Content>
				</xml>"; 

  if(!empty($obj->Content)) {//是文本消息
      $data = $obj->Content;
      $ch = curl_init();
      $url = 'http://apis.baidu.com/turing/turing/turing?key=879a6cb3afb84dbf4fc84a1df2ab7319&info='.$data.'&userid=eb2edb736';
      $header = array(
          'apikey: 您自己的apikey',
      );
      // 添加apikey到header
      curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      // 执行HTTP请求
      curl_setopt($ch , CURLOPT_URL , $url);
      $res = curl_exec($ch);
      $data = json_decode($res);
    //file_put_contents("yuyin.txt", $url);
      echo sprintf($textTpl,$obj->FromUserName,$obj->ToUserName,time(),'text',$data->text);

  }
评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2