aannnaa7 / apiclient-without-services
仅限Google API私有使用的客户端库
此包的规范存储库似乎已消失,因此该包已被冻结。
Requires
- php: >=5.4
- firebase/php-jwt: ~2.0|~3.0|~4.0
- google/apiclient-services: ^0.11
- google/auth: ^0.11
- guzzlehttp/guzzle: ~5.2|~6.0
- guzzlehttp/psr7: ^1.2
- monolog/monolog: ^1.17
- phpseclib/phpseclib: ~0.3.10|~2.0
Requires (Dev)
- cache/filesystem-adapter: ^0.3.2
- phpunit/phpunit: ~4
- squizlabs/php_codesniffer: ~2.3
- symfony/css-selector: ~2.1
- symfony/dom-crawler: ~2.1
Suggests
- cache/filesystem-adapter: For caching certs and tokens (using Google_Client::setCache)
- dev-master / 2.x-dev
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v2.0.0-RC8
- v2.0.0-RC7
- v2.0.0-RC6
- v2.0.0-RC5
- v2.0.0-RC4
- v2.0.0-RC3
- v2.0.0-RC2
- v2.0.0-RC1
- dev-v1-master / 1.1.x-dev
- v1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.0.6-beta
- 1.0.5-beta
- 1.0.4-beta
- 1.0.3-beta
- 1.0.2-beta
- v1.0.1-beta
- v1.0.0-alpha
- dev-prepare-2.2.0
- dev-fixes-1122
This package is not auto-updated.
Last update: 2020-04-17 04:03:51 UTC
README
PHP的Google APIs客户端库
描述
Google API客户端库使您能够在服务器上使用Google API,如Google+、Drive或YouTube。
测试版
此库处于测试版。我们对库的稳定性和功能有足够的信心,希望您基于它构建真实的生产应用程序。我们将努力支持库的公共和受保护接口,并在未来保持向后兼容性。虽然我们仍在测试版,但我们保留在必要时做出不兼容更改的权利。
要求
Google Cloud Platform API
如果您想调用Google Cloud Platform API,您将希望使用Google Cloud PHP库,而不是此库。
开发者文档
http://developers.google.com/api-client-library/php
安装
您可以使用Composer或简单地下载发布版
Composer
首选方法是使用composer。如果您尚未安装composer,请遵循安装说明。
安装composer后,在项目根目录中执行以下命令以安装此库
composer require google/apiclient:^2.0
最后,请确保包含自动加载器
require_once '/path/to/your-project/vendor/autoload.php';
下载发布版
如果您讨厌使用composer,您可以下载整个包。在发布版页面列出了所有稳定版本。下载任何名为google-api-php-client-[RELEASE_NAME].zip
的文件,其中包含此库及其依赖项。
解压缩您下载的zip文件,并在您的项目中包含自动加载器
require_once '/path/to/google-api-php-client/vendor/autoload.php';
有关安装和配置的更多信息,请参阅文档。
示例
查看examples/
目录以获取关键客户端功能的示例。您可以通过运行内置的PHP Web服务器在浏览器中查看它们。
$ php -S localhost:8000 -t examples/
然后浏览到您指定的主机和端口(在上面的示例中,https://:8000
)。
基本示例
// include your composer dependencies require_once 'vendor/autoload.php'; $client = new Google_Client(); $client->setApplicationName("Client_Library_Examples"); $client->setDeveloperKey("YOUR_APP_KEY"); $service = new Google_Service_Books($client); $optParams = array('filter' => 'free-ebooks'); $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); foreach ($results as $item) { echo $item['volumeInfo']['title'], "<br /> \n"; }
使用OAuth进行身份验证
这可以在
examples/simple-file-upload.php
中看到。
-
按照说明创建Web应用程序凭据
-
下载JSON凭据
-
使用
Google_Client::setAuthConfig
设置这些凭据的路径$client = new Google_Client(); $client->setAuthConfig('/path/to/client_credentials.json');
-
设置调用API所需的权限范围
$client->addScope(Google_Service_Drive::DRIVE);
-
设置您应用程序的重定向URI
// Your redirect URI can be any registered URI, but in this example // we redirect back to this same page $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; $client->setRedirectUri($redirect_uri);
-
在处理重定向URI的脚本中,将授权码交换为访问令牌
if (isset($_GET['code'])) { $token = $client->fetchAccessTokenWithAuthCode($_GET['code']); }
使用服务帐户进行身份验证
这可以在
examples/service-account.php
中看到。
-
按照说明创建服务帐户
-
下载JSON凭据
-
使用环境变量
GOOGLE_APPLICATION_CREDENTIALS
设置这些凭据的路径putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
-
告诉Google客户端使用您的服务帐户凭据进行身份验证
$client = new Google_Client(); $client->useApplicationDefaultCredentials();
-
设置调用API所需的权限范围
$client->addScope(Google_Service_Drive::DRIVE);
-
如果您已将域范围访问权限委派给服务帐户并且想要模拟用户帐户,请使用setSubject方法指定用户帐户的电子邮件地址
$client->setSubject($user_to_impersonate);
发出请求
在google-api-php-client-services中用于调用API的类是自动生成的。它们直接映射到在APIs Explorer中找到的JSON请求和响应。
对Datastore API的JSON请求看起来像这样
POST https://datastore.googleapis.com/v1beta3/projects/YOUR_PROJECT_ID:runQuery?key=YOUR_API_KEY { "query": { "kind": [{ "name": "Book" }], "order": [{ "property": { "name": "title" }, "direction": "descending" }], "limit": 10 } }
使用此库,相同的调用看起来可能像这样
// create the datastore service class $datastore = new Google_Service_Datastore($client); // build the query - this maps directly to the JSON $query = new Google_Service_Datastore_Query([ 'kind' => [ [ 'name' => 'Book', ], ], 'order' => [ 'property' => [ 'name' => 'title', ], 'direction' => 'descending', ], 'limit' => 10, ]); // build the request and response $request = new Google_Service_Datastore_RunQueryRequest(['query' => $query]); $response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request);
但是,因为每个JSON API属性的生成类都有一个对应的类,所以上述代码也可以写成这样
// create the datastore service class $datastore = new Google_Service_Datastore($client); // build the query $request = new Google_Service_Datastore_RunQueryRequest(); $query = new Google_Service_Datastore_Query(); // - set the order $order = new Google_Service_Datastore_PropertyOrder(); $order->setDirection('descending'); $property = new Google_Service_Datastore_PropertyReference(); $property->setName('title'); $order->setProperty($property); $query->setOrder([$order]); // - set the kinds $kind = new Google_Service_Datastore_KindExpression(); $kind->setName('Book'); $query->setKinds([$kind]); // - set the limit $query->setLimit(10); // add the query to the request and make the request $request->setQuery($query); $response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request);
使用的方法是个人偏好的问题,但是如果不先了解API的JSON语法,将很难使用此库,所以建议在使用这些服务之前查看APIs Explorer。
直接发出HTTP请求
如果外部应用程序需要Google身份验证,或者Google API尚未在此库中提供,则可以直接发出HTTP请求。
authorize
方法返回一个授权的Guzzle客户端,因此使用该客户端发出的任何请求都将包含相应的授权。
// create the Google client $client = new Google_Client(); /** * Set your method for authentication. Depending on the API, This could be * directly with an access token, API key, or (recommended) using * Application Default Credentials. */ $client->useApplicationDefaultCredentials(); $client->addScope(Google_Service_Plus::PLUS_ME); // returns a Guzzle HTTP Client $httpClient = $client->authorize(); // make an HTTP request $response = $httpClient->get('https://www.googleapis.com/plus/v1/people/me');
缓存
建议使用其他缓存库来提高性能。这可以通过向客户端传递一个兼容PSR-6的库来实现
$cache = new Stash\Pool(new Stash\Driver\FileSystem); $client->setCache($cache);
在这个例子中,我们使用StashPHP。使用composer将此库添加到您的项目中
composer require tedivm/stash
更新令牌
当使用刷新令牌或服务账户凭证时,当授予新的访问令牌时执行某些操作可能很有用。为此,将一个可调用对象传递给客户端上的setTokenCallback
方法
$logger = new Monolog\Logger; $tokenCallback = function ($cacheKey, $accessToken) use ($logger) { $logger->debug(sprintf('new access token received at cache key %s', $cacheKey)); }; $client->setTokenCallback($tokenCallback);
使用Charles调试您的HTTP请求
通过查看原始HTTP请求来调试API调用通常非常有用。此库支持使用Charles Web代理。下载并运行Charles,然后使用以下代码通过Charles捕获所有HTTP流量
// FOR DEBUGGING ONLY $httpClient = new GuzzleHttp\Client([ 'proxy' => 'localhost:8888', // by default, Charles runs on localhost port 8888 'verify' => false, // otherwise HTTPS requests will fail. ]); $client = new Google_Client(); $client->setHttpClient($httpClient);
现在,此库发出的所有调用都将出现在Charles UI中。
在Charles中查看SSL请求需要额外一步。转到Charles > 代理 > SSL代理设置,并添加您想要捕获的域名。对于Google API,这通常是*.googleapis.com
。
特定服务示例
YouTube: https://github.com/youtube/api-samples/tree/master/php
我如何贡献?
有关更多信息,请参阅贡献页面。特别是,我们喜欢拉取请求 - 但请确保签署贡献者许可协议。
常见问题
如果某些东西不起作用,我该怎么做?
有关库的支持,最佳询问方式是StackOverflow上的google-api-php-client标签:http://stackoverflow.com/questions/tagged/google-api-php-client
如果有关于库的特定错误,请在Github问题跟踪器中提交问题,包括失败的代码示例和任何特定错误。也可以提交功能请求,只要它们是核心库请求,而不是特定于API的:对于这些,请参阅各个API的文档,以确定最佳提交请求的地方。请尽量提供清晰的问题陈述,说明该功能将解决什么问题。
我想看看X的示例!
如果X是库的一个功能,请归档!如果X是使用特定服务的示例,最好的地方是去那些特定API的团队 - 我们更喜欢链接到他们的示例,而不是将它们添加到库中,因为这样它们可以针对库的特定版本进行固定。如果您有其他API的示例,请告诉我们,我们将很乐意在README上方添加链接!
为什么您仍然支持5.2版本?
当我们开始着手1.0.0分支时,我们知道需要修复库0.6版本中的几个基本问题。当时我们研究了库的使用情况,以及其他相关项目,并确定PHP 5.2的安装仍然有大量活跃的用户。您可以在WordPress统计中的PHP版本图表中看到这一点:[WordPress统计](https://wordpresstheme.cn/about/stats/)。我们将继续观察我们看到的使用类型,并在可能的情况下利用更新的PHP特性。
为什么Google_..._Service有奇怪的名字?
_Service类通常是从API发现文档自动生成的:[API发现文档](https://developers.google.com/discovery/)。有时API中添加了具有不寻常名称的新功能,这可能导致PHP类中存在一些意外的或不标准的命名风格。
如何处理非JSON响应类型?
某些服务默认返回XML或类似内容,而不是JSON,而库支持的是JSON。您可以通过向方法调用的最后一个参数添加一个'alt'参数来请求JSON响应
$opt_params = array(
'alt' => "json"
);
如何将字段设置为null?
库会从发送到Google API的对象中删除null,因为这是所有未初始化属性默认值。为了解决这个问题,将您希望设置为null的字段设置为Google_Model::NULL_VALUE
。这是一个占位符,在通过网络发送时会替换为真正的null。
代码质量
使用PHPUnit运行PHPUnit测试。您可以在BaseTest.php中配置API密钥和令牌以运行所有调用,但这需要在Google开发者控制台中做一些设置。
phpunit tests/
编码风格
要检查编码风格违规,运行
vendor/bin/phpcs src --standard=style/ruleset.xml -np
要自动修复(可修复的)编码风格违规,运行
vendor/bin/phpcbf src --standard=style/ruleset.xml