1 Star 0 Fork 12

snowind/exwechat

forked from youwen21/exwechat 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
exXMLMaker.php 3.60 KB
一键复制 编辑 原始数据 按行查看 历史
youwen21 提交于 2018-02-23 17:58 . 更改结构,添加微信支付API
<?php
namespace youwen\exwechat;
class exXMLMaker
{
private $_FromUserName = '';
private $_ToUserName = '';
public function __construct($FromUserName = '', $ToUserName='')
{
if(empty($FromUserName) || empty($ToUserName)){
$msg = exRequest::instance()->getMsg();
$this->_FromUserName = $msg['FromUserName'];
$this->_ToUserName = $msg['ToUserName'];
}else{
$this->_FromUserName = $FromUserName;
$this->_ToUserName = $ToUserName;
}
}
//创建文本消息
private $textTpl = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>';
public function createText($contentStr='', $from='', $to='')
{
$FromUserName = empty($from)? $this->_FromUserName : $from;
$ToUserName = empty($to)? $this->_ToUserName : $to;
empty($contentStr) ? $contentStr = "谢谢关注" : $contentStr;
return sprintf($this->textTpl, $FromUserName, $ToUserName, time(), 'text', $contentStr);
}
public function createNews($newres=[], $from='', $to='')
{
$FromUserName = empty($from)? $this->_FromUserName : $from;
$ToUserName = empty($to)? $this->_ToUserName : $to;
$str = "<xml>";
$str .= "<ToUserName><![CDATA[" . $FromUserName . "]]></ToUserName>";
$str .= "<FromUserName><![CDATA[" . $ToUserName . "]]></FromUserName>";
$str .= "<CreateTime>" . time() . "</CreateTime>";
$str .= "<MsgType><![CDATA[news]]></MsgType>";
$str .= "<ArticleCount>" . count($newres) . "</ArticleCount>";
$str .= "<Articles>";
foreach ($newres as $value) {
// url关键字替换成用户openid
$url = str_replace('openidvalue', $FromUserName, $value['url']);
$str .= "<item>";
$str .= "<Title><![CDATA[" . $value['title'] . "]]></Title>";
$str .= "<Description><![CDATA[" . $value['description'] . "]]></Description>";
$str .= "<PicUrl><![CDATA[" . $value['picurl'] . "]]></PicUrl>";
$str .= "<Url><![CDATA[" . $url . "]]></Url>";
$str .= "</item>";
}
$str .= "</Articles>";
$str .= "</xml>";
return $str;
}
protected function response_image($media_id = '', $from='', $to='')
{
$FromUserName = empty($from)? $this->_FromUserName : $from;
$ToUserName = empty($to)? $this->_ToUserName : $to;
$str = '<xml>';
$str .= '<ToUserName><![CDATA[' . $FromUserName . ']]></ToUserName>';
$str .= '<FromUserName><![CDATA[' . $ToUserName . ']]></FromUserName>';
$str .= '<CreateTime>' . time() . '</CreateTime>';
$str .= '<MsgType><![CDATA[image]]></MsgType>';
$str .= '<Image>';
$str .= '<MediaId><![CDATA[' . $media_id . ']]></MediaId>';
$str .= '</Image>';
$str .= '</xml>';
return $str;
}
public static function arrToXml(array $data, $cdata=[])
{
$cdataKey = array_merge(['detail', 'scene_info'], $cdata);
$xml = '<xml>';
foreach ($data as $key => $value) {
if(in_array($key, $cdataKey)){
$xml .='<'.$key.'><![CDATA['.$value.']]></'.$key.'>';
}else{
$xml .='<'.$key.'>'.$value.'</'.$key.'>';
}
}
$xml .='</xml>';
return $xml;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/snowid/exwechat.git
git@gitee.com:snowid/exwechat.git
snowid
exwechat
exwechat
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385