Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用WebService将产品添加到PrestaShop 1.6.0.9_Xml_Prestashop_Cdata_Prestashop 1.5_Prestashop 1.6 - Fatal编程技术网

Xml 使用WebService将产品添加到PrestaShop 1.6.0.9

Xml 使用WebService将产品添加到PrestaShop 1.6.0.9,xml,prestashop,cdata,prestashop-1.5,prestashop-1.6,Xml,Prestashop,Cdata,Prestashop 1.5,Prestashop 1.6,我正试图通过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://mys

我正试图通过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

我在
公共函数添加($options)
中的pswebserviceclibrary.php中发现了这个bug。问题在于请求:

$request = self::executeRequest($url, array(CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => 'xml='.urlencode($xml)));
请注意,
CURLOPT_POSTFIELDS=>'xml='.urlencode($xml)
。这会将XML作为字符串发送,并在文件开头加上前缀“XML=”字符串,url编码整个XML,但PS需要XML,因此应该是这样:

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

试试这个解决方案。它对我有用

<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>
添加产品

您可以使用更简单的方法,只需使用直接XML输入和产品模式, 直接使用$vars值添加值,如$myID等。 请注意,我在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\u product=add($opt);
我使用这种方式是为了直接在PS中添加Cart。如果您没有某些节点的值(您只能为必填字段传递数据),只需使用以下模式(例如id\u default\u image):


而不是

<id_default_image></id_default_image>


希望这能对您有所帮助。

若能解释您的更改,将不胜感激
<id_default_image></id_default_image>