"구글 API 튜토리얼"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-<source +<syntaxhighlight , -</source> +</syntaxhighlight>))
 
(다른 사용자 한 명의 중간 판 19개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
;Google API tutorial
;Google API tutorial
;구글 API 시작하기, 구글 API 튜토리얼
;구글 API 시작하기, 구글 API 튜토리얼
;구글API PHP 클라이언트 시작하기
;google-api-php-client 사용해보기
;google-api-php-client 사용해보기
* google-api-php-client를 설치하고, 포함된 예제를 하나 실행해보자.


==구글API PHP 클라이언트 설치 요건 확인==
==사전작업==
*[[구글 API 키 발급]]
 
==설치요건 확인==
{{참고|PHP 버전 확인}}
{{참고|PHP 버전 확인}}
{{참고|PHP 모듈 설치 확인}}
{{참고|PHP 모듈 설치 확인}}
*PHP 버전 5.2.1 이상, JSON 확장기능<ref>https://github.com/google/google-api-php-client</ref>
*PHP 버전 5.2.1 이상, JSON 확장기능<ref>https://github.com/google/google-api-php-client</ref>
<source lang='console'>
<syntaxhighlight lang='console'>
[root@zetawiki ~]# php -v
root@zetawiki:~# php -v
PHP 5.3.3 (cli) (built: Oct 30 2014 20:12:53)  
PHP 5.5.9-1ubuntu4.16 (cli) (built: Apr 20 2016 14:31:27)  
Copyright (c) 1997-2010 The PHP Group
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
</source>
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
<source lang='console'>
</syntaxhighlight>
[root@zetawiki ~]# php -m | grep json
<syntaxhighlight lang='console'>
root@zetawiki:~# php -m | grep json
json
json
</source>
</syntaxhighlight>
 
==composer 설치==
{{참고|composer 설치}}
 
==composer로 설치==
* [[/usr/share/php/]]로 이동하여 composer로 구글 API 클라이언트 설치
<syntaxhighlight lang='console'>
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
</syntaxhighlight>
* 폴더 이동
<syntaxhighlight lang='console'>
root@zetawiki:/usr/share/php# mv vendor/ google-api
root@zetawiki:/usr/share/php#
</syntaxhighlight>


==git으로 구글API 클라이언트 설치==
==인클루드 테스트==
*[[/usr/share/php/vendor/]]로 이동하여 git clone 설치
* 홈폴더로 이동하여 테스트 파일 작성
<source lang='console'>
<syntaxhighlight lang='console'>
[root@zetawiki ~]# cd /usr/share/php/vendor/
root@zetawiki:/usr/share/php# cd
[root@zetawiki vendor]# git clone https://github.com/google/google-api-php-client.git
root@zetawiki:~# vi google-api_test.php
Initialized empty Git repository in /usr/share/php/vendor/google-api-php-client/.git/
</syntaxhighlight>
remote: Counting objects: 8528, done.
<syntaxhighlight lang='php'>
remote: Total 8528 (delta 0), reused 0 (delta 0), pack-reused 8528
<?php
Receiving objects: 100% (8528/8528), 5.57 MiB | 1.87 MiB/s, done.
include 'google-api/autoload.php';
Resolving deltas: 100% (5953/5953), done.
$client = new Google_Client();
</source>
var_dump( $client );
*접근경로 단축을 위해 심볼릭 링크 생성
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
[root@zetawiki vendor]# cd ..
root@zetawiki:~# php google-api_test.php | head
[root@zetawiki php]# ln -s vendor/google-api-php-client/src/Google/ Google
Google_Client Object
[root@zetawiki php]# ll | grep Google
(
lrwxrwxrwx  1 root root  40 Sep  3 01:11 Google -> vendor/google-api-php-client/src/Google/
    [auth:Google_Client:private] =>
</source>
    [io:Google_Client:private] =>
    [cache:Google_Client:private] =>
    [config:Google_Client:private] => Google_Config Object
        (
            [configuration:protected] => Array
                (
                    [application_name] =>  
</syntaxhighlight>
:→ 이상없음


==my_batch1.php 작성==
==batch.php 테스트==
* 예제 batch.php를 홈폴더의 my_batch1.php로 복제하여 테스트
* 예제 batch.php를 홈폴더로 복제·수정하여 테스트
* my_batch1.php를 편집하여 라이브러리 위치를 맞춰주고, apiKey를 입력
* batch.php를 편집하여 라이브러리 위치를 맞춰주고, apiKey 입력
<source lang='console'>
<syntaxhighlight lang='console'>
[root@zetawiki php]# cd
root@zetawiki:~# cp /usr/share/php/google-api/google/apiclient/examples/batch.php .
[root@zetawiki ~]# cp /usr/share/php/vendor/google-api-php-client/examples/batch.php .
root@zetawiki:~# vi batch.php
[root@zetawiki ~]# cp batch.php my_batch1.php
</syntaxhighlight>
[root@zetawiki ~]# vi my_batch1.php
<syntaxhighlight lang='php' highlight='2,3,11,12,22,23'>
</source>
<source lang='php'>
... (생략)
... (생략)
#### include_once "templates/base.php";
#### include_once "templates/base.php";
include_once "vendor/google-api-php-client/examples/templates/base.php";
include_once "google-api/google/apiclient/examples/templates/base.php";
echo pageHeader("Batching Queries");
echo pageHeader("Batching Queries");


58번째 줄: 96번째 줄:
  ************************************************/
  ************************************************/
#### require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
#### require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
require_once 'Google/autoload.php';
require_once "google-api/autoload.php";


/************************************************
/************************************************
71번째 줄: 109번째 줄:
$apiKey = "AIzaSyBDEuxz8GIb0K2df4_hjlMOQSU68npWYrA";
$apiKey = "AIzaSyBDEuxz8GIb0K2df4_hjlMOQSU68npWYrA";
... (생략)
... (생략)
</source>
</syntaxhighlight>


==테스트==
==테스트==
<source lang='console'>
<syntaxhighlight lang='console'>
[root@zetawiki ~]# php my_batch1.php  
root@zetawiki:~# php batch.php  
<h3>Results Of Call 1:</h3>The Writings of Henry David Thoreau<br />  
<h3>Results Of Call 1:</h3>Walden<br />
Walden<br />  
The Writings of Henry David Thoreau<br />  
The Maine Woods<br />  
A Week on the Concord and Merrimack Rivers<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 />  
Catalog of Copyright Entries<br />  
Catalog of Copyright Entries. Part 1. [A] Group 1. Books. New Series<br />
Catalogue of Copyright Entries<br />
小泉信三文庫目録<br />  
小泉信三文庫目録<br />  
</source>
</syntaxhighlight>
:→ Book API가 잘 수행되었다. 다른 API도 사용해보자.<ref>단, API 종류에 따라 사용전 Enable 절차가 필요할 수 있다.</ref>
:→ Book API가 잘 수행되었다. 다른 API도 사용해보자.<ref>단, API 종류에 따라 사용전 Enable 절차가 필요할 수 있다.</ref>


90번째 줄: 140번째 줄:
*[[구글 API 튜토리얼 2 - ISBN으로 책정보 조회]]
*[[구글 API 튜토리얼 2 - ISBN으로 책정보 조회]]
*[[구글 애널리틱스 API 시작하기]]
*[[구글 애널리틱스 API 시작하기]]
*[[The calling IP address 135.79.246.80 does not match the IP restrictions configured on the API key. Please use the API Console to update your key restrictions.]]


==주석==
==주석==

2021년 9월 24일 (금) 23:27 기준 최신판

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 }}