구글 API 튜토리얼

Jmnote (토론 | 기여)님의 2016년 5월 25일 (수) 21:19 판 (→‎같이 보기)
Google API tutorial
구글 API 시작하기, 구글 API 튜토리얼
google-api-php-client 사용해보기

1 구글API PHP 클라이언트 설치 요건 확인

  • PHP 버전 5.2.1 이상, JSON 확장기능[1]
[root@zetawiki ~]# php -v
PHP 5.3.3 (cli) (built: Oct 30 2014 20:12:53) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
[root@zetawiki ~]# php -m | grep json
json

2 git으로 구글API 클라이언트 설치

[root@zetawiki ~]# cd /usr/share/php/vendor/
[root@zetawiki vendor]# git clone https://github.com/google/google-api-php-client.git
Initialized empty Git repository in /usr/share/php/vendor/google-api-php-client/.git/
remote: Counting objects: 8528, done.
remote: Total 8528 (delta 0), reused 0 (delta 0), pack-reused 8528
Receiving objects: 100% (8528/8528), 5.57 MiB | 1.87 MiB/s, done.
Resolving deltas: 100% (5953/5953), done.
  • 접근경로 단축을 위해 심볼릭 링크 생성
[root@zetawiki vendor]# cd ..
[root@zetawiki php]# ln -s vendor/google-api-php-client/src/Google/ Google
[root@zetawiki php]# ll | grep Google
lrwxrwxrwx  1 root root   40 Sep  3 01:11 Google -> vendor/google-api-php-client/src/Google/

3 my_batch1.php 작성

  • 예제 batch.php를 홈폴더의 my_batch1.php로 복제하여 테스트
  • my_batch1.php를 편집하여 라이브러리 위치를 맞춰주고, apiKey를 입력
[root@zetawiki php]# cd
[root@zetawiki ~]# cp /usr/share/php/vendor/google-api-php-client/examples/batch.php .
[root@zetawiki ~]# cp batch.php my_batch1.php
[root@zetawiki ~]# vi my_batch1.php
... (생략)
#### include_once "templates/base.php";
include_once "vendor/google-api-php-client/examples/templates/base.php";
echo pageHeader("Batching Queries");

/************************************************
  We're going to use the simple access to the
  books API again as an example, but this time we
  will batch up two queries into a single call.
 ************************************************/
#### require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
require_once 'Google/autoload.php';

/************************************************
  We create the client and set the simple API
  access key. If you comment out the call to
  setDeveloperKey, the request may still succeed
  using the anonymous quota.
 ************************************************/
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
#### $apiKey = "<YOUR_API_KEY>"; // Change to your API key.
$apiKey = "AIzaSyBDEuxz8GIb0K2df4_hjlMOQSU68npWYrA";
... (생략)

4 테스트

[root@zetawiki ~]# php my_batch1.php 
<h3>Results Of Call 1:</h3>The Writings of Henry David Thoreau<br /> 
Walden<br /> 
A Week on the Concord and Merrimack Rivers<br /> 
... (생략)
Catalog of Copyright Entries<br /> 
Catalog of Copyright Entries. Part 1. [A] Group 1. Books. New Series<br /> 
Catalogue of Copyright Entries<br /> 
小泉信三文庫目録<br />
→ Book API가 잘 수행되었다. 다른 API도 사용해보자.[2]

5 같이 보기

6 주석

  1. https://github.com/google/google-api-php-client
  2. 단, API 종류에 따라 사용전 Enable 절차가 필요할 수 있다.
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}