I am trying to add products to my shop powered by PrestaShop 1.6.0.9. Here is my code:
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
define('DEBUG', true);
define('_PS_DEBUG_SQL', true);
define('PS_SHOP_PATH', 'http://myshop.com');
define('PS_WS_AUTH_KEY', 'E6R9IDPK2R519WB9QAJ45MUACZ9GANC2');
require_once('PSWebServiceLibrary.php');
try {
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$opt = array('resource' => 'products');
$xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/products?schema=synopsis'));
$resources = $xml->children()->children();
unset($resources->position_in_category);
unset($resources->manufacturer_name);
$resources->price = '1000';
$resources->active = '1';
$resources->quantity = '50';
$resources->link_rewrite = 'blabla';
$resources->name->language[0][0] = 'blabla';
$resources->description->language[0][0] = '<p>blabla</p>';
$resources->description_short->language[0][0] = 'blabla';
$resources->associations = '';
$opt = array('resource' => 'products');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
}
catch (PrestaShopWebserviceException $ex) {
echo 'Other error: <br/>'.$ex->getMessage();
}
?>
But I receive this error as XML:
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[127]]></code>
<message><![CDATA[XML error : String could not be parsed as XML
XML length : 7891
Original XML : %3C%3Fxml+version%3D%221.0%22+encoding%3D%22UTF-8%22%3F%3E%0A%3Cprestashop+xmlns%3Axlink%3D...................
</error>
</errors>
</prestashop>
I receive error for every update or insert. What can be the problem? And how can I fix this? My PrestaShop version is 1.6.0.9.
I found the bug in PSWebServiceLibrary.php in public function add($options)
. The problem was with request:
$request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => 'xml='.urlencode($xml)));
Notice, that CURLOPT_POSTFIELDS => 'xml='.urlencode($xml)
. This sends XML as string with prepending 'xml=' string to the beginning of file and url encoding the whole XML, but PS expects XML, so it should be this way:
$request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $xml));