原文地址:https://doterlin.github.io/blog/2017/01/07/fb-share/
前言
动态内容
社交平台如何对页面抓取分享信息
html
js
<title/>
<meta/>
<title/>
<meta name="description" content="">
<meta property="og:image" content="图片地址" /> <!-- facebook -->
<meta name="twitter:image" content="图片地址" /> <!-- twitter -->
所有相关的meta写法请参考平台开发文档,或者查看这篇文章来大致了解(英文的,我都能大概看懂,请放心看): What You Need to Know About Open Graph Meta ...
就是说如果你的html像这样:
...
<meta property="og:image" content="xxx" /> <!-- facebook -->
<meta name="twitter:image" content="xxx" /><!-- twitter-->
<meta name="description" content="雷好,我系要分享的内容balabala...">
<title>这是标题</title>
最后社交平台会解析出来你的要分享的信息,并加上平台自己的样式(此处以twitter效果为例):
js分享操作
<title>
<meta>
- Facebook 首先先引入fb的sdk:
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
//下面填入一个你的app id,如果还没,请在fb开发者平台注册一个
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.8&appId={your app id}";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
触发事件进行分享示例:
var shareToFbBtn = document.getElementById('fb-btn');
shareToFbBtn.onclick = function(){
FB.ui({
method: 'share',
href: "htttp://www.eaxmple.com/share.html" //这里换成你的网址
}, function(response){
//分享回调
})
}
- Twitter 简单的tw分享并不需要调用sdk,只需要跳转到一个特定页面:
var shareToTwBtn = document.getElementById('tw-btn');
var twTitle = '输入标题';
var twUrl = '要分享的页面地址';
shareToTwBtn.onclick = function(){
window.open('http://twitter.com/home/?status='.concat(encodeURIComponent(twTitle)).concat(' ').concat(encodeURIComponent(twUrl))
}
服务端生成html
html
php
<?php
//如果传入image参数就生成相关的meta,以下几条类似
if(isset($_GET["image"])){
$image=$_GET["image"];
$meta_image_fb = '<meta property="og:image" content="'.$image.'" />';
$meta_image_tw = '<meta name="twitter:image" content="'.$image.'" />';
}
if(isset($_GET["description"])){
$description=$_GET["description"];
}
if(isset($_GET["title"])){
$title=$_GET["title"];
$meta_title = '<meta property="og:title" content="'.$title.'" />';
$meta_title = '<meta name="twitter:title" content="'.$title.'" />';
}
if(isset($_GET["type"])){
$type=$_GET["type"];
$meta_type_fb = '<meta property="og:type" content="'.$type.'" />';
}
if(isset($_GET["url"])){
$url=$_GET["url"];
$meta_url_fb = '<meta property="og:url" content="'.$url.'" />';
$meta_url_tw = '<meta name="twitter:url" content="'.$url.'" />';
}
//如果传入video参数就生成video相关的meta
if(isset($_GET["video"])){
$video=$_GET["video"];
$meta_video_fb = '<meta property="og:video" content="'.$video.'" /><meta property="og:video:type" content="video/mp4" /><meta property="og:video:width" content="487" />';
$meta_video_tw = '<meta name="twitter:player" content="'.$video.'" />';
}
if(isset($_GET["card"])){
$card=$_GET["card"];
$meta_card_tw = '<meta name="twitter:card" content="'.$card.'" />';
}
?>
<!DOCTYPE html>
<html>
<head>
<!-- 这段是移动端需要的meta设置,如果是pc请按需要做相关修改 -->
<meta charset="utf-8">
<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<meta content="email=yes" name="">
<?php echo isset($image)?$meta_image_fb:'' ?>
<?php echo isset($url)?$meta_url_fb:'' ?>
<?php echo isset($video)?$meta_video_fb:'' ?>
<?php echo isset($title)?$meta_title_fb:'' ?>
<?php echo isset($type)?$meta_type_fb:'' ?>
<meta property="og:description" content="<?php echo isset($description)?$description:'' ?>" />
<?php echo isset($image)?$meta_image_tw:'' ?>
<?php echo isset($url)?$meta_url_tw:'' ?>
<?php echo isset($video)?$meta_video_tw:'' ?>
<?php echo isset($title)?$meta_title_tw:'' ?>
<?php echo isset($card)?$meta_card_tw:'<meta name="twitter:card" content="summary" />' ?>
<meta name="twitter:site:id" content="">
<meta name="twitter:title" content="<?php echo isset($title)?$title:'' ?>">
<meta name="twitter:site" content="">
<meta name="twitter:description" content="<?php echo isset($description)?$description:'' ?>" />
<meta name="description" content="<?php echo isset($description)?$description:'' ?>">
<title><?php echo isset($title)?$title:'' ?></title>
<script type="text/javascript">
//如果希望用户点击了你的分享内容后跳转到特定页面
window.location.href="http://www.eaxmple.com/xxx.html";
</script>
</head>
<body></body>
</html>
share.php
http://www.example.com/share.php
var shareTitle = encodeURIComponent('我的昵称是:'+userName); //假设你要在标题中分享用户名,需要先定义好userName
var shareContent = encodeURIComponent('这里是要分享的内容balabala....'); //如果内容也不固定请传入内容
var shareUrl = 'http://www.example.com/share.php?'+'title='+shareTitle+'&description='+shareContent; //如果有其他需要请拼接,比如+"&image="+ encodeURIComponent(shareImageUrl);
//facebook
var shareToFbBtn = document.getElementById('fb-btn');
shareToFbBtn.onclick = function(){
FB.ui({
method: 'share',
href: shareUrl
}, function(response){
//分享回调,可留空
})
}
//twitter
var shareToTwBtn = document.getElementById('tw-btn');
shareToTwBtn.onclick = function(){
window.open('http://twitter.com/home/?status='.concat(shareTitle).concat(' ').concat(encodeURIComponent(shareUrl))
}
到此就ok!
最后几句
facebook
twitter
facebook