Prestashop 1.5 Webservice更新客户id_default_group PHP

时间:2013-08-04 13:32:45

标签: web-services prestashop

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

我可以获得客户记录,或者我只能获得客户记录的“id_default_group”,但是当我尝试更改记录值并更新Web服务时遇到了麻烦。我回来了“HTTP / 1.1 400 Bad Request”。

我似乎错误地尝试更新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();
    }
}

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

提前致谢, 兰斯

2 个答案:

答案 0 :(得分:1)

好的,我继续以下工作。

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

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

Follows是其余代码执行任务的行。

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

希望能帮助别人。

答案 1 :(得分:0)

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

$xml->customer->id_default_group = 4;

那应该没问题:)