一键分享到QQ空间、QQ好友、新浪微博、微信代码

chen'mo
2021-03-01 / 4 评论 / 5,027 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年03月01日,已超过1145天没有更新,若内容或图片失效,请留言反馈。

通过qq空间、qq聊天、新浪微博和微信二维码分享平台提供的接口,实现把网页中对应的图片、标题、描述的信息参数用javascript获取后传进接口中,实现一键分享。


提示:使用到的接口(测试时需要登录,网址和图片必须是公网的,不能localhost,QQ图片不能太宽,太宽标题描述会undefiend):

一、分享链接

======

1.分享到QQ空间接口:

https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?
url=你的网址
&sharesource=qzone
&title=你的分享标题
&pics=你的分享图片
&summary=你的分享描述信息

2.分享给QQ好友接口

http://connect.qq.com/widget/shareqq/index.html?
url=你的分享网址
&sharesource=qzone
&title=你的分享标题
&pics=你的分享图片地址
&summary=你的分享描述
&desc=你的分享简述

3.分享到新浪微博接口:

http://service.weibo.com/share/share.php?
url=你的分享网址
&sharesource=weibo
&title=你的分享标题
&pic=你的分享图片
&appkey=你的key需要在新浪微博开放平台中申请


二、一键分享代码参考如下:

=============

html(示例):

<div>分享到:</div>
<div onclick="shareTo('qzone')">
    <img src="https://zixuephp.net/static/images/qqzoneshare.png" width="32"> <!-- 分享图片不再提供访问 -->
</div>
<div onclick="shareTo('qq')">
    <img src="https://zixuephp.net/static/images/qqshare.png" width="32">
</div>
<div onclick="shareTo('sina')">
    <img src="https://zixuephp.net/static/images/sinaweiboshare.png" width="32">
</div>
<div onclick="shareTo('wechat')">
    <img src="https://zixuephp.net/static/images/wechatshare.png" width="32">
</div>

js(示例):

function shareTo(types){

    var title,imageUrl,url,description,keywords;

    //获取文章标题
    title = document.title;

    //获取网页中内容的第一张图片地址作为分享图
    //imageUrl = document.images[0].src;
    imageUrl = document.getElementById("pcdetails").getElementsByTagName("img")[0];
    //当内容中没有图片时,设置分享图片为网站logo
    if(typeof imageUrl == 'undefined'){
        imageUrl = 'https://'+window.location.host+'/static/images/logo.png';
    } else {
        imageUrl = imageUrl.src;
    }

    //获取当前网页url
    url = document.location.href;

    //获取网页描述
    description = document.querySelector('meta[name="description"]').getAttribute('content');

    //获取网页关键字
    keywords = document.querySelector('meta[name="keywords"]').getAttribute('content');

    //qq空间接口的传参
    if(types=='qzone'){
        window.open('https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+url+'&sharesource=qzone&title='+title+'&pics='+imageUrl+'&summary='+description);
    }
    //新浪微博接口的传参
    if(types=='sina'){
        window.open('http://service.weibo.com/share/share.php?url='+url+'&sharesource=weibo&title='+title+'&pic='+imageUrl+'&appkey=2706825840');
    }
    //qq好友接口的传参
    if(types == 'qq'){
        window.open('http://connect.qq.com/widget/shareqq/index.html?url='+url+'&sharesource=qzone&title='+title+'&pics='+imageUrl+'&summary='+description+'&desc='+keywords);
    }
    //生成二维码给微信扫描分享
    if(types == 'wechat'){
        //在线二维码(服务器性能限制,仅测试使用,屏蔽非大陆ip访问)
        window.open('https://zixuephp.net/inc/qrcode_img.php?url='+url);
    }

}

使用说明:

纯js使用时id改成自己站点的id选择器来获取。如果调试不成功,可以尝试本站中的分享功能,分享时会打开新窗口,那条链接是最终要分享的,已经拼接好的参数链接,可以复制进行比对参考。

分享效果点击本站文章分享功能,效果图如下:

结合一下我自己扒的,就是图片没有显示出来。有可能是文章图片没有加class,获取不到图片。

Byethost免费提供5G虚拟空间支持FTP、MySQL、CP面板
« 上一篇 02-28
给typecho添加个打赏功能
下一篇 » 03-01

评论 (4)

插入图片
  1. 头像
    mtyxbynzz Lv.1   上海市上海市
    Windows XP · Internet Explore
    沙发

    一键分享到QQ空间、QQ好友、新浪微博、微信代码 - 沉默的博客
    [url=http://www.gw0t387n1expg95y4629ev8c9ijc05t9s.org/]umtyxbynzz[/url]
    mtyxbynzz http://www.gw0t387n1expg95y4629ev8c9ijc05t9s.org/
    amtyxbynzz

    回复
  2. 头像
    tssz Lv.1   湖南省娄底市
    Windows 10 · Google Chrome
    板凳

    这个可以有。

    回复
    1. 头像
      chen'mo 作者 Lv.5   湖南省娄底市
      Windows 7 · Google Chrome
      @ tssz

      找到了另外一个,还是获取不到图片。

      回复
    2. 头像
      chen'mo 作者 Lv.5   湖南省娄底市
      Windows 10 · Google Chrome
      @ tssz

      需要重新弄一下才行。
      顺便测试一下回复邮件通知。

      回复