我想使用 tsql 的 Web 服务更新 PrestaShop 产品。
我已经在 sql 中创建了 xml 变量。
根据PrestaShop文档,我必须:
[...]发送带有整个XML作为正文内容的POST HTTP请求[...]
使用 Get 对于从 sql oa_mothods没有任何问题,但是 POST/PUT 任何 xml 变量来创建或更新现有产品的语法是什么?
declare @xmlRIA xml
set @xmlRIA = CAST('<?xml version="1.0" encoding="UTF-8"?>
<prestashop>
<customer>
<id></id>
<id_default_group>3</id_default_group>
<id_lang>1</id_lang>
<newsletter_date_add>0000-00-00 00:00:00</newsletter_date_add>
<ip_registration_newsletter></ip_registration_newsletter>
<last_passwd_gen>2017-04-29 05:34:22</last_passwd_gen>
<secure_key></secure_key>
<deleted>0</deleted>
<passwd></passwd>
<lastname>test</lastname>
<firstname>test</firstname>
<email>kow@testde.com</email>
<id_gender>2</id_gender>
<birthday>0000-00-00</birthday>
<newsletter>0</newsletter>
<optin>0</optin>
<website></website>
<company></company>
<siret></siret>
<ape></ape>
<outstanding_allow_amount>0.000000</outstanding_allow_amount>
<show_public_prices>0</show_public_prices>
<id_risk>0</id_risk>
<max_payment_days>0</max_payment_days>
<active>1</active>
<note></note>
<is_guest>0</is_guest>
<id_shop>1</id_shop>
<id_shop_group>1</id_shop_group>
<date_add>2017-04-29 11:34:22</date_add>
<date_upd>2017-04-29 11:34:22</date_upd>
<reset_password_token></reset_password_token>
<reset_password_validity>0000-00-00 00:00:00</reset_password_validity>
<associations>
<groups>
<group>
<id>3</id>
</group>
</groups>
</associations>
</customer>
</prestashop>
'as XML)
-- Open a connection
EXEC @ret = sp_OACreate 'MSXML2.ServerXMLHTTP', @token OUT;
if @ret <> 0 begin set @MessageError = 'sp_OACreate MSXML2.ServerXMLHttp.3.0 failed' goto final end
EXEC @ret = sp_OAMethod @token, 'open', NULL, 'POST', @url, 'false';
if @ret <> 0 begin set @MessageError = 'sp_OACreate open' goto final end
EXEC @ret = sp_OAMethod @token, 'setRequestHeader', NULL, 'Authentication', @authHeader;
if @ret <> 0 begin set @MessageError = 'sp_OACreate Authentication failed' goto final end
EXEC @ret = sp_OAMethod @token, 'setRequestHeader', NULL, 'Content-type', @contentType;
if @ret <> 0 begin set @MessageError = 'sp_OACreate Content-type failed' goto final end
EXEC @ret = sp_OAMethod @token, 'send', NULL, '';
if @ret <> 0 begin set @MessageError = 'sp_OAMethod send failed' goto final end
-- Handle response
EXEC @ret = sp_OAGetProperty @token, 'status', @status OUT;
if @ret <> 0 begin set @MessageError = 'sp_OAGetProperty status' goto final end
EXEC @ret = sp_OAGetProperty @token, 'statusText', @statusText OUT;
if @ret <> 0 begin set @MessageError = 'sp_OAGetProperty statusText failed' goto final end
EXEC @ret = sp_OAMethod @token, 'send', NULL, @xmlria
IF @ret <> 0 RAISERROR('sp_OAMethod SEND 2 failed.', 10, 1);
使用 PrestaShop Web 服务更新现有产品
这假设您的 XML 存储在 SimpleXML 对象(在 PHP 中(中,并且现有产品 ID 为 1。
<?php
include(__DIR__.'/config/config.inc.php');
include(__DIR__.'/init.php');
include(__DIR__.'/PSWebServiceLibrary.php');
/* Connect to the PrestaShop Web-service */
define('PS_SHOP_URL', 'http://localhost/prestashop');
define('PS_WS_AUTH_KEY', 'YOURWSKEY');
$ws = new PrestaShopWebservice(PS_SHOP_URL, PS_WS_AUTH_KEY, false);
$result = $ws->edit(['resource' => 'products', 'id' => 1, 'putXml' => $xml->asXML()]);
echo '<pre>';
print_r($result);
echo '</pre>';
更多示例和链接
您可以在此处找到PrestaShop网络服务文档:https://devdocs.prestashop.com/1.7/development/webservice/
您可以在此处找到该库:https://github.com/PrestaShop/PrestaShop-webservice-lib/blob/master/PSWebServiceLibrary.php
您可以在此处找到更多示例(检索、添加、更新、删除等(:https://github.com/PrestaShop/PrestaShop-webservice-lib/tree/master/examples
我希望这有帮助!