alxmsl/googleclient
Composer 安装命令:
composer require alxmsl/googleclient
包简介
Google services API library
README 文档
README
Google services API library. Supported APIs:
- OAuth2 authorization API
- Google Cloud Messaging API
- Android Publisher API: in-app products, purchases products and purchases subscriptions
Installation
For install library you need to modify your composer configuration file
"alxmsl/googleclient": "*"
And just run installation command
$ composer.phar install
OAuth2 authorization
To authorize client via Google OAuth2 you need to create WebServerApplication instance with needed scopes using client identifier, client secret and redirect uri from you console
$Client = new WebServerApplication();
$Client->setClientId(<client id>)
->setClientSecret(<client secret>)
->setRedirectUri(<redirect uri>);
...make authentication url
$Client->createAuthUrl([
'https://www.googleapis.com/auth/androidpublisher',
]
, ''
, WebServerApplication::RESPONSE_TYPE_CODE
, WebServerApplication::ACCESS_TYPE_OFFLINE
, WebServerApplication::APPROVAL_PROMPT_FORCE);
...compete authorization in browser and give authorization code. With this code you could get access token
$Token = $Client->authorizeByCode(<code>);
print((string) $Token);
You could see examples webclient.uri.php about uri creation, and webclient.authorize.php about code authentication. Already you could use completed script authorize.php
$ php bin/authorize.php
Using: /usr/local/bin/php bin/authorize.php [-h|--help] [-o|--code] -c|--client -r|--redirect -s|--scopes -e|--secret
-h, --help - show help
-o, --code - authorization code
-c, --client - client id
-r, --redirect - redirect uri
-s, --scopes - grant scopes
-e, --secret - client secret
$ php bin/authorize.php --client='my-client@id' --secret='clientsecret' --redirect='http://example.com/oauth2callback'
--scopes='https://www.googleapis.com/auth/androidpublisher'
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=my-client@id
&redirect_uri=http%3A%2F%2Fexample.com%2Foauth2callback&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher
&access_type=offline&approval_prompt=force
$ php bin/authorize.php --client='my-client@id' --secret='clientsecret' --code='authorization/code'
access token: ya.AccES5_T0keN
expires in: 3558
id token: id.T0kEn
refresh token: refrE5H_T0KEN
token type: Bearer
Of course, I created scripts for token refreshing
$ php bin/refresh.php
Using: /usr/local/bin/php bin/refresh.php [-h|--help] -c|--client -e|--secret -t|--token
-h, --help - show help
-c, --client - client id
-e, --secret - client secret
-t, --token - refresh token
...and token revoking
$ php bin/revoke.php --help
Using: /usr/local/bin/php bin/revoke.php [-h|--help] -t|--token
-h, --help - show help
-t, --token - revoked token
Google Cloud Messaging
For create Google Cloud Message you need to create child for
class PayloadData and define getDataFields method. You could see example below
or in gcm.php
// Create payload data class
final class NewPayloadData extends PayloadData {
protected function getDataFields() {
return [
'test' => 'test_01',
];
}
}
// Create and initialize payload message instance
$Message = new PayloadMessage();
$Message->setRegistrationIds('DeV1CeT0kEN')
->setType(PayloadMessage::TYPE_JSON)
->setData(new NewPayloadData());
// Create GCM client
$Client = new Client();
$Client->getRequest()->setConnectTimeout(60)
->setSslVersion(6);
$Client->setAuthorizationKey('aUTH0R1Z4t1oNKEy');
// ...and send the message
$Response = $Client->send($Message);
var_dump($Response);
You could use completed script
$ php bin/gcm.php
Using: /usr/local/bin/php bin/gcm.php [-h|--help] [-d|--data] -i|--id -k|--key
-h, --help - show help
-d, --data - payload data
-i, --id - device registration id
-k, --key - authorization key
$ php bin/gcm.php --id='DeV1CeT0kEN' --key='aUTH0R1Z4t1oNKEy' --data='{"test":"test_01"}'
success: 1
failure: 0
In-app purchases
You could use In-App products API to manage products of your application. For example, you could get all product prices for all end-user countries
$Client = new Client();
$Client->setPackage(<package name>)
->setAccessToken(<access token>);
/** @var InAppProductsResource $Resource */
$Resource = $Client->get(<product id>);
var_dump($Resource->getPrices());
Already, you could use inappproducts.get.php to get info about products
$ php bin/inappproducts.get.php --help
Using: /usr/local/bin/php bin/inappproducts.get.php [-h|--help] -a|--access [-p|--package] -r|--product
-h, --help - show help
-a, --access - access token
-p, --package - package name
-r, --product - product id
Purchases products
Using Purchases.Products API you could check user purchases in third-party server application. For example if purchase purchased and does not cancel now:
use alxmsl\Google\AndroidPublisher\Purchases\Client;
use alxmsl\Google\AndroidPublisher\Purchases\Products\Resource as ProductsResource;
$Client = new Client();
$Client->setPackage(<package name>)
->setAccessToken(<access token>);
/** @var ProductsResource $Resource */
$Resource = $Client->get(<product id>, <purchase token>);
var_dump($Resource->isPurchased() && !$Resource->isCancelled());
Purchases subscriptions
This library allows all operations over user subscriptions using some scripts: get, cancel, defer, refund and revoke
How to check subscription, for example:
use alxmsl\Google\AndroidPublisher\Purchases\Subscription\Resource as SubscriptionsResource;
use alxmsl\Google\AndroidPublisher\Purchases\Subscription\SubscriptionsClient;
$Client = new SubscriptionsClient();
$Client->setPackage(<package name>)
->setAccessToken(<access token>);
/** @var SubscriptionsResource $Resource */
$Resource = $Client->get(<subscription id>, <purchase token>);
var_dump($Resource->isAutoRenewing() && !$Resource->isExpired());
Tests
For completely tests running just call phpunit command
$ phpunit
PHPUnit 4.7.3 by Sebastian Bergmann and contributors.
..........................................
Time: 118 ms, Memory: 7.25Mb
OK (42 tests, 365 assertions)
License
Copyright 2015 Alexey Maslov alexey.y.maslov@gmail.com
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
alxmsl/googleclient 适用场景与选型建议
alxmsl/googleclient 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.22k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「google」 「gcm」 「apis」 「inapp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alxmsl/googleclient 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alxmsl/googleclient 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alxmsl/googleclient 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii 2 Apns and Gcm together
Firebase cloud messaging http v1 php
Standalone PHP library for easy devices notifications push.
Laravel package to send push notifications to mobile devices (apns, gcm)
An API Development Kit (ADK) to consistently build and manage APIs
An HTTP Kipchak driver (dependency) for the Kipchak API Development Kit (ADK)
统计信息
- 总下载量: 1.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2014-07-19