2017-10-31 5 views
0

google APIを使用して、Googleドライブのファイルの一部をウェブサイトに表示したいと考えています。私は簡単なファイルリストの結果をここの指示で得ることができました:https://developers.google.com/drive/v3/web/quickstart/php、それはコマンドラインだけです。ウェブサイトでPHPを使用しているGoogleドライブAPIをコマンドラインで使用しないでください。

PHPを使用してオンラインで実際のWebページを操作する方法に関するチュートリアルは見つかりません。

+1

クイックスタートでは、単にphpコマンドラインを使用してPHPファイルを実行しています。しかし、PHPとGoogle API(ドライブAPI)を使用してウェブサイトを作成する場合、同じ実装が行われます。ガイドについては、[PHPとOAuth2を使ってGoogle APIに接続 - サンプルコード](https://ctrlq.org/code/20363-google-api-php-oauth2-example)、[How to create PHPのGoogleドライブアプリ](https://www.prahladyeri.com/blog/2017/01/how-to-create-google-drive-app-php.html)と[Google Client API with PHP](http ://enarion.net/programming/php/google-client-api/google-client-api-php/)。お役に立てれば。 –

+0

それは素晴らしいです!すべてのリンクありがとう。あなたは主な回答として答えなければならないので、私はあなたにこれについての信用を与えることができます。 – KeepCool

答えて

1

クイックスタートでは、PHPコマンドラインを使用してPHPファイルを実行しています。しかし、PHPとGoogle API(ドライブAPI)を使用してウェブサイトを作成する場合、同じ実装が行われます。 Connect to Google API with PHP and OAuth2 – Sample CodeHow to create a Google Drive App in PHPGoogle Client API with PHP

<?php 
require_once __DIR__ . '/vendor/autoload.php'; 


define('APPLICATION_NAME', 'Drive API PHP Quickstart'); 
define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json'); 
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json'); 
// If modifying these scopes, delete your previously saved credentials 
// at ~/.credentials/drive-php-quickstart.json 
define('SCOPES', implode(' ', array(
    Google_Service_Drive::DRIVE_METADATA_READONLY) 
)); 

if (php_sapi_name() != 'cli') { 
    throw new Exception('This application must be run on the command line.'); 
} 

/** 
* Returns an authorized API client. 
* @return Google_Client the authorized client object 
*/ 
function getClient() { 
    $client = new Google_Client(); 
    $client->setApplicationName(APPLICATION_NAME); 
    $client->setScopes(SCOPES); 
    $client->setAuthConfig(CLIENT_SECRET_PATH); 
    $client->setAccessType('offline'); 

    // Load previously authorized credentials from a file. 
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); 
    if (file_exists($credentialsPath)) { 
    $accessToken = json_decode(file_get_contents($credentialsPath), true); 
    } else { 
    // Request authorization from the user. 
    $authUrl = $client->createAuthUrl(); 
    printf("Open the following link in your browser:\n%s\n", $authUrl); 
    print 'Enter verification code: '; 
    $authCode = trim(fgets(STDIN)); 

    // Exchange authorization code for an access token. 
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); 

    // Store the credentials to disk. 
    if(!file_exists(dirname($credentialsPath))) { 
     mkdir(dirname($credentialsPath), 0700, true); 
    } 
    file_put_contents($credentialsPath, json_encode($accessToken)); 
    printf("Credentials saved to %s\n", $credentialsPath); 
    } 
    $client->setAccessToken($accessToken); 

    // Refresh the token if it's expired. 
    if ($client->isAccessTokenExpired()) { 
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); 
    file_put_contents($credentialsPath, json_encode($client->getAccessToken())); 
    } 
    return $client; 
} 

/** 
* Expands the home directory alias '~' to the full path. 
* @param string $path the path to expand. 
* @return string the expanded path. 
*/ 
function expandHomeDirectory($path) { 
    $homeDirectory = getenv('HOME'); 
    if (empty($homeDirectory)) { 
    $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); 
    } 
    return str_replace('~', realpath($homeDirectory), $path); 
} 

// Get the API client and construct the service object. 
$client = getClient(); 
$service = new Google_Service_Drive($client); 

// Print the names and IDs for up to 10 files. 
$optParams = array(
    'pageSize' => 10, 
    'fields' => 'nextPageToken, files(id, name)' 
); 
$results = $service->files->listFiles($optParams); 

if (count($results->getFiles()) == 0) { 
    print "No files found.\n"; 
} else { 
    print "Files:\n"; 
    foreach ($results->getFiles() as $file) { 
    printf("%s (%s)\n", $file->getName(), $file->getId()); 
    } 
} 

あなたがガイドの次の参照を確認することができます。

これが役に立ちます。

関連する問題