Shorturl
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; }
글걸이님의 텍스트 큐브 블로그에서 가져온 것입니다.
누구나 수정하실 수 있습니다. 위키 사용법 참고하세요. |
---|