hlu2/quick-books_demo

QuickBooks PHP开源版本的演示

1.1.0 2017-02-02 17:54 UTC

This package is auto-updated.

Last update: 2024-09-19 09:42:51 UTC


README

=====================

PHP客户端,用于连接到QuickBooks Online V3 REST API。

要了解更多信息,请访问官方文档网站: http://developer.intuit.com/

Build status Latest Stable Version Total Downloads

要求

  • PHP 5.6或更高版本
  • PHP OAuth 1.2.3扩展已启用

要使用OAuth1连接到API,您需要以下内容

  • developer.intuit.com的账户
  • 在您的应用中配置消费者密钥和消费者密钥,以启动OAuth 1.0a流程
  • 访问令牌和访问令牌密钥

要生成OAuth访问令牌和访问令牌密钥,请参阅我们的文档: https://developer.intuit.com/docs/0100_quickbooks_online/0100_essentials/000500_authentication_and_authorization/connect_from_within_your_app

我们还建议您尝试OAuth playground: https://appcenter.intuit.com/Playground/OAuth/IA/

您可以使用它为QuickBooks Online生产环境或沙盒环境生成访问令牌。

安装

使用以下Composer命令从[Packagist上的Intuit供应商](composer require quickbooks/v3-php-sdk)安装API客户端

 $ composer require quickbooks/v3-php-sdk
 $ composer update

如果您不熟悉Composer,请在此处阅读Composer的入门指南:https://getcomposer.org.cn/doc/00-intro.md

配置

要使用类库,请在您的PHP脚本中调用其他库/类之前将以下内容作为第一行

require "vendor/autoload.php";

(您可以使用自己的spl自动加载器;然而,推荐使用Composer的 vendor/autoload.php 钩子)。

有两种方法可以提供静态配置来准备API调用服务上下文

OAuth 1.0

传递OAuth配置数组

$dataService = DataService::Configure(array(
         	  'auth_mode' => 'oauth1',
		  'consumerKey' => "qyprdUSoVpIHrtBp0eDMTHGz8UXuSz",
		  'consumerSecret' => "TKKBfdlU1I1GEqB9P3AZlybdC8YxW5qFSbuShkG7",
		  'accessTokenKey' => "lvprdePgDHR4kSxxC7KFb8sy84TjQMVJrbITqyeaRAwBrLuq",
		  'accessTokenSecret' => "VRm1nrr17JL1RhPAFGgEjepxWeSUbGGsOwsjrKLP",
		  'QBORealmID' => "123145812836282"
));

或者您可以使用sdk.config文件作为准备服务上下文的配置文件。您需要明确传递配置文件的路径

$dataService = DataService::Configure("/Your/Path/To/sdk.config");

连接到QuickBooks Online API

在某些情况下可能需要参考类方法。要访问类参考实体,请访问: https://developer-static.intuit.com/SDKDocs/QBV3Doc/IPPPHPDevKitV3/entities/index.html

创建新资源(PUT)

在QuickBooks Online中创建客户

<?php
require "vendor/autoload.php";

use QuickBooksOnline\API\DataService\DataService;
use QuickBooksOnline\API\Core\Http\Serialization\XmlObjectSerializer;
use QuickBooksOnline\API\Data\IPPCustomer;


$dataService = DataService::Configure(array(
	         'auth_mode' => 'oauth1',
		 'consumerKey' => "Your Cosumer Key",
		 'consumerSecret' => "Your Consumer secrets",
		 'accessTokenKey' => "Your Access Token",
		 'accessTokenSecret' => "Your Access Token Secrets",
		 'QBORealmID' => "193514489870599",
		 'baseUrl' => "https://sandbox.api.intuit.com/"
));

if (!$dataService)
	exit("Problem while initializing DataService.\n");

// Add a customer
$customerObj = new IPPCustomer();
$customerObj->Name = "Name" . rand();
$customerObj->CompanyName = "CompanyName" . rand();
$customerObj->GivenName = "GivenName" . rand();
$customerObj->DisplayName = "DisplayName" . rand();
$resultingCustomerObj = $dataService->Add($customerObj);
$error = $dataService->getLastError();
if ($error != null) {
    echo "The Status code is: " . $error->getHttpStatusCode() . "\n";
    echo "The Helper message is: " . $error->getOAuthHelperError() . "\n";
    echo "The Response message is: " . $error->getResponseBody() . "\n";
}
else {
    echo "Created Customer Id={$resultingCustomerObj->Id}. Reconstructed response body:\n\n";
    $xmlBody = XmlObjectSerializer::getPostXmlFromArbitraryEntity($resultingCustomerObj, $urlResource);
    echo $xmlBody . "\n";
}
?>

更新现有资源(PUT)

要更新单个资源,您需要首先读取资源,然后更新它

// Same as adding a customer before
....
$customerObj = new IPPCustomer();
$customerObj->Name = "Name" . rand();
$customerObj->CompanyName = "CompanyName" . rand();
$customerObj->GivenName = "GivenName" . rand();
$customerObj->DisplayName = "DisplayName" . rand();
$resultingCustomerObj = $dataService->Add($customerObj);
$error = $dataService->getLastError();
if ($error != null) {
    echo "The Status code is: " . $error->getHttpStatusCode() . "\n";
    echo "The Helper message is: " . $error->getOAuthHelperError() . "\n";
    echo "The Response message is: " . $error->getResponseBody() . "\n";
}
else {
    echo "Created Customer Id={$resultingCustomerObj->Id}. Reconstructed response body:\n\n";
    $xmlBody = XmlObjectSerializer::getPostXmlFromArbitraryEntity($resultingCustomerObj, $urlResource);
    echo $xmlBody . "\n";
}

// Update Customer Obj
$resultingCustomerObj->GivenName = "New Name " . rand();
$resultingCustomerUpdatedObj = $dataService->Add($resultingCustomerObj);
if ($error != null) {
    echo "The Status code is: " . $error->getHttpStatusCode() . "\n";
    echo "The Helper message is: " . $error->getOAuthHelperError() . "\n";
    echo "The Response message is: " . $error->getResponseBody() . "\n";
}
else {
    echo "Created Customer Id={$resultingCustomerObj->Id}. Reconstructed response body:\n\n";
    $xmlBody = XmlObjectSerializer::getPostXmlFromArbitraryEntity($resultingCustomerObj, $urlResource);
    echo $xmlBody . "\n";
}

删除资源(DELETE)

要删除单个资源,可以在dataService对象上调用delete方法

...
$currentResultObj = $dataService->Delete($targetObject);
if ($crudResultObj)
	echo "Delete the purchase object that we just created.\n";
else
	echo "Did not delete the purchase object that we just created.\n";

查询资源(查询)

要使用查询,您将构建查询字符串并调用 $dataService->Query() 方法

$allAccounts = $dataServices->Query("SELECT * FROM Account");

处理错误和超时

由于各种原因,API核心中的HTTP请求可能并不总是成功。

如果发生错误,每个方法都将返回false,您应该在采取任何行动之前始终检查这一点。

$resultingCustomerObj = $dataService->Add($customerObj);
$error = $dataService->getLastError();
if ($error != null) {
    echo "The Status code is: " . $error->getHttpStatusCode() . "\n";
    echo "The Helper message is: " . $error->getOAuthHelperError() . "\n";
    echo "The Response message is: " . $error->getResponseBody() . "\n";
}
else {
    # code...
    // Echo some formatted output
    echo "Created Customer Id={$resultingCustomerObj->Id}. Reconstructed response body:\n\n";
    $xmlBody = XmlObjectSerializer::getPostXmlFromArbitraryEntity($resultingCustomerObj, $urlResource);
    echo $xmlBody . "\n";
}

有关更多信息,请参阅我们的工具文档: https://developer.intuit.com/docs/0100_quickbooks_online/0400_tools/0005_sdks/0209_php