Jump to content

Ajour de commande via API


Recommended Posts

Bonjour,

 

J'ai une sale erreur quand j'ajoute des commandes. A mon avis elle vient de CURL mais je parviens pas à en savoir plus !

 

J'instancie un objet webservice qui continet mes infos de connexion à l'api.

Le XML que j'envoie ressemble à ça :

 

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<order>
<id></id>
<id_address_delivery>23</id_address_delivery>
<id_address_invoice>23</id_address_invoice>
<id_cart>3</id_cart>
<id_currency>2</id_currency>
<id_lang>5</id_lang>
<id_customer>24</id_customer>
<id_carrier>8</id_carrier>
<current_state>1</current_state>
<module>cheque</module>
<invoice_number>1</invoice_number>
<invoice_date>2013-03-21 16:56:30</invoice_date>
<delivery_number></delivery_number>
<delivery_date></delivery_date>
<valid>0</valid>
<date_add></date_add>
<date_upd></date_upd>
<id_shop_group>1</id_shop_group>
<id_shop>1</id_shop>
<secure_key></secure_key>
<payment>Chèque</payment>
<recyclable>0</recyclable>
<gift>0</gift>
<gift_message></gift_message>
<total_discounts>0.00</total_discounts>
<total_discounts_tax_incl>0.00</total_discounts_tax_incl>
<total_discounts_tax_excl>0.00</total_discounts_tax_excl>
<total_paid>45.90</total_paid>
<total_paid_tax_incl>45.90</total_paid_tax_incl>
<total_paid_tax_excl>36.9</total_paid_tax_excl>
<total_paid_real>45.90</total_paid_real>
<total_products>45.90</total_products>
<total_products_wt>45.90</total_products_wt>
<total_shipping>10.00</total_shipping>
<total_shipping_tax_incl>10.00</total_shipping_tax_incl>
<total_shipping_tax_excl>10.00</total_shipping_tax_excl>
<carrier_tax_rate>0.000</carrier_tax_rate>
<total_wrapping>0.00</total_wrapping>
<total_wrapping_tax_incl>0.00</total_wrapping_tax_incl>
<total_wrapping_tax_excl>0.00</total_wrapping_tax_excl>
<shipping_number></shipping_number>
<conversion_rate>1.000000</conversion_rate>
<reference>TISNWMBFZ</reference>

<associations><order_rows><order_row><product_id>9</product_id><product_quantity>1</product_quantity><product_name>Votre impression Multi-photo</product_name><product_price>35.9</product_price></order_row></order_rows></associations></order>
</prestashop>

Et quand j'envoie le xml via

 

$opt_add_order[ 'resource' ] = 'orders';
$opt_add_order['postXml'] = $xml_ajout; (le xml ci-dessus)
$webservice->add();  

 

Boum !

 

Verdict : Fatal Error.

 

Rien de plus. Rien dans les logs du serveur.

 

Voici ma fonction add :

 

public function add($options)
{

			$xml = '';

	if (isset($options['resource'], $options['postXml']) || isset($options['url'], $options['postXml']))
	{
		$url = (isset($options['resource']) ? 'http://prestashop.pixtory-redmine.reseaux.info/api/'.$options['resource'] : $options['url']);
		$xml = $options['postXml'];
	}
	else
		throw new \Exception('Mauvais paramètres envoyés.');
	$request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => 'xml='.$xml));

	self::checkStatusCode($request['status_code']);
	//return self::parseXML($request['response']);
			return $request['response'];
}

 

Puis la fonction executeRequest :

 

protected function executeRequest($url, $curl_params = array())
{

			$fp = fopen('curl_log.txt', 'w');

			$defaultParams = array(
		CURLOPT_HEADER => TRUE,
		CURLOPT_RETURNTRANSFER => TRUE,
		CURLINFO_HEADER_OUT => TRUE,
		CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
					CURLOPT_ENCODING=> 'UTF-8',
		CURLOPT_USERPWD => $this->key.':',
		CURLOPT_HTTPHEADER => array( 'Expect:' ),
					CURLOPT_VERBOSE => TRUE,
					CURLOPT_STDERR => $fp
	);

	$session = curl_init($url);

			$logfh = fopen("my_log.log", 'a+');
			if ($logfh !== false) {
			  print "Opened the log file without errors";
			}
			curl_setopt($session, CURLOPT_HEADER, false);
			curl_setopt($session, CURLOPT_FILE, $logfh);
			curl_setopt($session, CURLOPT_VERBOSE, true);

	$curl_options = array();
	foreach ($defaultParams as $defkey => $defval)
	{
		if (isset($curl_params[$defkey]))
			$curl_options[$defkey] = $curl_params[$defkey];
		else
			$curl_options[$defkey] = $defaultParams[$defkey];
	}
	foreach ($curl_params as $defkey => $defval)
		if (!isset($curl_options[$defkey]))
			$curl_options[$defkey] = $curl_params[$defkey];

	curl_setopt_array($session, $curl_options);

			$response = curl_exec($session);

	$index = strpos($response, "\r\n\r\n");
	if ($index === false && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD')
		throw new PrestaShopWebserviceException('Mauvaise requête.');

	$header = substr($response, 0, $index);
	$body = substr($response, $index + 4);

			$headerArrayTmp = explode("\n", $header);

	$headerArray = array();
	foreach ($headerArrayTmp as &$headerItem)
	{
		$tmp = explode(':', $headerItem);
		$tmp = array_map('trim', $tmp);
		if (count($tmp) == 2)
			$headerArray[$tmp[0]] = $tmp[1];
	}

	if (array_key_exists('PSWS-Version', $headerArray))
	{
		if (
			version_compare(PrestaShopWebservice::psCompatibleVersionsMin, $headerArray['PSWS-Version']) == 1 ||
			version_compare(PrestaShopWebservice::psCompatibleVersionsMax, $headerArray['PSWS-Version']) == -1
		)
		throw new \Exception('Librairie incompatible avec cette version de Prestashop.');
	}

	if ($this->debug)
	{
		$this->printDebug('HTTP REQUEST HEADER', curl_getinfo($session, CURLINFO_HEADER_OUT));
		$this->printDebug('HTTP RESPONSE HEADER', $header);

	}
	$status_code = curl_getinfo($session, CURLINFO_HTTP_CODE);
	if ($status_code === 0)
		throw new PrestaShopWebserviceException('CURL Error: '.curl_error($session));
	curl_close($session);
	if ($this->debug)
	{
		if ($curl_params[CURLOPT_CUSTOMREQUEST] == 'PUT' || $curl_params[CURLOPT_CUSTOMREQUEST] == 'POST')
			$this->printDebug('XML SENT', $curl_params[CURLOPT_POSTFIELDS]);
		if ($curl_params[CURLOPT_CUSTOMREQUEST] != 'DELETE' && $curl_params[CURLOPT_CUSTOMREQUEST] != 'HEAD')
			$this->printDebug('RETURN HTTP BODY', $body);
	}
	return array('status_code' => $status_code, 'response' => $body, 'header' => $header);
}

Edited by amelie13 (see edit history)
Link to comment
Share on other sites

Bonsoir,

 

L'objet PrestaShopWebservice a-t-il été instancié en mode debug ? Ce qui permettrait éventuellement de savoir ce qui est envoyé et reçu en réponse.

 

Oui c'est justement dans le débug qu'apparait cette erreur :

 

HTTP REQUEST HEADER

 

POST /api/orders HTTP/1.1

Authorization: Basic R05XTDM2NzUwSkhKNTBDVkdCWlg5TlhPVENNTldWMzA6

Host: prestashop.pixtory-redmine.reseaux.info

Accept: */*

Accept-Encoding: UTF-8

Content-Length: 1952

Content-Type: application/x-www-form-urlencoded

 

 

HTTP RESPONSE HEADER

 

HTTP/1.1 200 OK

Date: Thu, 21 Mar 2013 17:18:44 GMT

Server: Apache/2.2.16 (Debian)

X-Powered-By: PHP/5.3.3-7+squeeze14

Set-Cookie: 3f99a2f33f7744ef0eca694ec6b1a644=s9d7eoHKLJNpxgxpF77%2BJjhBFXH1XUuRLobW0dtbZaoIbcKpg0RsOylYsEG64biEKcLXXXPKEL1AOCLClJkWuA%3D%3D000059; expires=Wed, 10-Apr-2013 17:18:44 GMT; path=/; domain=prestashop.pixtory-redmine.reseaux.info; httponly

Vary: Authorization,Host

Transfer-Encoding: chunked

Content-Type: text/html; charset=utf-8

 

XML SENT

 

xml=<?xml version="1.0" encoding="UTF-8"?>

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

<order>

<id></id>

<id_address_delivery>23</id_address_delivery>

<id_address_invoice>23</id_address_invoice>

<id_cart>3</id_cart>

<id_currency>2</id_currency>

<id_lang>5</id_lang>

<id_customer>24</id_customer>

<id_carrier>8</id_carrier>

<current_state>1</current_state>

<module>cheque</module>

<invoice_number>1</invoice_number>

<invoice_date>2013-03-21 16:56:30</invoice_date>

<delivery_number></delivery_number>

<delivery_date></delivery_date>

<valid>0</valid>

<date_add></date_add>

<date_upd></date_upd>

<id_shop_group>1</id_shop_group>

<id_shop>1</id_shop>

<secure_key></secure_key>

<payment>Chèque</payment>

<recyclable>0</recyclable>

<gift>0</gift>

<gift_message></gift_message>

<total_discounts>0.00</total_discounts>

<total_discounts_tax_incl>0.00</total_discounts_tax_incl>

<total_discounts_tax_excl>0.00</total_discounts_tax_excl>

<total_paid>45.90</total_paid>

<total_paid_tax_incl>45.90</total_paid_tax_incl>

<total_paid_tax_excl>36.9</total_paid_tax_excl>

<total_paid_real>45.90</total_paid_real>

<total_products>45.90</total_products>

<total_products_wt>45.90</total_products_wt>

<total_shipping>10.00</total_shipping>

<total_shipping_tax_incl>10.00</total_shipping_tax_incl>

<total_shipping_tax_excl>10.00</total_shipping_tax_excl>

<carrier_tax_rate>0.000</carrier_tax_rate>

<total_wrapping>0.00</total_wrapping>

<total_wrapping_tax_incl>0.00</total_wrapping_tax_incl>

<total_wrapping_tax_excl>0.00</total_wrapping_tax_excl>

<shipping_number></shipping_number>

<conversion_rate>1.000000</conversion_rate>

<reference>UJEXJGLVB</reference>

 

<associations><order_rows><order_row><product_id>9</product_id><product_quantity>1</product_quantity><product_name>Votre impression Multi-photo</product_name><product_price>35.9</product_price></order_row></order_rows></associations></order>

</prestashop>

 

RETURN HTTP BODY

 

Fatal error

 

Par ailleurs, est-ce que l'appel à add() est entouré d'un bloc try...catch(), éventuellement ?

 

Non. Qu'est ce que ça apporterait de plus dans mon cas ?

Link to comment
Share on other sites

Cela pourrait éventuellement apporté une levée d'exception avant l'envoi à l'api et donc avant le retour du simple Fatal Error.

 

Sinon, le $xml_ajout est-il une variable de type string ou SimpleXML ? Car sinon il faut utiliser

$opt_add_order['postXml'] = $xml_ajout->asXml(); (le xml ci-dessus)

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...