希望将quickbooks桌面与php集成



我正在尝试使用以下github存储库集成quickbook桌面版本:

https://github.com/consolibyte/quickbooks-php

但当我创建了一个自定义的新文件,使用您的示例代码将客户从我们的网站创建到QB桌面应用程序中时,即。mydomain.com/qb_desktop/docs/web_connector/customer.php

在web连接器中添加该文件并运行该文件后,它将继续运行,并不断创建新的无限客户,直到我在php脚本中添加"die"以强制停止此操作。

你能看看我下面的代码,让我知道我到底做错了什么吗?

提前谢谢。

<?php
$primary_key_of_your_customer = 5;
require_once '../../QuickBooks.php';
$user = 'user';
$pass = 'password';
$map = array(QUICKBOOKS_ADD_CUSTOMER => array( '_quickbooks_customer_add_request', '_quickbooks_customer_add_response'));
$errmap = array( 3070 => '_quickbooks_error_stringtoolong');
$hooks = array();
$log_level = QUICKBOOKS_LOG_DEBUG;  
$soapserver = QUICKBOOKS_SOAPSERVER_BUILTIN;
$soap_options = array();
$handler_options = array('deny_concurrent_logins' => false,
'deny_reallyfast_logins' => false);
$driver_options = array();
$callback_options = array();
$dsn = 'mysqli://root:password@localhost/quickbooks';
$Server = new QuickBooks_WebConnector_Server($dsn, $map, $errmap, $hooks, $log_level, $soapserver, QUICKBOOKS_WSDL, $soap_options, $handler_options, $driver_options, $callback_options);
$response = $Server->handle(true, true);
$Queue = new QuickBooks_WebConnector_Queue($dsn);
$Queue->enqueue(QUICKBOOKS_ADD_CUSTOMER, $primary_key_of_your_customer);
function _quickbooks_customer_add_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
{
$xml = '<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="2.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<CustomerAddRq requestID="' . $requestID . '">
<CustomerAdd>
<Name>ConsoliBYTE, LLC (' . mt_rand() . ')</Name>
<CompanyName>ConsoliBYTE, LLC</CompanyName>
<FirstName>Keith</FirstName>
<LastName>Palmer</LastName>
<BillAddress>
<Addr1>ConsoliBYTE, LLC</Addr1>
<Addr2>134 Stonemill Road</Addr2>
<City>Mansfield</City>
<State>CT</State>
<PostalCode>06268</PostalCode>
<Country>United States</Country>
</BillAddress>
<Phone>860-634-1602</Phone>
<AltPhone>860-429-0021</AltPhone>
<Fax>860-429-5183</Fax>
<Email>Keith@ConsoliBYTE.com</Email>
<Contact>Keith Palmer</Contact>
</CustomerAdd>
</CustomerAddRq>
</QBXMLMsgsRq>
</QBXML>';
return $xml;
}
function _quickbooks_customer_add_response($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents)
{
// Great, customer $ID has been added to QuickBooks with a QuickBooks
//  ListID value of: $idents['ListID']
//
// We probably want to store that ListID in our database, so we can use it
//  later. (You'll need to refer to the customer by either ListID or Name
//  in other requests, say, to update the customer or to add an invoice for
//  the customer.
}

Web连接器基于SOAP,因此您在这里实际要做的是设置Web连接器连接到的SOAP服务器。

这里需要注意的是,Web连接器每次连接时都会对SOAP服务进行多次调用(例如,对PHP脚本的许多独立HTTP请求)。至少,即使没有实际的数据可以交换,它也至少会调用4个:

  • 客户端版本
  • 服务器版本
  • 认证
  • closeConnection

这里的含义是,每当Web连接器连接到您的服务以尝试与QuickBooks交换数据时,您在PHP脚本中放入的任何内容都至少运行4次。因此,每次Web连接器连接时,这段代码至少运行4次:

$Queue = new QuickBooks_WebConnector_Queue($dsn);
$Queue->enqueue(QUICKBOOKS_ADD_CUSTOMER, $primary_key_of_your_customer);

不应该在该文件中包含这段代码。它应该在其他地方。将其从此文件中删除,您的问题就会消失。

相反,修改您的web应用程序,以便在web应用程序中创建新客户时,您同时排队请求QuickBooks。所以当你做这样的事情时,在你的应用程序中的某个地方:

// Person submitted the form, so save the data they submitted into my database
$my_customer['first_name'] = $_POST['first_name'];
$my_customer['last_name'] = $_POST['last_name'];
$my_customer_id = $MyModelOrDatabase->insert('customer_table', $my_customer);

你应该这样做:

// Person submitted the form, so save the data they submitted into my database
$my_customer['first_name'] = $_POST['first_name'];
$my_customer['last_name'] = $_POST['last_name'];
$my_customer_id = $MyModelOrDatabase->insert('customer_table', $my_customer);
if ($my_customer_id)
{
$Queue = new QuickBooks_WebConnector_Queue($dsn);
$Queue->enqueue(QUICKBOOKS_ADD_CUSTOMER, $my_customer_id);
}

这会将一条记录放入QuickBooks的队列中。然后,当Web连接器连接时,它可以从您已经构建的队列中提取并处理它。

如果你看看这些例子,有一个例子说明了这一点:

  • https://github.com/consolibyte/quickbooks-php/tree/master/docs/web_connector/example_app_web_connector

具体来说,这个例子:

  • https://github.com/consolibyte/quickbooks-php/blob/master/docs/web_connector/example_app_web_connector/handler.php

看起来像这样:

// Handle the form post
if (isset($_POST['submitted']))
{
// Save the record
mysql_query("
INSERT INTO
my_customer_table
(
name, 
fname, 
lname
) VALUES (
'" . mysql_escape_string($_POST['name']) . "', 
'" . mysql_escape_string($_POST['fname']) . "', 
'" . mysql_escape_string($_POST['lname']) . "'
)");
// Get the primary key of the new record
$id = mysql_insert_id();
// Queue up the customer add 
$Queue = new QuickBooks_WebConnector_Queue($dsn);
$Queue->enqueue(QUICKBOOKS_ADD_CUSTOMER, $id);
die('Great, queued up a customer!');
}

如果你看一下这些文档,它们实际上明确警告你不要做你迄今为止所做的事情:

  • https://github.com/consolibyte/quickbooks-php/blob/master/docs/web_connector/example_web_connector.php#L284

文档:

// NOTE: You would normally *never* want to do this in this file! This is 
//  meant as an initial test ONLY. See example_web_connector_queueing.php for more 
//  details!
// 
// IMPORTANT NOTE: This particular example of queueing something up will 
//  only ever happen *once* when these scripts are first run/used. After 
//  this initial test, you MUST do your queueing in another script. DO NOT 
//  DO YOUR OWN QUEUEING IN THIS FILE! See 
//  docs/example_web_connector_queueing.php for more details and examples 
//  of queueing things up.

最新更新