微信平台发图文
<?php
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET['echostr'])) {
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
header('content-type:text');
echo $echoStr;
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$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>";
$imageTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>//消息类型为news(图文)
<ArticleCount>1</ArticleCount>//图文数量为1(单图文)
<Articles>
<item>//第一张图文消息
<Title><![CDATA[%s]]></Title> //标题
<Description><![CDATA[%s]]></Description>//描述为空(懒得描述)
<PicUrl><![CDATA[%s]]></PicUrl>//打开前的图片链接地址
<Url><![CDATA[%s]]></Url>//点击进入后显示的图片链接地址
</item>
</Articles>
</xml> ";
if(!empty( $keyword ))
{
$title = "哎呦我去,又下雨了";//标题
$Description ="上海";
$PicUrl = "https://1.star530.sinaapp.com/weather.jpg";//图片链接
$Url = "https://1.star530.sinaapp.com/weather.jpg";//打开后的图片链接
$resultStr = sprintf($imageTpl, $fromUsername, $toUsername, $time, $title, $Description,$PicUrl,$Url);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
}
?>
评论