구글 API 튜토리얼

(구글 API 시작하기에서 넘어옴)

1 개요[ | ]

Google API tutorial
구글 API 시작하기, 구글 API 튜토리얼
구글API PHP 클라이언트 시작하기
google-api-php-client 사용해보기
  • google-api-php-client를 설치하고, 포함된 예제를 하나 실행해보자.

2 사전작업[ | ]

3 설치요건 확인[ | ]

  • PHP 버전 5.2.1 이상, JSON 확장기능[1]
root@zetawiki:~# php -v
PHP 5.5.9-1ubuntu4.16 (cli) (built: Apr 20 2016 14:31:27) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
root@zetawiki:~# php -m | grep json
json

4 composer 설치[ | ]

5 composer로 설치[ | ]

  • /usr/share/php/로 이동하여 composer로 구글 API 클라이언트 설치
root@zetawiki:~# cd /usr/share/php/
root@zetawiki:/usr/share/php# rm -f composer.*
root@zetawiki:/usr/share/php# composer require 'google/apiclient'
Using version ^1.1 for google/apiclient
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing google/apiclient (1.1.7)
    Downloading: 100%         

Writing lock file
Generating autoload files
  • 폴더 이동
root@zetawiki:/usr/share/php# mv vendor/ google-api
root@zetawiki:/usr/share/php#

6 인클루드 테스트[ | ]

  • 홈폴더로 이동하여 테스트 파일 작성
root@zetawiki:/usr/share/php# cd
root@zetawiki:~# vi google-api_test.php
<?php
include 'google-api/autoload.php';
$client = new Google_Client();
var_dump( $client );
root@zetawiki:~# php google-api_test.php | head 
Google_Client Object
(
    [auth:Google_Client:private] => 
    [io:Google_Client:private] => 
    [cache:Google_Client:private] => 
    [config:Google_Client:private] => Google_Config Object
        (
            [configuration:protected] => Array
                (
                    [application_name] =>
→ 이상없음

7 batch.php 테스트[ | ]

  • 예제 batch.php를 홈폴더로 복제·수정하여 테스트
  • batch.php를 편집하여 라이브러리 위치를 맞춰주고, apiKey 입력
root@zetawiki:~# cp /usr/share/php/google-api/google/apiclient/examples/batch.php .
root@zetawiki:~# vi batch.php
... (생략)
#### include_once "templates/base.php";
include_once "google-api/google/apiclient/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-api/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";
... (생략)

8 테스트[ | ]

root@zetawiki:~# php batch.php 
<h3>Results Of Call 1:</h3>Walden<br /> 
The Writings of Henry David Thoreau<br /> 
The Maine Woods<br /> 
A Week on the Concord and Merrimack Rivers<br /> 
Excursions<br /> 
Letters to Various Persons<br /> 
A Yankee in Canada<br /> 
A Hand-book of English Literature<br /> 
Neota Wilderness<br /> 
First Editions of American Authors<br /> 
<h3>Results Of Call 2:</h3>Catalog of Copyright Entries. Third Series<br /> 
English Literature of the 19th & 20th Centuries...<br /> 
Catalog of Copyright Entries. Part 1. [C] Group 3. Dramatic Composition and Motion Pictures. New Series<br /> 
Catalog of Copyright Entries. Part 1. [A] Group 1. Books. New Series<br /> 
Catalog of Copyright Entries. New Series<br /> 
Catalog of copyright entries<br /> 
Catalogue of English literature, comprising early plays, balads, poetry from Chaucer to Swinburne, books with colored plates, first editions, association books, authors' manuscripts, autograph letters...<br /> 
Catalog of Copyright Entries. Third Series<br /> 
Catalog of Copyright Entries<br /> 
小泉信三文庫目録<br />
→ Book API가 잘 수행되었다. 다른 API도 사용해보자.[2]

9 같이 보기[ | ]

10 주석[ | ]

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