在通过webservice创建类别期间的prestashop错误302

时间:2014-02-10 22:34:41

标签: php web-services prestashop crud http-status-code-302

美好的一天。

我正在通过webservice在prestashop上添加一个类别。 我遇到了一些302错误,所以我试图从头开始。

这是我从doc网站上获取的文件,作为插入类别的示例:

<html><head><title>CRUD - Create Categories</title></head><body>
<?php
// Here we define constants /!\ You need to replace this parameters
define('DEBUG', true);
define('PS_SHOP_PATH', 'http://www.myserver.com');
define('PS_WS_AUTH_KEY', 'THEAUTHKEYISHERE');
require_once('./PSWebServiceLibrary.php');

error_reporting(E_ALL);
ini_set('display_errors', '1');

// Here we use the WebService to get the schema of "customers" resource
try
{
    $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
    $opt = array('resource' => 'categories');
    if (isset($_GET['Create']))
        $xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/categories?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' => 'categories');
        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();
    }
}

// We set the Title
echo '<h1>Categories\'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;
            if ((bool)$resource['required'] == true)
                echo ' (*)'; // * for required fields
            echo '</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>

错误是一样的:

HTTP/1.1 302 Found
Date: Mon, 10 Feb 2014 22:32:17 GMT
Server: Apache/2.2.16 (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze18
location: http://myserver.com/api/categories?filter%5Bname%5D=%5Bcategoria+test%5D?url=categories&filter%5Bname%5D=%5Bcategoria+test%5D
Vary: Accept-Encoding
Content-Length: 0
Content-Type: text/html; charset=utf-8

已启用url重写,并且空白架构网址上的类别空白架构是可用的。

这个奇怪的302错误(状态)的任何想法?

更新2/11/2014 我正在挖掘更多,我已经阅读了302状态,所以我设法从302状态获得“location:”参数,这是一个urlencoded网址。 如果我使用那个url“as is”prestashop,webservice将不会响应它,但如果idecode为url,并检查它,一切都很好,我正在搜索的类别就在那里,这里是例子:

http://myserver.com/api/categories?filter[name]=[iPods]

这似乎奏效了。 while:

http://myserver.com/api/categories?filter%5Bname%5D=%5B+iPods+%5D?url=categories&filter%5Bname%5D=%5B+iPods+%5D

不起作用,但这是网络服务回馈给我的网址......任何人都有一些想法?

2 个答案:

答案 0 :(得分:3)

解决。问题是我穿上了

define('PS_SHOP_PATH', 'http://www.myserver.com');

但是看看回应,他们来自

http://myserver.com/api/categories?filter%5Bname%5D=%5Bcategoria+test%5D?url=categories&filter%5Bname%5D=%5Bcategoria+test%5D

我只需要在define中删除“www”。 这就是

问题是htaccess试图进行重定向,一个302,但是读取xml的组件不支持它。

答案 1 :(得分:0)

302表示“找到”。这很好 - 不是错误。也许我不确定你在问什么。