我正在尝试通过webservice将新客户添加到我的prestashop数据库,但在外观上,一切正常,返回代码200但响应xml正文为空,数据库没有新客户。
我为GET和PUT crud方法启用了webservice并生成了API Key。
添加客户的代码是下一个:
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();
}
执行时,此代码返回代码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>
任何人都知道为什么会这样?谢谢你的时间。
答案 0 :(得分:0)
问自己。在我的案例中,我在每个订单中需要一个webservice,所以:
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
这一行必须在foreach迭代中。