0

我需要通过 Prestashop 网络服务使用 PHP 自动更新 prestashop->customers->customer->id_default_group 值。脚本知道新值,并且在页面加载之前/加载时完成。

我可以获取客户记录,也可以只获取客户记录的“id_default_group”,但是当我尝试更改记录值和更新 Web 服务时遇到了麻烦。我返回“HTTP/1.1 400 错误请求”。

似乎我错误地尝试更新 xml 或对象。

最好只通过获取和更新“id_default_group”来完成所有这些操作,而不必检索完整的客户记录。

我有:

define('DEBUG', true);
define('PS_SHOP_PATH', 'http://www.site.com/');
define('PS_WS_AUTH_KEY', 'yuyiu');
require_once('../PSWebServiceLibrary.php');

// First : We always get the customer's list or a specific one
try
{
    $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
    $opt = array('resource' => 'customers');  // Full customer record
    //$opt = array('resource'=>'customers','display'=> '[id_default_group]','filter[id]' => '['.$_SESSION["iemIdCustomer"].']'); // Customer id_default_group value
    if (isset($_SESSION['iemIdCustomer'])){
        $opt['id'] = $_SESSION['iemIdCustomer'];
    }

    $xml = $webService->get($opt);

    // The elements from children
    $resources = $xml->children()->children();
    // $resources = $xml->children()->children()->children(); // If just getting id_default_group
}
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';
}

// Second : We update the data and send it to the web service
if (isset($_SESSION['iemIdCustomer'])){

    // Update XML with new values
    $xml->customers->customer->id_default_group = 4;
    //$resources = $xml->children()->children()->children();

    // And call the web service
    try
    {
        $opt = array('resource' => 'customers');
        //$opt = array('resource'=>'customers','display'=> '[id_default_group]','filter[id]' => '['.$_SESSION["iemIdCustomer"].']');
        $opt['putXml'] = $xml->asXML();
        $opt['id'] = $_SESSION['iemIdCustomer'];
        //$opt['display'] = 'id_default_group';
        $xml = $webService->edit($opt);
        // if WebService succeeds
        echo "Successfully updated.";
    }
    catch (PrestaShopWebserviceException $ex)
    {
        // Dealing with errors
        $trace = $ex->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 />'.$ex->getMessage();
    }
}

这对于那些知道的人来说是基本的,所以我希望你能提供帮助。

在此先感谢, 兰斯

4

2 回答 2

1

好的,我去以下工作。

以下行似乎可以解决问题:

$xml->children()->children()->id_default_group = 4;

以下是完成任务的其余代码行。

    // Get the customer's id_default_group
try
{
    $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);

    $opt = array('resource' => 'customers');
    $opt['id'] = $_SESSION["iemIdCustomer"];
    $xml = $webService->get($opt);

    // Get the elements from children of customer
    $resources = $xml->children()->children()->children();
}
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';
}

// Update the data and send it to the web service

    // Update XML with new value
    $xml->children()->children()->id_default_group = 4;

    // And call the web service
    try
    {
        $opt = array('resource' => 'customers');
        //$opt = array('resource'=>'customers','display'=> '[id_default_group]','filter[id]' => '['.$_SESSION["iemIdCustomer"].']');
        $opt['putXml'] = $xml->asXML();
        $opt['id'] = $_SESSION['iemIdCustomer'];
        //$opt['display'] = 'id_default_group';
        $xml = $webService->edit($opt);
        // if WebService don't throw an exception the action worked well and we don't show the following message
        echo "Successfully updated.";
    }
    catch (PrestaShopWebserviceException $ex)
    {
        // Here we are dealing with errors
        $trace = $ex->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 />'.$ex->getMessage();
    }

希望它可以帮助某人。

于 2013-08-18T04:37:50.390 回答
0

我认为你有这个错误,因为你正在编辑坏节点;)尝试

$xml->customer->id_default_group = 4;

那应该没问题:)

于 2013-08-14T08:06:39.340 回答