개발자도구

PHP의 API 사용법

API 키는 시스템에서 처리 할 요청에 필요합니다. 사용자가 등록하면이 사용자에 대해 API 키가 자동으로 생성됩니다. API 키는 각 요청과 함께 키 매개 변수를 통해 전송되어야합니다 (아래 전체 예제 참조). API 키가 전송되지 않거나 만료 된 경우 오류가 발생합니다. 악용을 방지하기 위해 API 키를 비공개로 유지하십시오.

 

URL 단축 요청 보내기

요청을 보내려면 변수 api 및 url이 필요한 곳에 다음 형식을 사용해야합니다. 아래 예제에서는 데모 URL이 사용되었지만 자신의 도메인 이름을 사용해야합니다. 맞춤 별칭을 요청하려면 끝에 & custom =을 추가하기만 하면 됩니다.

GET https://han.gl/api/?key=apikey&url=THELONGURLTOBESHORTENED&custom=CUSTOMALIAS

서버 응답

As before, the response will encoded in JSON format (default). This is done to facilitate cross-language usage. The first element of the response will always tell if an error has occurred (error: 1) or not (error: 0). The second element will change with respect to the first element. If there is an error, the second element will be named 'msg'. which contains the source of error, otherwise it will be named “short” which contains the short URL. (See below for an example)

오류 없음

{"error":0,"short":"https:\/\/han.gl\/DkZOb"}

오류가 발생했습니다

{"error":1,"msg":"Please enter a valid url" }

일반 텍스트 형식 사용

이제 요청이 끝날 때 & format = text를 추가하여 일반 텍스트로 응답을 요청할 수 있습니다. 오류가 발생하면 아무 것도 출력하지 않으므로 비어있는 경우 오류가 발생할 수 있습니다.


PHP에서 API 사용하기

PHP 응용 프로그램에서 API를 사용하려면 file_get_contents 또는 cURL을 통해 GET 요청을 보내야합니다. 둘 다 신뢰할 수있는 방법입니다. 아래 함수를 복사 할 수 있습니다. 모든 것이 이미 설정되어 있습니다.

 1,CURLOPT_URL => $api_url));$Response = curl_exec($curl);curl_close($curl);if($format == "text"){$Ar = json_decode($Response,TRUE);if($Ar["error"]){return $Ar["msg"];}else{return $Ar["short"];}}else{return $Response;}}?>

간단한 사용법사용자 정의 별칭 사용

echo shorten("https://google.com");?>

사용자 정의 별칭 사용

echo shorten("https://google.com", "google");?>

사용자 정의 별칭 및 텍스트 형식 사용

echo shorten("https://google.com", "google", "text");?>