PrestaShop-使用Web服务和t-sql存储过程/ XML

时间:2019-05-30 11:11:42

标签: web-services tsql post request prestashop

我想使用来自tsql的Web服务更新PrestaShop产品。

我已经在sql中创建了xml变量。

根据PrestaShop文档,我必须:

  

[...]发送带有整个XML作为正文内容的POST HTTP请求[...]

使用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);

1 个答案:

答案 0 :(得分:0)

使用PrestaShop网络服务更新现有产品

这假定您的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

我希望这会有所帮助!