Jump to content

Insert customers via webservice


zibrnp

Recommended Posts

Hello,

 

I have XML file with new customers data and would like to add this using web services to database.

 

My XML file with two customers looks like:

<?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>[email protected]</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>
	<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>test2</lastname>
		<firstname>test2</firstname>
		<email>[email protected]</email>
		<id_gender>1</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>

Above XML file is accessible on my localhost via:

http://localhost:8888/PHP_API/006_XML_Clients.xml

 

PHP file that is suppose to handle everything looks like:

	ini_set('display_errors', 1);
	$myXMLfile= 'http://localhost:8888/PHP_API/006_XML_Clients.xml';
	$xml = simplexml_load_file($myXMLfile);
	$customers = $xml->customer;

	require_once('PSWebServiceLibrary.php');
	define('DEBUG', true);
	define('PS_SHOP_PATH', 'http://localhost:8888/prestashop2/live_site_local/www/');
	define('PS_WS_AUTH_KEY', 'DUJE1VTU15S31EUHNHCGG7KD7RYKSBA7');

	foreach ($customers as $resources) {

		$xml = $resources;

		try	{

			$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
			$opt['resource'] = 'customers';
			$opt['postXml'] = $xml->asXML();
			$xml = $webService->add($opt);
			echo "Successfully added.";
	
		}	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();
	
		}
	}

When I run this code I get error that:

<error><code><![CDATA[41]]></code><message><![CDATA[parameter "passwd" required]]></message></error>

Even if I try to enter pass it is not working.

I am not sure what is wrong, If someone has idea and can help, please. Maybe I have wrong approach.

 

Thanks, T

Link to comment
Share on other sites

Hello,

 

I am not sure if anyone ever is going to need it. Because the fix was so simple, below I am posting correct code.

Basically I complicated it myself by adding foreach loops. By removing them code works perfect.

Also in my XML example, I use first and last name "test" this is not allowed. More realistic name is necessary.

 

Thanks

	$myXMLfile= 'http://localhost:8888/PHP_API/006_XML_Clients.xml';
	$xml = simplexml_load_file($myXMLfile);

	require_once('PSWebServiceLibrary.php');
	define('DEBUG', true);
	define('PS_SHOP_PATH', 'http://localhost:8888/prestashop/');
	define('PS_WS_AUTH_KEY', 'DUJE1VTU15S31EXHNHCGG7KD7RYKSBA7');


		try	{

			$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
			$opt['resource'] = 'customers';
			$opt['postXml'] = $xml->asXML();
			$xml = $webService->add($opt);
			echo "Successfully added.";
	
		}	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();
	
		}
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...