通过 prestashop webservice 添加客户时 HTTP 正文为空

Http body empty when adding customer via prestashop webservice

我正在尝试通过网络服务向我的 prestashop 数据库添加新客户,但表面上一切正常,返回代码 200,但响应 xml 正文为空且数据库没有新客户。

我已经为 GET 和 PUT crud 方法启用了网络服务并生成 API 密钥。

添加客户的代码是下一个:

require_once('../../../lib/PSWebServiceLibrary.php');

$id_customer    = 0;
$id_address     = 0;
$id_cart        = 0;
$date_now       = date("Y-m-d H:i:s");
try {

$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);

foreach ($pedidos->orders as $ord) {

    if (!$id_customer) {
        // Getting the empty XML document to send back completed
        $xml = $webService->get(array('url' => PS_SHOP_PATH . 'api/customers?schema=blank'));

        // Adding dinamic values
        // Required
        $xml->customer->passwd = Tools::encrypt($password = 'mypassword');
        $xml->customer->lastname = $ord->customer->lastname;
        $xml->customer->firstname = $ord->customer->firstname;
        $xml->customer->email = $ord->customer->customer_id.'@example.com';
        // Others
        $xml->customer->id_lang = 1;//$id_lang;
        $xml->customer->id_shop = 1;
        $xml->customer->id_shop_group = 1;
        $xml->customer->id_default_group = 3;//$id_group; // Customers
        $xml->customer->active = 1;
        $xml->customer->newsletter = 0;
        $xml->customer->newsletter_date_add = $date_now;
        $xml->customer->last_passwd_gen = $date_now;
        $xml->customer->date_add = $date_now;
        $xml->customer->date_upd = $date_now;
        $xml->customer->id_gender = 0;
        $xml->customer->associations->groups->group[0]->id = 3; // customers

        // Adding the new customer
        $opt = array('resource' => 'customers');
        $opt['postXml'] = $xml->asXML();
        $xml = $webService->add($opt);
        $id_customer = $xml->customer->id;
    }
}

}catch (PrestaShopWebserviceException $e) {
    // Here we are dealing with errors
    $trace = $e->getTrace();
    if ($trace[0]['args'][0] == 404) echo 'Bad ID';
    else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
    else echo 'Other error<br />'.$e->getMessage();
}

执行时,这个 returns 代码 200 但响应正文完全为空。

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<customer>
    <id></id>
    <id_default_group></id_default_group>
    <id_lang></id_lang>
    <newsletter_date_add></newsletter_date_add>
    <ip_registration_newsletter></ip_registration_newsletter>
    <last_passwd_gen></last_passwd_gen>
    <secure_key></secure_key>
    <deleted></deleted>
    <passwd></passwd>
    <lastname></lastname>
    <firstname></firstname>
    <email></email>
    <id_gender></id_gender>
    <birthday></birthday>
    <newsletter></newsletter>
    <optin></optin>
    <website></website>
    <company></company>
    <siret></siret>
    <ape></ape>
    <outstanding_allow_amount></outstanding_allow_amount>
    <show_public_prices></show_public_prices>
    <id_risk></id_risk>
    <max_payment_days></max_payment_days>
    <active></active>
    <note></note>
    <is_guest></is_guest>
    <id_shop></id_shop>
    <id_shop_group></id_shop_group>
    <date_add></date_add>
    <date_upd></date_upd>
<associations>
<groups>
    <group>
        <id></id>
    </group>
</groups>
</associations>
</customer>
</prestashop>

有人知道为什么会这样吗?谢谢你的时间。

问自己。在我上面解释的例子中,每个订单我需要一个网络服务,所以:

$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);

这一行必须在每次迭代中。