2014-08-27 44 views
3

我試圖將產品添加到由PrestaShop 1.6.0.9支持的我的商店中。這裏是我的代碼:使用WebService將產品添加到PrestaShop 1.6.0.9中

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

但我收到此錯誤的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> 

我收到錯誤每次更新或插入。可能是什麼問題?我該如何解決這個問題?我的PrestaShop版本是1.6.0.9。

回答

2

我在PSWebServiceLibrary.php in public function add($options)找到了這個bug。問題是與要求:

$request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => 'xml='.urlencode($xml))); 

請注意,即CURLOPT_POSTFIELDS => 'xml='.urlencode($xml)。這將XML作爲字符串的前面加上「XML =」字符串文件和編碼整個XML URL的開頭,但PS預計XML,所以它應該是這樣的:

$request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => $xml)); 
2

嘗試這種解決方案。它爲我

<html><head><title>Add Product</title></head><body> 
<?php 

define('DEBUG', 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'); 
    if (isset($_GET['Create'])) 
     $xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/products?schema=blank')); 
    else 
     $xml = $webService->get($opt); 
    $resources = $xml->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'; 
} 

if (count($_POST) > 0) 
{ 
// Here we have XML before update, lets update XML 
    foreach ($resources as $nodeKey => $node) 
    { 
     $resources->$nodeKey = $_POST[$nodeKey]; 
    } 
    try 
    { 
     $opt = array('resource' => 'products'); 
     if ($_GET['Create'] == 'Creating') 
     { 
      $opt['postXml'] = $xml->asXML(); 
      $xml = $webService->add($opt); 
      echo "Successfully added."; 
     } 
    } 
    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(); 
    } 
} 

// Title 
echo '<h1>Product\'s '; 
if (isset($_GET['Create'])) echo 'Creation'; 
else echo 'List'; 
echo '</h1>'; 

// We set a link to go back to list if we are in creation 
if (isset($_GET['Create'])) 
    echo '<a href="?">Return to the list</a>'; 

if (!isset($_GET['Create'])) 
    echo '<input type="button" onClick="document.location.href=\'?Create\'" value="Create">'; 
else 
    echo '<form method="POST" action="?Create=Creating">'; 

echo '<table border="5">'; 
if (isset($resources)) 
{ 

echo '<tr>'; 
if (count($_GET) == 0) 
{ 
    echo '<th>Id</th></tr>'; 

    foreach ($resources as $resource) 
    { 
     echo '<tr><td>'.$resource->attributes().'</td></tr>'; 
    } 
} 
else 
{ 
    echo '</tr>'; 
    foreach ($resources as $key => $resource) 
    { 
     echo '<tr><th>'.$key.'</th><td>'; 
     if (isset($_GET['Create'])) 
      echo '<input type="text" name="'.$key.'" value=""/>'; 
     echo '</td></tr>'; 
    } 
} 

} 
echo '</table><br/>'; 

if (isset($_GET['Create'])) 
    echo '<input type="submit" value="Create"></form>'; 


?> 
</body></html> 
+0

很好解釋你的改變 – baao 2014-10-27 07:26:59

0

您可以使用一個簡單的方法只使用直接XML輸入和產品架構, 增加值直接與$瓦爾像價值$身份識別碼等 請注意,我使用2種語言在PS 。

$ps_product = <<<XML 
<prestashop> 
<product> 
    <id></id> 
    <id_manufacturer></id_manufacturer> 
    <id_supplier></id_supplier> 
    <id_category_default></id_category_default> 
    <new></new> 
    <cache_default_attribute></cache_default_attribute> 
    <id_default_image></id_default_image> 
    <id_default_combination></id_default_combination> 
    <id_tax_rules_group></id_tax_rules_group> 
    <position_in_category></position_in_category> 
    <type></type> 
    <id_shop_default></id_shop_default> 
    <reference></reference> 
    <supplier_reference></supplier_reference> 
    <location></location> 
    <width></width> 
    <height></height> 
    <depth></depth> 
    <weight></weight> 
    <quantity_discount></quantity_discount> 
    <ean13></ean13> 
    <upc></upc> 
    <cache_is_pack></cache_is_pack> 
    <cache_has_attachments></cache_has_attachments> 
    <is_virtual></is_virtual> 
    <on_sale></on_sale> 
    <online_only></online_only> 
    <ecotax></ecotax> 
    <minimal_quantity></minimal_quantity> 
    <price></price> 
    <wholesale_price></wholesale_price> 
    <unity></unity> 
    <unit_price_ratio></unit_price_ratio> 
    <additional_shipping_cost></additional_shipping_cost> 
    <customizable></customizable> 
    <text_fields></text_fields> 
    <uploadable_files></uploadable_files> 
    <active></active> 
    <redirect_type></redirect_type> 
    <id_product_redirected></id_product_redirected> 
    <available_for_order></available_for_order> 
    <available_date></available_date> 
    <condition></condition> 
    <show_price></show_price> 
    <indexed></indexed> 
    <visibility></visibility> 
    <advanced_stock_management></advanced_stock_management> 
    <date_add></date_add> 
    <date_upd></date_upd> 
    <meta_description><language id='1'></language><language id='2'></language></meta_description> 
    <meta_keywords><language id='1'></language><language id='2'></language></meta_keywords> 
    <meta_title><language id='1'></language><language id='2'></language></meta_title> 
    <link_rewrite><language id='1'></language><language id='2'></language></link_rewrite> 
    <name><language id='1'></language><language id='2'></language></name> 
    <description><language id='1'></language><language id='2'></language></description> 
    <description_short><language id='1'></language><language id='2'></language></description_short> 
    <available_now><language id='1'></language><language id='2'></language></available_now> 
    <available_later><language id='1'></language><language id='2'></language></available_later> 
<associations> 
<categories> 
    <category> 
    <id></id> 
    </category> 
</categories> 
<images> 
    <image> 
    <id></id> 
    </image> 
</images> 
<combinations> 
    <combination> 
    <id></id> 
    </combination> 
</combinations> 
<product_option_values> 
    <product_option_value> 
    <id></id> 
    </product_option_value> 
</product_option_values> 
<product_features> 
    <product_feature> 
    <id></id> 
    <id_feature_value></id_feature_value> 
    </product_feature> 
</product_features> 
<tags> 
    <tag> 
    <id></id> 
    </tag> 
</tags> 
<stock_availables> 
    <stock_available> 
    <id></id> 
    <id_product_attribute></id_product_attribute> 
    </stock_available> 
</stock_availables> 
<accessories> 
    <product> 
    <id></id> 
    </product> 
</accessories> 
<product_bundle> 
    <product> 
    <id></id> 
    <quantity></quantity> 
    </product> 
</product_bundle> 
</associations> 
</product> 
</prestashop> 
XML; 

$xml = new SimpleXMLElement($psXML); 
$opt = array('resource' => 'products'); 
$opt['postXml'] = $xml->asXML(); 
$xml = $webService->add($opt); 

我使用這種方式,以便直接在PS中添加購物車。如果你沒有值某些節點(可以通過只爲必填字段數據),只需使用下面的架構(例如id_default_image):

<id_default_image/> 

,而不是

<id_default_image></id_default_image> 

希望這將幫你。

相關問題