欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

wordpress文章自动同步到腾讯微博

shiping1 的头像
有了同步到新浪微博,不给腾讯来个同步,说不过去吧。哈哈,说干就干,下面介绍下怎么使用QQ互联的api实现文章自动同步。

同步效果

20150124092641

api介绍

发表一条微博api  https://graph.qq.com/t/add_t

调用需要用到的参数  官方介绍

1
2
3
4
openid = '5321BEETEST1FEA402B790EA65993C1C'; //此处是你的用户openid
oauth_consumer_key = '101183846';   //此处是你的QQ互联应用APP ID
access_token = '276DB4F2086TEST34E3B123CAA30719C';// 此处是你的QQ验证登录和授权的access_token
content = '【文章发布】test pic NO.9';    //此处是微博内容

准备工作

oauth2.0_guid_11

1. 申请appid和appkey的用途

appid:应用的唯一标识。在OAuth2.0认证过程中,appid的值即为oauth_consumer_key的值。

appkey:appid对应的密钥,访问用户资源时用来验证应用的合法性。在OAuth2.0认证过程中,appkey的值即为oauth_consumer_secret的值。

申请地址

申请流程

1. 点击页面上的“申请加入”按钮,申请成为开发者;

(1)申请appid(oauth_consumer_key/client_id)和appkey(auth_consumer_secret/client_secret);

(2)进入 http://connect.qq.com/manage/ 页面,点击“立即添加”,在弹出的对话框中填写网站或应用的详细资料(名称,域名,回调地址);

2. 点击“确定”按钮,提交资料后,获取appid和appkey。

注意:申请appid时,登录的QQ号码将与申请到的appid绑定,后续维护均需要使用该号码。

注意:对appid和appkey信息进行保密,不要随意泄漏。

2. 保证连接畅通

接入QQ登录时,网站需要不停的和Qzone进行交互,发送请求和接受响应。

1. 对于PC网站:请在你的服务器上ping graph.qq.com ,保证连接畅通。

2. 对于WAP网站:请在你的服务器上ping open.z.qq.com和ping graph.z.qq.com,保证连接畅通。

3. 移动应用无需此步骤

获取Access_Token

oauth2.0_guid_1

1. 获取Authorization Code

访问下列网址,返回code数据。注意:用你要发微博的QQ帐号授权登录

1
https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101183846&redirect_uri=http://www.ykuaile.net&state=test

需要更改的参数

  • client_id         是你的你的QQ互联应用APP ID
  • redirect_uri    是你的QQ互联应用里的回调地址

返回值

1
code =058F35AD2C10CED5D78D2CCTEST1C211

2. 获取Access_Token

访问下列网址,返回Access_Token数据

1
https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=101183846&client_secret=941c938aec1test5b18e0c85fe4744c0&code=058F35AD2C10CED5D78D2CCTEST1C211&redirect_uri=http://www.ykuaile.net

需要更改的参数

  • client_id          是你的QQ互联应用APP ID
  • client_secret   是你的QQ互联应用APP KEY
  • code               是你上一步的Authorization Code
  • redirect_uri     是你的QQ互联应用里的回调地址

返回值

1
access_token=276DB4F2086TEST34E3B123CAA30719C&expires_in=7776000&refresh_token=92357511C7D123E01234CA446030F00E

获取用户OpenID

oauth2.0_guid_13

访问下列网址,返回用户OpenID数据

1
https://graph.qq.com/oauth2.0/me?access_token=276DB4F2086TEST34E3B123CAA30719C

需要更改的参数

  • access_token 是你上一步的 access_token

返回值

1
callback( {"client_id":"101183846","openid":"5321BEETEST1FEA402B790EA65993C1C"} );

部署代码

把下面代码加在主题的functions.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//同步腾讯微博
function post_to_tencent_weibo($post_ID) {
    if (wp_is_post_revision($post_ID)) return//修订版本(更新)不发微博
    $get_post_info = get_post($post_ID);
    $get_post_centent = get_post($post_ID)->post_content;
    $get_post_title = get_post($post_ID)->post_title;
    if ($get_post_info->post_status == 'publish' && $_POST['original_post_status'] != 'publish') {
        $openid = '5321BEETEST1FEA402B790EA65993C1C'; //此处是你的用户openid
        $oauth_consumer_key = '101183846';   //此处是你的QQ互联应用APP ID
        $access_token = '276DB4F2086TEST34E3B123CAA30719C';// 此处是你的QQ验证登录和授权的access_token
        $request = new WP_Http;
        $keywords = "";
 
        //获取文章标签关键词
        $tags = wp_get_post_tags($post_ID);
        foreach ($tags as $tag ) {
            $keywords = $keywords.'#'.$tag->name.'# ';
        }
 
        $content = '【文章发布】' . strip_tags( $get_post_title ).':'.mb_strimwidth(strip_tags( apply_filters('the_content', $get_post_centent)),0, 132,'...') .$keywords. ' 查看全文:' . get_permalink($post_ID) ;
 
        $api_url = 'https://graph.qq.com/t/add_t';
        $body = array('oauth_consumer_key' => $oauth_consumer_key , 'format' => 'json','access_token' => $access_token ,'content' => $content,'openid' => $openid);
        $result = $request->post($api_url, array('body' => $body));
 
    }
}
add_action('publish_post', 'post_to_tencent_weibo', 0);
原文链接:,转发请注明来源!
来自 http://www.ykuaile.net/archives/1036.html
普通分类: