웹프로그래밍

Global It Leader!!


jQuery


 
 

URL 주소 짧게 해주는 API 서비스 소개

페이지 정보

작성자 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 댓글 0건 조회 3,744회 작성일 14-01-30 00:54

본문

오픈 API


shortUrl 생성

1. 요청 URL(request url)

http://surl.kr/Api/create.php

2. 요청 변수(request parameter)

요청변수 설명
longUrl string (필수) 줄이고자 하는 긴 원본 URL을 입력합니다.
type string : 기본값 json, (json or xml) 반환받을 결과값의 형식을 지정합니다.
- get 요청시 샘플 URL
'http://lepas.pe.kr/main/?document_srl=984' 을 줄이고자 할 경우 :
http://surl.kr/Api/create.php?type=json&longUrl=http%3A%2F%2Flepas.pe.kr%2Fmain%2F%3Fdocument_srl%3D984

3. 반환값(return value)

[json 형식]
  1. 구조
    • longUrl : 긴 원본 URL
    • status : 처리 성공여부 (success or failure)
    • shortUrl : 짧은 URL
    • key : 고유 키값
  2. 구조 예제 { "longUrl":"http://lepas.pe.kr/main/?document_srl=984", "status":"success", "shortUrl":"http://surl.kr/12e", "key":"12e" }
[xml 형식]
  1. 구조
    • long_url : 긴 원본 URL
    • status : 처리 성공여부 ('success' or 'failure')
    • shor_url : 짧은 URL
    • key : 고유 키값
  2. 구조 예제 1
    <?xml version="1.0" encoding="UTF-8" ?> <surl> <long_url>http://lepas.pe.kr/main/?document_srl=984</long_url> <status>success</status> <short_url>http://surl.kr/12e</short_url> <key>12e</key> </surl>
  3. 구조 예제 2
    <?xml version="1.0" encoding="UTF-8" ?> <surl> <long_url>http://a.b</long_url> <status>failure</status> <msg>URL이 너무 짧습니다.</msg> </surl>

4. 예제소스

[php]
  • 짧은 주소 생성 함수
    1. 소스
      <?php # 짧은 주소 생성 함수 function getSurl($longUrl) { $url = 'surl.kr'; $longUrl = urlencode($longUrl); $fp = @fsockopen($url, 80,$errno,$errstr,10); if ($fp){ fwrite($fp, "GET /Api/create.php?longUrl={$longUrl} HTTP/1.0\r\nHost: $url\r\n\r\n"); while (!feof($fp)) $out .= fread($fp, 1024); fclose($fp); $out = explode("\r\n\r\n",$out); array_shift($out); $out = implode("",$out); } if($out){ $outObj = json_decode($out); return is_object($outObj) && $outObj->status=='success' && $outObj->shortUrl ? $outObj->shortUrl : $longUrl; } else{ return $longUrl; } } $longUrl = "http://lepas.pe.kr/main/?document_srl=984"; $surl = getSurl($longUrl); echo "원본 URL : " . $longUrl . " <br /> 단축 URL : " . $surl; ?>
    2. 출력 원본 URL : http://lepas.pe.kr/main/?document_srl=984
      단축 URL : http://surl.kr/12e
  • 응용함수 - 문장에서 URL 추출하여 surl로 변환후 링크걸기
    1. 소스
      <?php # 짧은 주소 생성 함수 function getSurl($longUrl) { $url = 'surl.kr'; $longUrl = urlencode($longUrl); $fp = @fsockopen($url, 80,$errno,$errstr,10); if ($fp){ fwrite($fp, "GET /Api/create.php?longUrl={$longUrl} HTTP/1.0\r\nHost: $url\r\n\r\n"); while (!feof($fp)) $out .= fread($fp, 1024); fclose($fp); $out = explode("\r\n\r\n",$out); array_shift($out); $out = implode("",$out); } if($out){ $outObj = json_decode($out); return is_object($outObj) && $outObj->status=='success' && $outObj->shortUrl ? $outObj->shortUrl : $longUrl; } else{ return $longUrl; } } # 응용함수 - 문장에서 URL 추출하여 surl로 변환후 링크걸기 function regURL($msg){ preg_match_all("/(http|https|ftp|mms)\:\/\/([\\x80-\\xff\w\/\.\?\-\#\=\&\&\%\+]+)/i",$msg,$matches); foreach($matches[0] as $longUrl){ if(strlen($longUrl) > 30 && !preg_match("/^http:\/\/surl\.kr/i",$longUrl) && !preg_match("/^http:\/\/www\.surl\.kr/i",$longUrl)){ $shortUrl = getSurl(htmlspecialchars_decode($longUrl)); $msg = str_replace($longUrl, "<a href='{$shortUrl}?PHPSESSID=28d252f879e256a21f872e2d305e7bc3' target='_blank'>{$shortUrl}</a>",$msg); } else{ $msg = str_replace($longUrl, "<a href='{$longUrl}?PHPSESSID=28d252f879e256a21f872e2d305e7bc3' target='_blank'>{$longUrl}</a>",$msg); } } return $msg; } $msg = "SURL을 소개합니다. http://lepas.pe.kr/main/?document_srl=984 왼쪽의 URL을 클릭해주세요."; $msg = regURL($msg); echo $msg; ?>
    2. 출력 SURL을 소개합니다. http://surl.kr/12e 왼쪽의 URL을 클릭해주세요.
[jquery]
  • "URL 축소" 버튼 클릭시 Textarea에서 URL을 추출하여 surl로 변환하기
    1. 소스
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="UTF-8" /> <title>Surl JQuery Demo</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> <script> function makeSurlInTextArea(objId){ var textAreaObj = $('#'+objId); var list = textAreaObj.text().match(/(http|https|ftp|mms)\:\/\/([\\x80-\\xff\w\/\.\?\-\#\=\&\&\%\+]+)/gi); if(list != null){ var cnt = list.length; for(var i=0;i<cnt;i++){ $.ajax({ type: "get", url: "http://surl.kr/Api/create.php", data: "longUrl=" + encodeURIComponent(list[i]), dataType: "json", success: function(res){ if(res.status=='success') textAreaObj.text(textAreaObj.text().replace(res.longUrl,res.shortUrl)); } }); } } return false; } </script> </head> <body> <a href="#" onclick="return makeSurlInTextArea('myTextarea')">URL 축소</a> <textarea id="myTextarea" cols="100" rows="5">SURL을 소개합니다. http://lepas.pe.kr/main/?document_srl=984 왼쪽의 URL을 클릭해주세요.
    2. 출력
      Demo Page View

댓글목록

등록된 댓글이 없습니다.

전체 142
게시물 검색
jQuery 목록
번호 제목 글쓴이 조회 날짜
102 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 5073 07-11
101 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3492 06-24
100 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3605 12-22
99 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3790 08-13
98 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3715 08-04
97 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3901 07-17
96 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 7320 06-16
95 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3575 06-04
94 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4397 05-20
93 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 4628 05-20
92 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3303 05-13
91 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3433 05-12
90 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3374 05-12
89 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3318 05-01
88 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3209 04-23
87 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3354 04-17
86 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3308 02-03
85 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3285 01-12
84 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3292 04-04
83 no_profile 오원장 쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 3750 01-30