THELIA Forum

Welcome to the THELIA support and discusssion forum

Announcement

Rejoignez la communauté sur le Discord Thelia : https://discord.gg/YgwpYEE3y3

Offline

#1 Product import via API

(12-10-2015 11:16:43)


Hello,

I am trying to use the API to import product data but am a bit unclear. The call below seems to be causing a 500 error.

Am I doing things correctly?

require_once  __DIR__ . '/vendor' . '/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$client = new GuzzleHttp\Client();

function getSignature($requestContent = '')
{
    $secureKey = pack('H*', file_get_contents("api-local.key"));
    return hash_hmac('sha1', $requestContent, $secureKey);
}

//build request body and send
$headers  = ['Authorization' => 'Token 7B3252C02EBDAEF0251B2BF2B'];

$json = '{
    "ref": "foo",
    "locale": "en_US",
    "title": "product create from api",
    "description": "product description from api",
    "default_category": 5,
    "visible": 1,
    "price": "10",
    "currency": 3,
    "tax_rule": 3,
    "weight": 10,
    "brand_id": 0,
    "lang": 2}
';

$post = (json_decode($json, true));
$post['sign'] =  getSignature();

$response = $client->request('POST', 'http://local.feedmark2/api/products', array('headers' => $headers, 'query' => $post));

Last edited by majic (12-10-2015 11:17:04)

Offline

#2 Re: Product import via API

(12-10-2015 13:49:12)


Hi,

is there an error message in the log/log-thelia.txt file ?


OpenStudio Toulouse

Offline

#3 Re: Product import via API

(12-10-2015 14:29:03)


Hi,

Thanks for the reply...

Nothing in the file. PHP  logs are showing

[12-Oct-2015 14:21:39 Europe/London] PHP Fatal error:  Uncaught exception 'GuzzleHttp\Exception\ServerException' with message 'Server error: 500' in /Users/christo/www/feedmark/feedmark-internal/feedmark-api/vendor/guzzlehttp/guzzle/src/Middleware.php:68
Stack trace:
#0 /Users/christo/www/feedmark/feedmark-internal/feedmark-api/vendor/guzzlehttp/promises/src/Promise.php(199): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
#1 /Users/christo/www/feedmark/feedmark-internal/feedmark-api/vendor/guzzlehttp/promises/src/Promise.php(152): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)
#2 /Users/christo/www/feedmark/feedmark-internal/feedmark-api/vendor/guzzlehttp/promises/src/TaskQueue.php(60): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
#3 /Users/christo/www/feedmark/feedmark-internal/feedmark-api/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(96): GuzzleHttp\Promise\TaskQueue->run()
#4 /Users/christo/www/feedmark/feedmark-internal/feedmark-api/vendor/guzzlehttp/guzzle/src/Hand in /Users/christo/www/feedmark/feedmark-internal/feedmark-api/vendor/guzzlehttp/guzzle/src/Middleware.php on line 68

I am able to get product info for example the code below works as expected

<?php

require_once  __DIR__ . '/vendor' . '/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;


echo '<h1>Feedmark API</h1>';

$client = new GuzzleHttp\Client();

function getSignature($requestContent = '')
{
    $secureKey = pack('H*', file_get_contents("api-local.key"));
    return hash_hmac('sha1', $requestContent, $secureKey);
}


//build request body and send
$headers  = ['Authorization' => 'Token 7B3252C02EBDAEF0251B2BF2B'];

$post = ['sign' => getSignature()];

$response = $client->request('GET', 'http://local.feedmark2/api/products', array('headers' => $headers, 'query' => $post));
print_r($response);

?>

Last edited by majic (12-10-2015 14:29:32)

Offline

#4 Re: Product import via API

(12-10-2015 14:53:00)


The PHP logs are on your side of the wire, they cant't give details about the 500 error.

Could you please have a look in  the Apache error log ?


OpenStudio Toulouse

Offline

#5 Re: Product import via API

(12-10-2015 17:03:29)


Hi,

Apache log below

[Mon Oct 12 16:54:37 2015] [notice] caught SIGTERM, shutting down
[Mon Oct 12 16:54:42 2015] [notice] Digest: generating secret for digest authentication ...
[Mon Oct 12 16:54:42 2015] [notice] Digest: done
[Mon Oct 12 16:54:42 2015] [notice] FastCGI: process manager initialized (pid 9353)
[Mon Oct 12 16:54:42 2015] [notice] Apache/2.2.29 (Unix) mod_wsgi/3.4 Python/2.7.8 PHP/5.6.10 mod_ssl/2.2.29 OpenSSL/0.9.8zg DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.8 Perl/v5.20.0 configured -- resuming normal operations

I am starting to think it may be my development setup (MAMP). I will try on a vagrant setup and also on our staging site tomorrow. Will update this post accordingly. Thanks for the help so far.

Offline

#6 Re: Product import via API

(13-10-2015 09:45:05)


Ok I have made the API request to a remote staging server. Still the 500 error when trying to add products via API. Querying products still works just fine.

Nothing in error logs but the apache log is below.

root@li143-224:/srv/www/staging-feedmark-dog/logs# tail access.log
127.0.0.1 - - [13/Oct/2015:09:26:13 +0100] "GET /api/products?sign=ebace71b3c69c36a6095d5061638a6d8f2f557c7 HTTP/1.1" 401 332 "-" "GuzzleHttp/6.1.0 curl/7.41.0 PHP/5.6.10"
127.0.0.1 - - [13/Oct/2015:09:26:51 +0100] "GET /api/products/?sign=ebace71b3c69c36a6095d5061638a6d8f2f557c7 HTTP/1.1" 401 332 "-" "GuzzleHttp/6.1.0 curl/7.41.0 PHP/5.6.10"
127.0.0.1 - - [13/Oct/2015:09:26:52 +0100] "GET /api/products/?sign=ebace71b3c69c36a6095d5061638a6d8f2f557c7 HTTP/1.1" 401 332 "-" "GuzzleHttp/6.1.0 curl/7.41.0 PHP/5.6.10"
127.0.0.1 - - [13/Oct/2015:09:26:53 +0100] "GET /api/products/?sign=ebace71b3c69c36a6095d5061638a6d8f2f557c7 HTTP/1.1" 401 332 "-" "GuzzleHttp/6.1.0 curl/7.41.0 PHP/5.6.10"
127.0.0.1 - - [13/Oct/2015:09:26:53 +0100] "GET /api/products/?sign=ebace71b3c69c36a6095d5061638a6d8f2f557c7 HTTP/1.1" 401 332 "-" "GuzzleHttp/6.1.0 curl/7.41.0 PHP/5.6.10"
127.0.0.1 - - [13/Oct/2015:09:27:33 +0100] "GET /api/products/?sign=ebace71b3c69c36a6095d5061638a6d8f2f557c7 HTTP/1.1" 401 332 "-" "GuzzleHttp/6.1.0 curl/7.41.0 PHP/5.6.10"
127.0.0.1 - - [13/Oct/2015:09:27:56 +0100] "GET /api/products/?sign=ebace71b3c69c36a6095d5061638a6d8f2f557c7 HTTP/1.1" 200 2462 "-" "GuzzleHttp/6.1.0 curl/7.41.0 PHP/5.6.10"
127.0.0.1 - - [13/Oct/2015:09:28:41 +0100] "GET /?category_id=7&locale=es_ES&view=category&lang=cs_CZ HTTP/1.1" 200 5080 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
127.0.0.1 - - [13/Oct/2015:09:31:53 +0100] "GET /?category_id=7&locale=it_IT&view=category&lang=cs_CZ HTTP/1.1" 200 5079 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"
127.0.0.1 - - [13/Oct/2015:09:33:26 +0100] "POST /api/products/?ref=foo&locale=en_US&title=product%20create%20from%20api&description=product%20description%20from%20api&default_category=5&visible=1&price=10&currency=3&tax_rule=3&weight=10&brand_id=0&lang=2&sign=ebace71b3c69c36a6095d5061638a6d8f2f557c7 HTTP/1.1" 500 791 "-" "GuzzleHttp/6.1.0 curl/7.41.0 PHP/5.6.10"
root@li143-224:/srv/www/staging-feedmark-dog/logs#

Offline

#7 Re: Product import via API

(13-10-2015 10:08:49)


If an exception occurs when creating a product, a Json response containing the error text is returned. Could you please check this server response ?


OpenStudio Toulouse

Offline

#8 Re: Product import via API

(13-10-2015 13:20:07)


Hi,

I have changed the code, it looks like I need to pass http_errors to stop guzzle catching the 500 to early.

<?php

require_once  __DIR__ . '/vendor' . '/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;


$client = new GuzzleHttp\Client();

function getSignature($requestContent = '')
{
    $secureKey = pack('H*', file_get_contents("api-local.key"));

    return hash_hmac('sha1', $requestContent, $secureKey);
}



//build request body and send
$headers  = ['Authorization' => 'Token 7B3252C02EBDAEF0251B2BF2B'];

$json = '{
    "ref": "55",
    "label": "tedt",
    "locale": "en_US",
    "title": "product create from api",
    "description": "product description from api",
    "default_category": 5,
    "visible": 1,
    "price": 10.00,
    "currency": 3,
    "tax_rule": 3,
    "weight": 10,
    "brand_id": 0,
    "lang": 2,
    "virtual": 0,
    "weight": 5.00,
    "tax_price": 10.00
    }
';


$post = (json_decode($json, true));

$post['sign'] =  getSignature();



try {
    $response = $client->request('POST', 'http://local.feedmark2/api/products', array('headers' => $headers, 'query' => $post, 'http_errors' => false));
    $body = $response->getBody();
    echo $body;

} catch (GuzzleHttp\Exception\BadResponseException $e) {
   echo $e->getMessage();
    $data = $response->json();
    echo gettype($data);
}

?>

The response is failing on validation, I am not sure why as I'm adding required fields.

{"error":"Missing or invalid data: [Product reference *] , [Product title] , [Default product category *] , [locale] , [This product is online] , [This product does not have a physical presence] , [Product base price excluding taxes *] , [Product base price with taxes] , [Price currency *] , [Tax rule for this product *] , [Weight] , [Brand \/ Supplier] , [Summary] , [Detailed description] , [Conclusion] , [success_url] , [error_url] , [error_message]"}

Offline

#9 Re: Product import via API

(13-10-2015 13:31:17)


It seems that the form is not populated with the data you're sending. Maybe something wrong with the request ?


OpenStudio Toulouse

Offline

#10 Re: Product import via API

(13-10-2015 17:31:55)


Hmm they appear to be showing in the request

    [query] => Symfony\Component\HttpFoundation\ParameterBag Object
        (
            [parameters:protected] => Array
                (
                    [ref] => 55
                    [label] => tedt
                    [locale] => en_US
                    [title] => product create from api
                    [description] => product description from api
                    [default_category] => 5
                    [visible] => 1
                    [price] => 10
                    [currency] => 3
                    [tax_rule] => 3
                    [weight] => 5
                    [brand_id] => 0
                    [lang] => 2
                    [virtual] => 0
                    [tax_price] => 10
                    [sign] => 714365c484c165676c6115118a285dfa5af08863
                )

        )

So I can't figure out why they are not being picked up do I need to use multipart/form-data as a content type?

Running out of ideas I really would love to use the API method as an import hmm

Offline

#11 Re: Product import via API

(04-11-2015 12:53:37)


I'm in the same situation. is it solved?

Offline

#12 Re: Product import via API

(04-11-2015 14:03:12)


do I need to use multipart/form-data as a content type?

Yes.


OpenStudio Toulouse

Offline

#13 Re: Product import via API

(09-11-2015 13:28:30)


Still an issue for me unfortunately sad

Steps taken since last post
Upgraded to thelia 2.2.1
multipart/form-data was already the content type
Renaming POST variables to match the returned JSON error:

{"error":"Missing or invalid data: [Product reference *] , [Product title] , [Default product category *] , [locale] , [This product is online] , [This product does not have a physical presence] , [Product base price excluding taxes *] , [Product base price with taxes] , [Price currency *] , [Tax rule for this product *] , [Weight] , [Brand \/ Supplier] , [Summary] , [Detailed description] , [Conclusion] , [success_url] , [error_url] , [error_message]"}

Offline

#14 Re: Product import via API

(10-11-2015 11:07:42)


I have managed to solve the issue. I think what got me was the documentation site says ... If you want to create a product, you have to send the following fields with the POST method.

Which is not quite right.  Only sign goes in post the rest of product data is a json array in the body.

Fortunately the unit tests have clearer examples (tests/phpunit/Thelia)

Offline

#15 Re: Product import via API

(10-11-2015 11:15:15)


Many thanks for investigating this problem.

I've opened a issue in the doc GitHub (https://github.com/thelia/thelia.github.io/issues/181).


OpenStudio Toulouse