想要将 quickbooks 桌面与 php 集成

want to integrate quickbooks desktop with php

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

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

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

在 Web 连接器和 运行 中添加此文件后,它会持续 运行ning 并继续创建新的无限客户,直到我在 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 次:

  • 客户端版本
  • 服务器版本
  • 验证
  • 关闭连接

这里的含义是,无论您在 PHP 脚本中输入什么,每次 Web 连接器连接到您的服务以尝试交换数据时, 至少运行 4 次与 QuickBooks。所以每次 Web 连接器连接时,这段代码 至少运行 4 次:

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

不应该在这个文件中有这段代码。它应该在别处。从这个文件中删除它,你的问题就会消失。

相反,修改您的网络应用程序,以便在您的网络应用程序中创建新客户时,您同时对 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 连接器连接时,它可以从您已经构建的 队列中取出并处理它。

如果你看一下例子,有一个例子可以说明这一点:

具体来说,这个例子:

看起来像这样:

// 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!');
}

如果您查看文档,他们实际上会明确警告您不要做您目前所做的事情:

文档:

    // 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.