차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

tech:shorturl [2013/04/05 01:53] – 새로 만듦 14.32.18.124tech:shorturl [2016/07/12 00:56] (현재) – 바깥 편집 127.0.0.1
줄 1: 줄 1:
 +{{tag>shorturl}}
 +======Shorturl======
 +
 +
 +  * [[http://pat.im/807|짧은 주소로 바꾸기 (bit.ly / j.mp / is.gd / v.gd / to.ly / goo.gl / durl.me / tinyurl.com)]]
 +
 +
 +<code php>
 +function get_shortURL($longURL, $shortURL_domain) {
 +  switch($shortURL_domain) {
 +    case "bit.ly" :
 +    case "j.mp" :
 +      $login = "bit.ly/j.mp 계정 아이디";
 +      $api_key = "API 열쇠";
 +      $curlopt_url = "http://api.".$shortURL_domain."/v3/shorten?login=".$login."&apiKey=".$api_key."&uri=".$longURL."&format=txt";
 +      break;
 +    case "is.gd" :
 +      $curlopt_url = "http://is.gd/api.php?longurl=".$longURL;
 +      break;
 +    case "v.gd" :
 +      $curlopt_url = "http://v.gd/create.php?format=simple&url=".$longURL;
 +      break;
 +    case "to.ly" :
 +      $curlopt_url = "http://to.ly/api.php?longurl=".$longURL;
 +      break;
 +    case "goo.gl" :
 +      $api_key = "API 열쇠";
 +      $curlopt_url = "https://www.googleapis.com/urlshortener/v1/url?key=".$api_key;
 +      break;
 +    case "durl.me" :
 +      $curlopt_url = "http://durl.me/api/Create.do?type=json&longurl=".$longURL;
 +      break;   
 +    case "tinyurl" :
 +      $curlopt_url = "http://tinyurl.com/api-create.php?url=".$longURL;
 +      break;
 +
 +  } 
 +  $ch = curl_init();
 +  //$timeout = 10;
 +  
 +  curl_setopt($ch, CURLOPT_URL, $curlopt_url);
 +  curl_setopt($ch, CURLOPT_HEADER, false);
 +  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 +  //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 +
 +  if($shortURL_domain ===== "goo.gl" || $shortURL_domain ===== "durl.me") {
 +    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 +    curl_setopt($ch, CURLOPT_POST, true); 
 +    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
 +    $jsonArray = array('longUrl' => $longURL); 
 +    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsonArray)); 
 +    $shortURL = curl_exec($ch);
 +    curl_close($ch);
 +    $result_array = json_decode($shortURL, true);
 +
 +    if($result_array['shortUrl']) return $result_array['shortUrl'];  // durl.me
 +    else if($result_array['id']) return $result_array['id']; // goo.gl
 +    else return false;
 +  }
 +
 +  $shortURL = curl_exec($ch);
 +  curl_close($ch);
 +
 +  // bit.ly(j.mp) 주소 끝에 붙은 줄바꿈 문자를 없앰 
 +  if( ($shortURL_domain ===== "j.mp" || $shortURL_domain  ===== "bit.ly") && bin2hex(substr($shortURL, -1, 1)) ===== "0a") $shortURL = substr($shortURL, 0, strlen($shortURL)-1);
 +
 +  return $shortURL;
 +}
 +</code>
 +
 +[[http://pat.im/|글걸이]]님의 텍스트 큐브  블로그에서 가져온 것입니다. 
 +
 +
 +^  누구나 수정하실 수 있습니다. [[http://vaslor.net/syntax|위키 사용법]] 참고하세요.  ^
 +
 +