Client-side/SCRIPT
게시물 제목+URL SNS 링크 스크립트
그곰
2011. 11. 7. 14:26
1. 잛은 URL(Short URL) 생성 -- PHP버전
/*
* SNS용 Short URL 생성
* mypikup.kr에서 제공하는 API를 이용
*/
function getShortURL($longUrl) {
$url = "http://mypickup.kr/?c=api&m=short&q=".urlencode($longUrl);
$data = file_get_contents($url);
$json = json_decode($data, true);
return urldecode($json['short_numalpha']);
}
$SNS_sURL = getShortUrl("http://".$SERVER_NAME."/".$PHP_SELF."?".$QUERY_STRING);
2. 자바스크립트로 링크 걸기
/*
* SNS용 Short URL 생성
* mypikup.kr에서 제공하는 API를 이용
*/
function getShortURL($longUrl) {
$url = "http://mypickup.kr/?c=api&m=short&q=".urlencode($longUrl);
$data = file_get_contents($url);
$json = json_decode($data, true);
return urldecode($json['short_numalpha']);
}
$SNS_sURL = getShortUrl("http://".$SERVER_NAME."/".$PHP_SELF."?".$QUERY_STRING);
/*
* SNS 링크 연결
* 사용방법 goSns(msg,url,mode)
* msg : 게시물 제목
* url : 현재 페이지 Full URL
* mode : twitter, facebook, me2day, yozm 중 서비스하는 SNS 영문명
*/
var SNS_SHORT_URL = '';
function goSns(msg,url,mode){
if(SNS_SHORT_URL == ''){
$.post("/ajax/get_short_url", { url:url },
function(data){
if(data.result == 'true')
{
SNS_SHORT_URL = data.url;
social_type(msg,SNS_SHORT_URL,mode);
}
else
{
alert(data.message);
}
}, "json");
}else{
social_type(msg,SNS_SHORT_URL,mode);
}
}
// 종류별 분기
function social_type(msg,url,mode){
// 트위터
if(mode == 'twitter'){
goTwitter(msg,url);
}
// 페이스북
else if(mode == 'facebook'){
goFaceBook(msg,url);
}
// 미투데이
else if(mode == 'me2day'){
goMe2Day(msg,url, '사이트 이름');
}
// 요즘
else if(mode == 'yozm'){
goYozmDaum(url,msg,msg);
}
}
// 트위터
function goTwitter(msg,url) {
var href = "http://twitter.com/home?status=" + encodeURIComponent(msg) + " " + encodeURIComponent(url);
var a = window.open(href, 'twitter', '');
if ( a ) {
a.focus();
}
}
// 미투데이
function goMe2Day(msg,url,tag) {
url = '"'+url+'":'+url
var href = "http://me2day.net/posts/new?new_post[body]=" + encodeURIComponent(msg) + " " + encodeURIComponent(url) + "&new_post[tags]=" + encodeURIComponent(tag);
var a = window.open(href, 'me2Day', '');
if ( a ) {
a.focus();
}
}
// 페이스북
function goFaceBook(msg,url) {
var href = "http://www.facebook.com/sharer.php?u=" + url + "&t=" + encodeURIComponent(msg);
var a = window.open(href, 'facebook', '');
if ( a ) {
a.focus();
}
}
// 요즘
function goYozmDaum(link,prefix,parameter) {
var href = "http://yozm.daum.net/api/popup/prePost?sourceid=54&link=" + encodeURIComponent(link) + "&prefix=" + encodeURIComponent(prefix) + "¶meter=" + encodeURIComponent(parameter);
var a = window.open(href, 'yozmSend', 'width=466, height=356');
if ( a ) {
a.focus();
}
}