php 네이버 단축 url 예제 api로 뚝딱 만들기

Posted by 슈퍼너드 리보
2020. 5. 22. 01:12 프로그래밍/PHP
반응형

안녕하세요. 오늘 해볼건 네이버 단축 url인데요. 네이버 오픈api 사이트에 들어가신 다음 네이버 단축 url api를 얻기 위해 api이용신청을 해줍시다.

그런 다음 클라이언트 아이디와 클라이언트 비밀번호를 메모장에 옮겨 놓고, 아래 api 예제 코드를 여러분의 php 에디터에다 복붙해줍니다.

<?php
  // 네이버 단축URL Open API 예제
  $client_id = "YOUR_CLIENT_ID"; // 네이버 개발자센터에서 발급받은 CLIENT ID
  $client_secret = "YOUR_CLIENT_SECRET";// 네이버 개발자센터에서 발급받은 CLIENT SECRET
  $encText = urlencode("https://developers.naver.com/docs/utils/shortenurl");
  $postvars = "url=".$encText;
  //$url = "https://openapi.naver.com/v1/util/shorturl";
  //$is_post = true;
  $url = "https://openapi.naver.com/v1/util/shorturl?url=" + $encText ;
  $is_post = false;
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, $is_post);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  //curl_setopt($ch,CURLOPT_POSTFIELDS, $postvars);
  $headers = array();
  $headers[] = "X-Naver-Client-Id: ".$client_id;
  $headers[] = "X-Naver-Client-Secret: ".$client_secret;
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  $response = curl_exec ($ch);
  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  echo "status_code:".$status_code."<br>";
  curl_close ($ch);
  if($status_code == 200) {
    echo $response;
  } else {
    echo "Error 내용:".$response;
  }
?>
반응형