네이버 밴드 글 가져오기

by 조쉬 posted Dec 13, 2019
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄

. 게시판 관리자에서

test

게시판을 만들어 준다.

 

1. 테마의 tail.php 나 index.php 혹은 적당한 곳에 아래 스크립트 넣기

- 자동으로 밴드글을 가져오는 역할을 함.

 

<script>

$.ajax({
    url: '/api/band_posts.php', // 요청 할 주소
    async: false, // false 일 경우 동기 요청으로 변경
    type: 'POST', // GET, PUT
    data: {
    }, // 전송할 데이터
    dataType: 'text', // xml, json, script, html
    success: function(jqXHR) {console.log('success');}, // 요청 완료 시
    error: function(jqXHR) {console.log('error');}, // 요청 실패.
});

</script>

 

 

 

 

2. 해당 경로에 아래 파일 만들어 넣기

 

/*    /api/band_posts.php  */

 

<meta charset="utf-8">
< ?
include_once('./_common.php');

/*
https://developers.band.us
네이버 밴드 개발자 사이트에서 내 서비스등록후, Aaccess Token 을 발급받는다.


밴드 목록에서 밴드키 찾기 부분, 밴드키를 찾은 후에는 해당 부분만 주석처리 한다.
*/


/************************************************************************************

$url = 'https://openapi.band.us/v2.1/bands';

$ch = curl_init();

$data = "access_token=네이버 밴드에서 발급받은 토큰키를 넣는다.";

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, 0); // 헤더 출력 여부
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 결과값을 받을것인지

$result=curl_exec($ch);

curl_close ($ch);

var_dump($result);

**************************************************************************************/

 

 

 


//밴드 게시글 가져오기

$url = 'https://openapi.band.us/v2/band/posts';

$ch = curl_init();

$data = "access_token=네이버 밴드에서 발급받은 토큰키를 넣는다.&band_key=위밴드키 찾기부분에서 얻은 밴드키를 넣는다.";

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, 0); // 헤더 출력 여부
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 결과값을 받을것인지

$result=curl_exec($ch);

curl_close ($ch);


$jsonData = json_decode($result, true);

$bo_table = "test";

// 게시판 ID를 넣는다.


$write_table = $g5['write_prefix'] . $bo_table; // 게시판 테이블 전체이름

foreach ($jsonData['result_data']['items'] as $items) {

 //포스트글 중복체크
 $cnt = sql_fetch("select wr_id from $write_table where wr_1 = '".trim($items['post_key'])."' ");
 if( $cnt>0 || $items['content']=='Uploaded event.' ) {
  continue;
 } else {

  $wr_date_time = date('Y-m-d H:i:s',substr($items['created_at'],0,10));
  $wr_subject = "네이버 밴드글 (".substr($wr_date_time,0,10).")";
  // 제목은 적당하게 넣어 준다.

  $wr_content = substr(trim(nl2br($items['content'])),0,65536);
  $wr_content = preg_replace("#[\\\]+$#", "", $wr_content);

  $mb_id = "admin";
  $wr_name = "운영자";
  // 게시자 이름도 적당하게 넣어 준다.

  $wr_password = get_encrypt_string("1a2a3a4a5a6a");
  $wr_email = '';

  $wr_num = get_next_num($write_table);
  $wr_reply = '';
  $html = "html2";

  // 포스트 키
  $wr_1 = trim($items['post_key']); 

  $tmp_photo = "";
  foreach($items['photos'] as $photo){
   $tmp_photo .= '<img src="'.$photo['url'].'" alt="band images" ><br><br>';
  }

  $wr_content .= $tmp_photo;

  $sql = " insert into $write_table
   set wr_num = '$wr_num',
     wr_reply = '$wr_reply',
     wr_comment = 0,
     ca_name = '',
     wr_option = '$html,$secret,$mail',
     wr_subject = '$wr_subject',
     wr_content = '$wr_content',
     wr_link1 = '',
     wr_link2 = '',
     wr_link1_hit = 0,
     wr_link2_hit = 0,
     wr_hit = 0,
     wr_good = 0,
     wr_nogood = 0,
     mb_id = '{$mb_id}',
     wr_password = '$wr_password',
     wr_name = '$wr_name',
     wr_email = '$wr_email',
     wr_homepage = '',
     wr_datetime = '".$wr_date_time."',
     wr_last = '".$wr_date_time."',
     wr_ip = '{$_SERVER['REMOTE_ADDR']}',
     wr_1 = '$wr_1',
     wr_2 = '',
     wr_3 = '',
     wr_4 = '',
     wr_5 = '',
     wr_6 = '',
     wr_7 = '',
     wr_8 = '',
     wr_9 = '',
     wr_10 = '' ";
  
  sql_query($sql);

  $wr_id = sql_insert_id();

  // 부모 아이디에 UPDATE
  sql_query(" update $write_table set wr_parent = '$wr_id' where wr_id = '$wr_id' ");

  // 새글 INSERT
  sql_query(" insert into {$g5['board_new_table']} ( bo_table, wr_id, wr_parent, bn_datetime, mb_id ) values ( '{$bo_table}', '{$wr_id}', '{$wr_id}', '".G5_TIME_YMDHIS."', '{$member['mb_id']}' ) ");

  // 게시글 1 증가
  sql_query("update {$g5['board_table']} set bo_count_write = (select count(wr_id) from  $write_table where 1=1) where bo_table = '{$bo_table}'");
 }
}

?>

 

 

 

3. 밴드 게시물은 등록된 역순으로 가져오게 되므로,

게시판 관리자에서 정렬순서를 wr_datetime desc 로 변경해 준다.

 

4. 다음 작업할때 쓸려고 팁자료실에 남겨 본다. 끝.

 

5. 추가

=================

 

해당 경로에 파일 만들기

/*    /api/_common.php  */

 

<?php
include_once('../common.php');
?>

 

/*

이 파일이 없으면 오류가 나고 DB 연결이 안됨.

그누보드 기본내용이라서 생략하였으나, 추가함.

*/


Articles

1 2 3