Jump to content

Webservice edit customer PS 1.6 : namespace error


T.Baron

Recommended Posts

Bonjour à tous,

 

J'ai mis en place un script qui, via les webservices de Prestashop, viendrait mettre à jour le mot de passe d'un client.

C'est une requête en POST qui récupère le client via son adresse email. Le problème c'est qu'il n'arrive pas à mettre à jour le mot de passe, il s'arrête juste avant et me retourne plusieurs erreurs du genre :

[PHP Warning #2] SimpleXMLElement::__construct(): namespace error : Namespace prefix xlink for href on id_default_group is not defined (/home/XXX/site/classes/webservice/WebserviceRequest.php, line 1374)]

 

J'ai beau relire mon code, impossible de trouver ce qui pourrait coincer. J'ai d'autres utilisations des webservices sur ce site qui fonctionnent très bien (création de client par exemple).

 

Le code PHP :

<?php
define('DEBUG', true);
define('PS_SHOP_PATH', 'XXXXXXXXX');
define('PS_WS_AUTH_KEY', 'XXXXXXXX');
require_once('./PSWebServiceLibrary.php');
require_once('./Encrypt.php');

$email=$_POST['email'];
$decrypted=$mcrypt->decrypt($_POST['passwd']);
$passwd=$decrypted;

try
{
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);

$filter = array(
    'email' => $email
);

$opt = array(
    'resource' => 'customers',
    'display' => 'full',
    'filter' => $filter
);

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

$resources = $xml->children()->children();

$customer_id = $resources->customer->id; 

$resources->customer->passwd = $passwd; 

// Appel du web service

    $opt = array('resource' => 'customers');
    $opt['putXml'] = $xml->children()->asXML(); 
    $opt['id'] = $customer_id;
    $xml = $webService->edit($opt);
    		
    echo "Successfully updated";
}
catch (PrestaShopWebserviceException $ex)
{
    $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();
}
?>

 

 

L'erreur retournée :

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): namespace error : Namespace prefix xlink for href on id_default_group is not defined (/home/XXX/site/classes/webservice/WebserviceRequest.php, line 1374)]]></message>
</error>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): 	<id_default_group xlink:href="https://site.com/api/groups/3">3</id_default (/home/XXX/site/classes/webservice/WebserviceRequest.php, line 1374)]]></message>
</error>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): 	                                                                 ^ (/home/XXX/site/classes/webservice/WebserviceRequest.php, line 1374)]]></message>
</error>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): namespace error : Namespace prefix xlink for href on id_lang is not defined (/home/XXX/site/classes/webservice/WebserviceRequest.php, line 1374)]]></message>
</error>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): 	<id_lang xlink:href="https://site.com/api/languages/1">1</id_lang> (/home/XXX/site/classes/webservice/WebserviceRequest.php, line 1374)]]></message>
</error>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): 	                                                           ^ (/home/XXX/site/classes/webservice/WebserviceRequest.php, line 1374)]]></message>
</error>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): namespace error : Namespace prefix xlink for href on group is not defined (/home/XXX/site/classes/webservice/WebserviceRequest.php, line 1374)]]></message>
</error>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): 	<group xlink:href="https://site.com/api/groups/3"> (/home/XXX/site/classes/webservice/WebserviceRequest.php, line 1374)]]></message>
</error>
<error>
<code><![CDATA[3]]></code>
<message><![CDATA[[PHP Warning #2] SimpleXMLElement::__construct(): 	                                                      ^ (/home/XXX/site/classes/webservice/WebserviceRequest.php, line 1374)]]></message>
</error>
</errors>
</prestashop>

 

 

 

Si quelqu'un a déjà eu ce genre de problème.... Merci d'avance pour votre aide :)

Link to comment
Share on other sites

  • 6 months later...
  • 1 year later...

I manage to resolve this issue.

The solution is to add the namespace to the outest element of the XML you send on edit() call.

So instead of:

<customers> 
  <customer> 
    ... 
  </customer> 
</customers>

You need to send:

<customers xmlns:xlink="http://www.w3.org/1999/xlink"> 
  <customer> 
    ... 
  </customer> 
</customers>

If you activate the debug, you will see this namespace added to the <prestashop> XML node, but when you do: 

 $opt['putXml'] = $xml->children()->asXML(); 

you are loosing this external node with the namespace reference.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...