Question

Good day.

i'm adding a category on prestashop, via webservice. i've faced some a 302 error, and so I have tryed to start from scratch.

this is the file I have taken from the doc website as example of inserting a category:

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

the error is the same:

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 rewrite is on, and the category blank schema is avialable at the blank schema url.

any idea of this strange 302 error (status)?

UPDATE 2/11/2014 i've digging some more, I have read about the 302 status, so i manage to have the "location:" parameteter from the 302 status, wich is a urlencoded URL. if I use that url "as is" prestashop the webservice will not respond as it should, but if idecode the url, and check it, all is nice, the category i was searching is there, here the examples :

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

this seems to work. while :

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

does not work, but this is the url that the webservice give back to me... anyone has some idea?

Was it helpful?

Solution

solved. the problem was that i had put on the

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

but looking at the response, they come from

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

I only needed to delete that "www" in the define. that's all.

the problem was that htaccess try to make a redirection, a 302 one, but the component tthat read the xml doesn't support it.

OTHER TIPS

302 means "Found". That's good - not an error. Maybe I'm not sure what you are asking.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top