<?php
  $client_id = " ";   // <--  이곳에 $client_id 를 적습니다.
  $client_secret = " "; // <--  이곳에 Client Secret 를 적습니다.

  $encText = urlencode("만가지행동");
  $url = "https://openapi.naver.com/v1/search/book.xml?query=".$encText."&display=1&sort=sim"; // json 결과

  $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);

  $headers = array();
  $headers[] = "X-Naver-Client-Id: ".$client_id;
  $headers[] = "X-Naver-Client-Secret: ".$client_secret;
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

  $response = curl_exec ($ch);
  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close ($ch);

  //성공적으로 로드가 되면
  if($status_code == 200) {
    echo " 데이터 로드 성공~ <br/>";
    $result_array = new SimpleXMLElement($response);

  } else {
    echo "Error 내용:".$response;
  }
?>

<!DOCTYPE html>
<html lang="kr" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>네이버 도서 api </title>
  </head>
  <body>

    <!-- html 부분-->
    <p>검색어: 네이버</p>
            <table>
            <tr>
                    <th>제목</th>
                    <th>이미지</th>
                    <th>저자</th>
                    <th>출판사</th>
                    <th>가격</th>
                    <th>설명</th>
            </tr>
            <tr>
                    <td><?php echo $result_array->channel->item->title ?></td>
                    <td><img src="<?php echo $result_array->channel->item->image ?>"></td>
                    <td><?php echo $result_array->channel->item->author ?></td>
                    <td><?php echo $result_array->channel->item->publisher ?></td>
                    <td><?php echo $result_array->channel->item->price ?></td>
                    <td><?php echo $result_array->channel->item->description ?></td>
            </tr>

  </body>
</html>
