微信公众平台 回复图文信息方法优化(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);

你可能感兴趣的:(微信公众平台 回复图文信息方法优化(PHP))