cakephp-biztech/cakephp-xero-oauth2
Composer 安装命令:
composer require cakephp-biztech/cakephp-xero-oauth2
包简介
Xero Oauth2 API plugin for CakePHP 3.x
README 文档
README
This plugin provides access to Xero OAuth2 API for CakePHP. This plugin is wrapper around Xero PHP official SDK.
Requirements
- CakePHP 3.5 or greater
- PHP 5.6 or greater
Installation
-
You can install this plugin into your CakePHP application using composer
composer require cakephp-biztech/cakephp-xero-oauth2 -
After installation, load the plugin
Plugin::load('XeroOauth2', ['routes' => true]);
Or, you can load the plugin using the shell command:
bin/cake plugin load -r XeroOauth2 -
Run plugin migration to create table
bin/cake migrations migrate -p XeroOauth2
Setup
Now create new file to set your Xero App details.
-
Create new file
xero_config.phpinconfigdirectory:<?php return [ 'XeroOauth2' => [ 'clientId' => 'your-client-id', 'clientSecret' => 'your-client-secret', 'baseUri' => 'https://example.com', 'scope' => [ 'openid', 'email', 'profile', 'offline_access', 'accounting.settings', 'accounting.contacts', // Any other scopes needed for your application goes here ], 'successUrl' => 'http://example.com/success' ] ];
Note: Do not forget to replace "https://example.com" with your website URL in your
config/xero_config.phpfile. -
After creating the configuration file, make sure to load the file in your
bootstrap.php:Configure::load('xero_config', 'default');
Important:
When you create your Xero API App you must have to specify 'OAuth 2.0 redirect URI' to https://your-website.com/xero-oauth2/callback (replace "https://your-website.com" with your website URL).
Usage
This plugin ships with XeroOauth component which can be used to get the instance of:
\XeroAPI\XeroPHP\Api\AccountingApiviaaccountingApi()method\XeroAPI\XeroPHP\Api\AssetApiviaassetApi()method\XeroAPI\XeroPHP\Api\IdentityApiviaidentityApi()method\XeroAPI\XeroPHP\Api\ProjectApiviaprojectApi()method
Load XeroOauth component:
$this->loadComponent('XeroOauth2.XeroOauth'); $accountingApi = $this->XeroOauth->accountingApi();
Once you have an instance of \XeroAPI\XeroPHP\Api\AccountingApi::class you're dealing directly with Xero's official SDK.
The Accounting API requires we pass through a tenant ID on each request. You can get value of that using Storage component. Also, you can get additional token details from this component as well.
$this->loadComponent('XeroOauth2.Storage'); $tenantId = $this->Storage->getXeroTenantId();
Following examples shows how to get contacts from Accounting API:
src/Controller/ContactsController.php
<?php namespace App\Controller; class ContactsController extends AppController { public function index() { $this->loadComponent('XeroOauth2.Storage'); $this->loadComponent('XeroOauth2.XeroOauth'); $tenantId = $this->Storage->getXeroTenantId(); $accountingApi = $this->XeroOauth->accountingApi(); foreach ($accountingApi->getContacts($tenantId)->getContacts() as $contact) { debug([ 'id' => $contact->getContactId(), 'name' => $contact->getName(), 'email' => $contact->getEmailAddress(), ]); } } }
You can also check XeroAPI Oauth2 App repository's example file.
Reference
- Official Xero PHP SDK: https://github.com/XeroAPI/xero-php-oauth2
- Example: https://github.com/XeroAPI/xero-php-oauth2-app/blob/master/example.php
Issues
Feel free to submit issues and enhancement requests.
统计信息
- 总下载量: 1.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-04