微信公众平台 回复图文信息方法优化(PHP)

  1. //回复文本

  2. function responseTextMsg($funcFlag = 0,$content){
  3. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  4. if (!empty($postStr)){
  5. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  6. $fromUsername = $postObj->FromUserName;
  7. $toUsername = $postObj->ToUserName;
  8. $keyword = trim($postObj->Content);
  9. $time = time();
  10. $text = "
  11. ".$fromUsername."
  12. ".$toUsername."
  13. ".$time."
  14. text
  15. ".$content."
  16. ".$funcFlag."
  17. "; //格式化回复文本
  18. echo $text;
  19. }
  20. return 0;
  21. }

  22. //回复图文
  23. function responseNewsMsg($funcFlag = 0,$contentArray){
  24. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
  25. if (!empty($postStr)){
  26. $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  27. $fromUsername = $postObj->FromUserName;
  28. $toUsername = $postObj->ToUserName;
  29. $time = time();
  30. $articleCount = count($contentArray);
  31. $news = "";
  32. $news .= "
  33. ".$fromUsername."
  34. ".$toUsername."
  35. ".$time."
  36. news
  37. ".$articleCount."
  38. ";
  39. $i = 0;
  40. while($i < $articleCount){
  41. $news .= "
  42. ". $contentArray[$i]["description"]."
  43. ". $contentArray[$i]["picUrl"]."
  44. ". (count($contentArray[$i]) == 4 ? $contentArray[$i]["url"]:OpenSite)."
  45. ";
  46. $i++;
  47. }
  48. $news .= "
  49. ".$funcFlag."
  50. ";
  51. echo $news;
  52. }
  53. return 0;
  54. }
以后每次回复图文消息就可以直接构造数组发送了,例:
  1. $arrayCon = array(array("title"=>"图文消息标题1",
  2. "description"=>"图文消息描述1",
  3. "picUrl"=>"图片链接1","url"=>"点击图文消息跳转链接1"),
  4. array("title"=>"图文消息标题2",
  5. "description"=>"图文消息描述2",
  6. "picUrl"=>"图片链接2",
  7. "url"=>"点击图文消息跳转链接2"));
  8. responseNewsMsg(0,$arrayCon);

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值