使用PHP从Quickbooks获取发票(借助API)
这是我的设想使用PHP从Quickbooks获取发票(借助API),php,api,quickbooks,Php,Api,Quickbooks,这是我的设想 将有一家公司在QuickBooks上拥有业务帐户 公司将向其帐户中添加客户/客户/用户 现在公司将向这些客户开具发票 我必须显示(列出)公司生成的发票,我将传递CustomerID(ClientID/UserID)作为参数,以仅获取该客户/用户/客户的发票 请注意,公司将向我们提供其凭证,以便获取其客户/客户/用户的发票。 我只需要显示发票,不需要对Quickbooks或Quickbooks API做任何其他事情。 还请注意,我已从Quickbooks Online和桌面版本(使用
提前感谢,如果您正在构建SaaS服务(例如,您每月向您的客户收取将其QuickBook连接到您的应用程序的费用),那么您可以使用IPP/IDS执行此操作。 如果您不向他们收取月费,那么您可以使用Web Connector/qbXML 无论您选择哪种连接类型,这都没有任何意义:
- docs/example_ipp_config.php
- docs/example_ipp_oauth.php
- docs/example_ipp_ids_5.php
$Service = new QuickBooks_IPP_Service_Invoice();
$perpage = 3;
for ($page = 1; $page <= 3; $page++)
{
print('PAGE ' . $page . "\n\n");
$list = $InvoiceService->findAll($Context, $realm, null, $page, $perpage);
foreach ($list as $Invoice)
{
print('Internal ID [' . $Invoice->getId() . ' => ' . $Invoice->getHeader()->getDocNumber() . ']' . "\n\n");
}
print("\n\n\n");
}
$Service=新的QuickBooks\u IPP\u服务\u发票();
每页$3;
对于($page=1;$page findAll($Context,$realm,null,$page,$perpage);
foreach($作为发票列出)
{
打印('Internal ID['.$Invoice->getId().=>'.$Invoice->getHeader()->getDocNumber().].“\n\n”);
}
打印(“\n\n\n”);
}
有关Web连接器示例(QuickBooks桌面)请下载:
- docs/example\u web\u connector\u import.php
/**
* Build a request to import invoices already in QuickBooks into our application
*/
function _quickbooks_invoice_import_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
{
// Iterator support (break the result set into small chunks)
$attr_iteratorID = '';
$attr_iterator = ' iterator="Start" ';
if (empty($extra['iteratorID']))
{
// This is the first request in a new batch
$last = _quickbooks_get_last_run($user, $action);
_quickbooks_set_last_run($user, $action); // Update the last run time to NOW()
// Set the current run to $last
_quickbooks_set_current_run($user, $action, $last);
}
else
{
// This is a continuation of a batch
$attr_iteratorID = ' iteratorID="' . $extra['iteratorID'] . '" ';
$attr_iterator = ' iterator="Continue" ';
$last = _quickbooks_get_current_run($user, $action);
}
// Build the request
$xml = '<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="' . $version . '"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<InvoiceQueryRq ' . $attr_iterator . ' ' . $attr_iteratorID . ' requestID="' . $requestID . '">
<MaxReturned>' . QB_QUICKBOOKS_MAX_RETURNED . '</MaxReturned>
<ModifiedDateRangeFilter>
<FromModifiedDate>' . $last . '</FromModifiedDate>
</ModifiedDateRangeFilter>
<IncludeLineItems>true</IncludeLineItems>
<OwnerID>0</OwnerID>
</InvoiceQueryRq>
</QBXMLMsgsRq>
</QBXML>';
return $xml;
}
/**
* Handle a response from QuickBooks
*/
function _quickbooks_invoice_import_response($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents)
{
if (!empty($idents['iteratorRemainingCount']))
{
// Queue up another request
$Queue = QuickBooks_WebConnector_Queue_Singleton::getInstance();
$Queue->enqueue(QUICKBOOKS_IMPORT_INVOICE, null, QB_PRIORITY_INVOICE, array( 'iteratorID' => $idents['iteratorID'] ));
}
// This piece of the response from QuickBooks is now stored in $xml. You
// can process the qbXML response in $xml in any way you like. Save it to
// a file, stuff it in a database, parse it and stuff the records in a
// database, etc. etc. etc.
//
// The following example shows how to use the built-in XML parser to parse
// the response and stuff it into a database.
// Import all of the records
$errnum = 0;
$errmsg = '';
$Parser = new QuickBooks_XML_Parser($xml);
if ($Doc = $Parser->parse($errnum, $errmsg))
{
$Root = $Doc->getRoot();
$List = $Root->getChildAt('QBXML/QBXMLMsgsRs/InvoiceQueryRs');
foreach ($List->children() as $Invoice)
{
$arr = array(
'TxnID' => $Invoice->getChildDataAt('InvoiceRet TxnID'),
'TimeCreated' => $Invoice->getChildDataAt('InvoiceRet TimeCreated'),
'TimeModified' => $Invoice->getChildDataAt('InvoiceRet TimeModified'),
'RefNumber' => $Invoice->getChildDataAt('InvoiceRet RefNumber'),
'Customer_ListID' => $Invoice->getChildDataAt('InvoiceRet CustomerRef ListID'),
'Customer_FullName' => $Invoice->getChildDataAt('InvoiceRet CustomerRef FullName'),
'ShipAddress_Addr1' => $Invoice->getChildDataAt('InvoiceRet ShipAddress Addr1'),
'ShipAddress_Addr2' => $Invoice->getChildDataAt('InvoiceRet ShipAddress Addr2'),
'ShipAddress_City' => $Invoice->getChildDataAt('InvoiceRet ShipAddress City'),
'ShipAddress_State' => $Invoice->getChildDataAt('InvoiceRet ShipAddress State'),
'ShipAddress_PostalCode' => $Invoice->getChildDataAt('InvoiceRet ShipAddress PostalCode'),
'BalanceRemaining' => $Invoice->getChildDataAt('InvoiceRet BalanceRemaining'),
);
QuickBooks_Utilities::log(QB_QUICKBOOKS_DSN, 'Importing invoice #' . $arr['RefNumber'] . ': ' . print_r($arr, true));
foreach ($arr as $key => $value)
{
$arr[$key] = mysql_real_escape_string($value);
}
// Store the invoices in MySQL
mysql_query("
REPLACE INTO
qb_example_invoice
(
" . implode(", ", array_keys($arr)) . "
) VALUES (
'" . implode("', '", array_values($arr)) . "'
)") or die(trigger_error(mysql_error()));
// Remove any old line items
mysql_query("DELETE FROM qb_example_invoice_lineitem WHERE TxnID = '" . mysql_real_escape_string($arr['TxnID']) . "' ") or die(trigger_error(mysql_error()));
// Process the line items
foreach ($Invoice->children() as $Child)
{
if ($Child->name() == 'InvoiceLineRet')
{
$InvoiceLine = $Child;
$lineitem = array(
'TxnID' => $arr['TxnID'],
'TxnLineID' => $InvoiceLine->getChildDataAt('InvoiceLineRet TxnLineID'),
'Item_ListID' => $InvoiceLine->getChildDataAt('InvoiceLineRet ItemRef ListID'),
'Item_FullName' => $InvoiceLine->getChildDataAt('InvoiceLineRet ItemRef FullName'),
'Descrip' => $InvoiceLine->getChildDataAt('InvoiceLineRet Desc'),
'Quantity' => $InvoiceLine->getChildDataAt('InvoiceLineRet Quantity'),
'Rate' => $InvoiceLine->getChildDataAt('InvoiceLineRet Rate'),
);
foreach ($lineitem as $key => $value)
{
$lineitem[$key] = mysql_real_escape_string($value);
}
// Store the lineitems in MySQL
mysql_query("
INSERT INTO
qb_example_invoice_lineitem
(
" . implode(", ", array_keys($lineitem)) . "
) VALUES (
'" . implode("', '", array_values($lineitem)) . "'
) ") or die(trigger_error(mysql_error()));
}
}
}
}
return true;
}
// Register in DESKTOP mode to get these. Docs:
// http://www.consolibyte.com/docs/index.php/QuickBooks_Online_via_qbXML#Connecting_with_the_.27Desktop.27_model_of_communication
$application_id = '134476472';
$application_login = 'qboe.test.test.com';
$connection_ticket = 'TGT-47-1sRm2nXMVfm$n8hb2MZfVQ';
// Create our new gateway instance
$Gateway = new QuickBooks_Gateway_OnlineEdition(
$application_id,
$application_login,
$connection_ticket);
$xml = '<QBXMLMsgsRq onError="stopOnError">
<InvoiceQueryRq>
</InvoiceQueryRq>
</QBXMLMsgsRq>';
// Send the request
$resp = $Gateway->qbxml($xml);
print($resp);
/**
*构建一个请求,将QuickBooks中已有的发票导入到我们的应用程序中
*/
函数\u quickbooks\u invoice\u import\u request($requestID,$user,$action,$ID,$extra,&$err,$last\u action\u time,$last\u ActionIdentity\u time,$version,$locale)
{
//迭代器支持(将结果集分成小块)
$attr_iteratorID='';
$attr_iterator='iterator=“Start”';
if(空($extra['iteratorID']))
{
//这是新批中的第一个请求
$last=\u quickbooks\u get\u last\u run($user,$action);
_quickbooks_set_last_run($user$action);//将上次运行时间更新到现在()
//将当前运行设置为$last
_quickbooks\u set\u current\u run($user、$action、$last);
}
其他的
{
//这是一批的延续
$attr_iteratorID='iteratorID=“”。$extra['iteratorID'].';
$attr_iterator='iterator=“Continue”';
$last=\u quickbooks\u get\u current\u run($user,$action);
}
//生成请求
$xml='0
如果您正在构建SaaS服务(例如,您每月向您的客户收取将其QuickBook连接到您的应用程序的费用),那么您可以使用IPP/IDS完成此操作。
如果您不向他们收取月费,那么您可以使用Web Connector/qbXML
无论您选择哪种连接类型,这都没有任何意义:
我必须显示(列出)由公司生成的发票,我将传递CustomerID(ClientID/UserID)作为参数来获取发票
仅限该客户/用户/客户
OAuth令牌/连接是特定于QuickBooks公司文件的,没有这样的字段“CustomerID”(或“ClientID”或“UserID”)
对于SaaS/IPP/IDS示例,请下载以下内容:
看看这些脚本:
- docs/example_ipp_config.php
- docs/example_ipp_oauth.php
- docs/example_ipp_ids_5.php
您必须在此处注册才能获得自己的OAuth令牌:
docs/example_ipp_ids_5.php脚本显示了如何获取所有客户。您可以将其更改为使用QuickBooks_ipp_Service_Invoice获取所有发票
您的代码最终将显示如下所示:
$Service = new QuickBooks_IPP_Service_Invoice();
$perpage = 3;
for ($page = 1; $page <= 3; $page++)
{
print('PAGE ' . $page . "\n\n");
$list = $InvoiceService->findAll($Context, $realm, null, $page, $perpage);
foreach ($list as $Invoice)
{
print('Internal ID [' . $Invoice->getId() . ' => ' . $Invoice->getHeader()->getDocNumber() . ']' . "\n\n");
}
print("\n\n\n");
}
$Service=新的QuickBooks\u IPP\u服务\u发票();
每页$3;
对于($page=1;$page findAll($Context,$realm,null,$page,$perpage);
foreach($作为发票列出)
{
打印('Internal ID['.$Invoice->getId().=>'.$Invoice->getHeader()->getDocNumber().].“\n\n”);
}
打印(“\n\n\n”);
}
有关Web连接器示例(QuickBooks桌面)请下载:
看看这个例子:
- docs/example\u web\u connector\u import.php
以及快速入门指南:
您将得到类似于以下内容的结果:
/**
* Build a request to import invoices already in QuickBooks into our application
*/
function _quickbooks_invoice_import_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
{
// Iterator support (break the result set into small chunks)
$attr_iteratorID = '';
$attr_iterator = ' iterator="Start" ';
if (empty($extra['iteratorID']))
{
// This is the first request in a new batch
$last = _quickbooks_get_last_run($user, $action);
_quickbooks_set_last_run($user, $action); // Update the last run time to NOW()
// Set the current run to $last
_quickbooks_set_current_run($user, $action, $last);
}
else
{
// This is a continuation of a batch
$attr_iteratorID = ' iteratorID="' . $extra['iteratorID'] . '" ';
$attr_iterator = ' iterator="Continue" ';
$last = _quickbooks_get_current_run($user, $action);
}
// Build the request
$xml = '<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="' . $version . '"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<InvoiceQueryRq ' . $attr_iterator . ' ' . $attr_iteratorID . ' requestID="' . $requestID . '">
<MaxReturned>' . QB_QUICKBOOKS_MAX_RETURNED . '</MaxReturned>
<ModifiedDateRangeFilter>
<FromModifiedDate>' . $last . '</FromModifiedDate>
</ModifiedDateRangeFilter>
<IncludeLineItems>true</IncludeLineItems>
<OwnerID>0</OwnerID>
</InvoiceQueryRq>
</QBXMLMsgsRq>
</QBXML>';
return $xml;
}
/**
* Handle a response from QuickBooks
*/
function _quickbooks_invoice_import_response($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents)
{
if (!empty($idents['iteratorRemainingCount']))
{
// Queue up another request
$Queue = QuickBooks_WebConnector_Queue_Singleton::getInstance();
$Queue->enqueue(QUICKBOOKS_IMPORT_INVOICE, null, QB_PRIORITY_INVOICE, array( 'iteratorID' => $idents['iteratorID'] ));
}
// This piece of the response from QuickBooks is now stored in $xml. You
// can process the qbXML response in $xml in any way you like. Save it to
// a file, stuff it in a database, parse it and stuff the records in a
// database, etc. etc. etc.
//
// The following example shows how to use the built-in XML parser to parse
// the response and stuff it into a database.
// Import all of the records
$errnum = 0;
$errmsg = '';
$Parser = new QuickBooks_XML_Parser($xml);
if ($Doc = $Parser->parse($errnum, $errmsg))
{
$Root = $Doc->getRoot();
$List = $Root->getChildAt('QBXML/QBXMLMsgsRs/InvoiceQueryRs');
foreach ($List->children() as $Invoice)
{
$arr = array(
'TxnID' => $Invoice->getChildDataAt('InvoiceRet TxnID'),
'TimeCreated' => $Invoice->getChildDataAt('InvoiceRet TimeCreated'),
'TimeModified' => $Invoice->getChildDataAt('InvoiceRet TimeModified'),
'RefNumber' => $Invoice->getChildDataAt('InvoiceRet RefNumber'),
'Customer_ListID' => $Invoice->getChildDataAt('InvoiceRet CustomerRef ListID'),
'Customer_FullName' => $Invoice->getChildDataAt('InvoiceRet CustomerRef FullName'),
'ShipAddress_Addr1' => $Invoice->getChildDataAt('InvoiceRet ShipAddress Addr1'),
'ShipAddress_Addr2' => $Invoice->getChildDataAt('InvoiceRet ShipAddress Addr2'),
'ShipAddress_City' => $Invoice->getChildDataAt('InvoiceRet ShipAddress City'),
'ShipAddress_State' => $Invoice->getChildDataAt('InvoiceRet ShipAddress State'),
'ShipAddress_PostalCode' => $Invoice->getChildDataAt('InvoiceRet ShipAddress PostalCode'),
'BalanceRemaining' => $Invoice->getChildDataAt('InvoiceRet BalanceRemaining'),
);
QuickBooks_Utilities::log(QB_QUICKBOOKS_DSN, 'Importing invoice #' . $arr['RefNumber'] . ': ' . print_r($arr, true));
foreach ($arr as $key => $value)
{
$arr[$key] = mysql_real_escape_string($value);
}
// Store the invoices in MySQL
mysql_query("
REPLACE INTO
qb_example_invoice
(
" . implode(", ", array_keys($arr)) . "
) VALUES (
'" . implode("', '", array_values($arr)) . "'
)") or die(trigger_error(mysql_error()));
// Remove any old line items
mysql_query("DELETE FROM qb_example_invoice_lineitem WHERE TxnID = '" . mysql_real_escape_string($arr['TxnID']) . "' ") or die(trigger_error(mysql_error()));
// Process the line items
foreach ($Invoice->children() as $Child)
{
if ($Child->name() == 'InvoiceLineRet')
{
$InvoiceLine = $Child;
$lineitem = array(
'TxnID' => $arr['TxnID'],
'TxnLineID' => $InvoiceLine->getChildDataAt('InvoiceLineRet TxnLineID'),
'Item_ListID' => $InvoiceLine->getChildDataAt('InvoiceLineRet ItemRef ListID'),
'Item_FullName' => $InvoiceLine->getChildDataAt('InvoiceLineRet ItemRef FullName'),
'Descrip' => $InvoiceLine->getChildDataAt('InvoiceLineRet Desc'),
'Quantity' => $InvoiceLine->getChildDataAt('InvoiceLineRet Quantity'),
'Rate' => $InvoiceLine->getChildDataAt('InvoiceLineRet Rate'),
);
foreach ($lineitem as $key => $value)
{
$lineitem[$key] = mysql_real_escape_string($value);
}
// Store the lineitems in MySQL
mysql_query("
INSERT INTO
qb_example_invoice_lineitem
(
" . implode(", ", array_keys($lineitem)) . "
) VALUES (
'" . implode("', '", array_values($lineitem)) . "'
) ") or die(trigger_error(mysql_error()));
}
}
}
}
return true;
}
// Register in DESKTOP mode to get these. Docs:
// http://www.consolibyte.com/docs/index.php/QuickBooks_Online_via_qbXML#Connecting_with_the_.27Desktop.27_model_of_communication
$application_id = '134476472';
$application_login = 'qboe.test.test.com';
$connection_ticket = 'TGT-47-1sRm2nXMVfm$n8hb2MZfVQ';
// Create our new gateway instance
$Gateway = new QuickBooks_Gateway_OnlineEdition(
$application_id,
$application_login,
$connection_ticket);
$xml = '<QBXMLMsgsRq onError="stopOnError">
<InvoiceQueryRq>
</InvoiceQueryRq>
</QBXMLMsgsRq>';
// Send the request
$resp = $Gateway->qbxml($xml);
print($resp);
/**
*构建一个请求,将QuickBooks中已有的发票导入到我们的应用程序中
*/
函数\u quickbooks\u invoice\u import\u request($requestID,$user,$action,$ID,$extra,&$err,$last\u action\u time,$last\u ActionIdentity\u time,$version,$locale)
{
//迭代器支持(bre)