Search code examples
web-servicesprestashopprestashop-1.6

Http body empty when adding customer via prestashop webservice


I'm trying to add a new customer via webservice to my prestashop database but in appearance, everything is OK, returned code 200 but response xml body is empty and database has no new customers.

I've enabled webservice for the GET and PUT crud methods and generate API Key.

Code for the customer addition is the next one:

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();
}

When executed, this one returns a code 200 but the response body is completely empty.

<?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>

Anyone knows why is this happening? Thanks for your time.


Solution

  • Asked myself. In my case explained on top, I need one webservice per order so:

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

    This line must be inside foreach iteraction.