使用新浪微博开放平台api同步微博内容至自己网站( 不用插件实现Wordpress文章同步微博代码贴到你主题的function.文件)
优采云 发布时间: 2021-12-04 06:18使用新浪微博开放平台api同步微博内容至自己网站(
不用插件实现Wordpress文章同步微博代码贴到你主题的function.文件)
新浪微博的人气非常高。几乎每个人每天都会阅读微博。如果你能将你的wordpress文章同步到新浪微博,你可以获得大量的社交媒体流量。还有可以实现同步更新功能的插件,比如评论框。但是今天,我想在这篇文章中介绍的是,无需插件即可实现Wordpress文章 同步更新到新浪微博。
1、申请新浪微博应用接入
要实现微博同步功能,首先需要在新浪微博开发者中心申请权限,然后创建网站访问应用。使用您的新浪微博账号登录微博开放平台:。点击页面导航中的【编辑开发者信息】或访问:,根据页面表单内容填写信息,提交后等待审核。新浪评论一般需要1个左右的工作。审核通过后,即可创建应用。然后在微博开放平台首页点击橙色【立即创建微链接】->【网站访问】或直接访问填写好的申请信息,提交审核。应用审核时间会稍长。这段时间可以使用新浪的开放端口进行开发,
2、Wordpress 同步新浪微博代码
新浪微博有很多接口供开发者调用,根据需要,我们只需要了解更新发布接口即可。相关开发文档:. 根据开发文档,我们可以得到如下接口信息:
接口URL:https://api.weibo.com/2/statuses/repost.json
数据格式:JSON
请求方式:POST
请求参数:status(微博内容)、source(A*敏*感*词*ey,从开发者中心我的应用中获取)
是否需要登陆:是
根据以上微博界面信息,我们可以编写wordpress代码如下(a*敏*感*词*ey、用户名、用户密码需要自己替换):
/*
文章同步到新浪微博
代码来源: www.wpzhinan.com
*/
function post_to_sina_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') {
$a*敏*感*词*ey='3838258703';
$username='微博用户名';
$userpassword='微博密码';
$request = new WP_Http;
$status = '【' . strip_tags($get_post_title) . '】 ' . mb_strimwidth(strip_tags(apply_filters('the_content', $get_post_centent)) , 0, 132, '...') . ' 全文地址:' . get_permalink($post_ID);
$api_url = 'https://api.weibo.com/2/statuses/update.json';
$body = array('status' => $status,'source' => $a*敏*感*词*ey);
$headers = array('Authorization' => 'Basic ' . base64_encode("$username:$userpassword"));
$result = $request->post($api_url, array('body' => $body,'headers' => $headers));
}
}
add_action('publish_post', 'post_to_sina_weibo', 0);//给发布文章增加一个分享微博的动作
将上面的wordpress文章同步微博代码粘贴到你主题的function.php文件中。