Blog

Blog

PHODAL

Wordpress 微信 WP API+weChat打造Wordpress的微信查询一

wordpress上没有一个好的微信工具,于是这里就用上了 王超 写的 微信公众平台后台框架

这里是基于ruby上的sinatra框架,而wordpress方面则是依赖于wp api生成的json

Wordpress api

这就是一个简单的示例

http://localhost/?wpapi=search&dev=1&keyword=title

会返回这样一个结果


{"status":"ok","count":2,"count_total":2,"pages":1,"currPage":1,"posts":[{"id":"4","type":"post","slug":"other-title","url":"http:\/\/xunta.phodal.net\/?p=4","status":"publish","title":"other title","title_plain":"other title","date":"2014-03-26 20:38:58","modified":"2014-03-26 20:38:58","excerpt":"this \u00a0a test","parent":"0","category":[{"term_id":1,"name":"Uncategorized","slug":"uncategorized","term_group":0,"term_taxonomy_id":1,"taxonomy":"category","description":"","parent":0,"count":2,"object_id":4,"filter":"raw","cat_ID":1,"category_count":2,"category_description":"","cat_name":"Uncategorized","category_nicename":"uncategorized","category_parent":0}],"tag":[],"author":[{"id":"1","slug":"root","name":"root","first_name":"","last_name":"","nickname":"root","url":"","description":"","gravatar":"http:\/\/www.gravatar.com\/avatar\/56d54d4b6b4dc4e3efa458f745d61513?s=100&d=mm&r=g"}],"comment_count":"0","comment_status":"open"}]}
而我们在ruby上所要做的就是解析这个json数据.

Ruby 解析JSON数据

这里需要用到ruby的json库以及net/http

ruby http get

一个简单的http get请求


    require 'json'
    require 'net/http' 
response = Net::HTTP.get_response("localhost","/?wpapi=search&dev=1&keyword=title") p JSON.parse response.body
就可以解析完输出的结果

Ruby weChat

安装weChat

gem install 'wei-backend'

官方的示例如下


    require 'sinatra'
    require 'wei-backend'

token "mytoken"

on_text do
    "你发送了如下内容: #{params[:Content]}!!"
end

on_subscribe do
    "感谢您的订阅"
end

on_unsubscribe do
    "欢迎您再次订阅"
end

所以我们需要布到服务器上面,同时修改一下上面的token

当然这里还支持下面这种格式


    on_text do
      [{
           :title => '收到一个文本消息,返回两个图文消息',
           :description => 'desc',
           :picture_url => 'pic url',
           :url => 'url'
       },
       {
           :title => '这是第二个图文消息',
           :description => 'desc1',
           :picture_url => 'pic url1',
           :url => 'url1'
       }]
    end

所以我们要做的就是把我们数据放进去

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签