尝试执行AJAX时Quickbooks Web Connector parsererror

时间:2016-05-18 23:34:23

标签: php ajax wordpress file-get-contents quickbooks

使用Keith Palmer的Quickbooks课程,位于:https://github.com/consolibyte/quickbooks-php

我现在从Quickbooks Web连接器应用程序返回一个解析错误。

基本上,我正在执行这样的AJAX请求:

$('.form-item').on('submit', function(event) {
    event.preventDefault();

    var $this = $(this),
        $index = $this.data('index'),
        $company = $('input.company-' + $index).val(),
        $status = $this.data('status');

    // Current Action/Status being performed here!
    console.log(current_action);
    console.log($status);

    var data = {
        action: 'hunter_' + current_action,
        security: HUNTER_admin[current_action + '_nonce'],
        form: $this.serialize(), // serialize the form data
        company: $company
    };

    // Might need to create Customer
    if (current_action == 'create_estimate')
    {
        // This returns an array of current emails in the database and QB
        data['qb_emails'] = HUNTER_admin.emails;
    }

    console.dir(data);

    $.ajax({
        type: 'POST',
        url: HUNTER_admin.ajax_url,
        data: data,
        dataType: 'json'
    }).fail(function(jqXHR, textStatus) {
        alert("Request failed: " + textStatus);
    }).done(function(response) {
        alert('Success');
        $('.' + $status + '-' + $index).replaceWith($('<div />').html(response.statustext));

    }).always(function(response) {
        console.dir(response);
    });

现在我的functions.php wordpress文件中的ajax:

add_action('wp_ajax_hunter_create_estimate', 'hunter_create_estimate');

function hunter_create_estimate()
{
    global $wpdb;

    check_ajax_referer('create-estimate', 'security');
    $response = array();

    if (!current_user_can('manage_options'))
    {
        return $response;
        die();
    }
    require_once(get_stylesheet_directory() . '/QuickBooks.php');

    // Get All data!
    parse_str($_POST['form'], $data);

    // Check current emails in quickbooks and see if a match is found with users email, to determine if we need to create a New Customer in Quickbooks or not:

    $qb_emails = !empty($_POST['qb_emails']) ? array_map('strtolower', $_POST['qb_emails']) : array();

    if (empty($qb_emails) || !in_array(strtolower($data['email']), $qb_emails))
    {
        $data['has_email'] = false;

        // Email not set within the database, so we need to add this customer to the quickbooks Queue!
        $dsn = 'mysqli://' . DB_USER . ':' . DB_PASSWORD . '@' . DB_HOST . '/' . DB_NAME;
        $Queue = new QuickBooks_WebConnector_Queue($dsn);
        $Queue->enqueue(QUICKBOOKS_ADD_CUSTOMER);

    }
    else
        $data['has_email'] = true;


    // Now fire up the Queue that will Add the Estimate!

    $response = array_merge($data, array('company' => $_POST['company'], 'statustext' => '<p class="text">Creating Estimate...</p>', 'emails' => $_POST['qb_emails']));

    echo json_encode($response);

    die();
}

在连接Quickbooks.php文件并调用队列之前,一切都很好。现在,我在Quickbooks的提交和响应期间收到了Parser错误。

我实际上只是想测试QUICKBOOKS_ADD_CUSTOMER来电:

我的地图数组如下所示:

$map = array(
    QUICKBOOKS_ADD_CUSTOMER => array( '_quickbooks_customer_add_request', '_quickbooks_customer_add_response' ),
    QUICKBOOKS_QUERY_CUSTOMER => array('_quickbooks_query_customer_request', '_quickbooks_query_customer_response'),
);

这两个函数看起来像这样:

function _quickbooks_customer_add_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
{
    $arr = array(
        'name' => 'Doe Enterprises',
        'fname' => 'John',
        'lname' => 'Doe'
    );

    $xml = '<?xml version="1.0" encoding="utf-8"?>
        <?qbxml version="11.0"?>
        <QBXML>
            <QBXMLMsgsRq onError="stopOnError">
                <CustomerAddRq>
                    <CustomerAdd>
                        <CompanyName>' . $arr['name'] . '</CompanyName>
                        <FirstName>' . $arr['fname'] . '</FirstName>
                        <LastName>' . $arr['lname'] . '</LastName>
                    </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)
{
    global $theme_dir;

    $data = json_decode(json_encode(simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA)), true);
    file_put_contents($theme_dir . '/customer_response.txt', 'ListID = ' . mysql_escape_string($idents['ListID']) . PHP_EOL . var_export($data, true) . PHP_EOL . PHP_EOL . 'idents' . PHP_EOL . var_export($idents, true), FILE_APPEND | LOCK_EX);
}

我从Quickbooks获得的错误如下:

  

0x80040400:QuickBooks在解析提供的XML时发现错误   文本流。

1 个答案:

答案 0 :(得分:1)

您需要删除此行:

  

//在这里解析错误,需要调查一下!   require_once(trailingslashit(get_home_path())。&#39; server.php&#39;);

你不能这样测试。 Web连接器应该调用server.php。你不应该。

您可以通过AJAX排队,但qbXML请求/服务器组件的实际处理需要由Web连接器触发,而不是由您触发。

请注意文档中如何排队的示例如何不要调用任何服务器组件: