首页 > 解决方案 > 想要将 quickbooks 桌面与 php 集成

问题描述

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

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

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

在 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.
}

标签: phpquickbooks

解决方案


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

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

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

这意味着每次 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 连接器连接时,它可以从您已经构建的队列中提取并处理它。

如果您查看示例,则有一个示例可以说明这一点:

具体来说,这个例子:

看起来像这样:

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

推荐阅读