cancel
Showing results for 
Search instead for 
Did you mean: 

REST API Product create very slow

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

REST API Product create very slow

Feature request from MrSena, posted on GitHub Jul 21, 2016

Hi! I am develop integration with ERP. But have many problem with import large product catalog to Magento (2.1 from composer metapackage.) REST API Product create very slow - import one thousand products more 15 min. (post request to /rest/V1/products). Internal csv import work fast, but I can't start him remote. Also this way, there is no way to create the attributes and attribute_sets.

Solve the problem with the speed REST API, please

4 Comments
apiuser
New Member

Comment from choukalos, posted on GitHub Jul 25, 2016

Hi @MrSena,

What product creation api response time are you looking for? How many products are you trying to sync at one time (assume initial sync)? Have you used the scheduled import/export function in Enterprise edition? If that could be defined via XML for an extension would that address your need?

Thanks, Chuck

CC'ing @mbrinton01

apiuser
New Member

Comment from MrSena, posted on GitHub Jul 27, 2016

Hi @mbrinton01 I am use Community Edition. Now I have a directory with more than 20,000 products put /V1/products /{sku} for about 1 second for each item In my case, the price update for all products takes more than 5 hours. If I use CSV import from admin, the import time is a few seconds, but I can not initiate this process remotely. Initial sync I can run manually. But regular update need automatic mode - csv import unsuitable in the current implementation Thanks!

brendanb
Contributor

The API has always been slow, even from Magento 1x days. Has only improved slightly over the years. I initially used the API for imports/updates but gave up in the end. Too slow...

 

What I now do is have a custom table(s) on the magento database which contains all the relevant product data. I sync to these table(s) off and on adding and editing records as required.

 

Then I have a php script which makes calls the the magento product model updating the data as needed from the table(s). This is all run using a crontab schedule. As a record is updated or added I change the status on the customer table record to flag as done.

 

it works fairly well

 

brendan

bj1207
Senior Member
  • Hello I am php developer and I am working with magento 2 rest api. Problem is that when I create product using rest api in my localhost It take time at least 8 second per 1 product. (Please check code snippet)
  • How Can I reduce time for API response or speed up in rest api
  • One more question : Can I add bunch of products in one request??
<?php 
logWrite(": On Process Started");
if(file_exists("test.jpg"))
{
    $product_image = "http://" . $_SERVER['SERVER_NAME'].'/magento2api/test.jpg';
}
$imagedata = file_get_contents($product_image);
$base64 = base64_encode($imagedata);
$url="http://localhost/magento2/index.php/";
$token_url=$url."rest/V1/integration/admin/token";
$product_url=$url. "rest/V1/products";
$username="testuser";
$password="test123";
//Authentication rest API magento2, get access token
$ch = curl_init();
$data = array("username" => $username, "password" => $password);
$data_string = json_encode($data);

$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
    );
$token = curl_exec($ch);
$adminToken=  json_decode($token);
$sampleProductData = array(
        'sku'               => 'SKU10002',
        'name'              => 'Simple Product ' . uniqid(),
        'visibility'        => 4,
        'type_id'           => 'simple',
        'price'             => 20,
        'status'            => 1,
        'attribute_set_id'  => 4,
        'weight'            => 1,
        'extension_attributes' => array(
                "stock_item"=>array(
                        "qty"=>10,
                        "is_in_stock"=>true,
                ),
        ),
        'custom_attributes' => array(
            array( 'attribute_code' => 'category_ids', 'value' => ["2"] ),
            array( 'attribute_code' => 'description', 'value' => 'Simple Description' ),
            array( 'attribute_code' => 'short_description', 'value' => 'Simple  Short Description' ),
        ),
        'media_gallery_entries' => array(
            array(
                'id'=> 0,
                'media_type'=> 'image',
                'label'=> 'TESTIMAGE',
                'position'=> 0,
                'disabled'=> 0,
                'types' => array('image','small_image','thumbnail'),
                'file' => 'test.jpg',
                'content' => array(
                        'base64_encoded_data' => $base64,
                        "type"=> "image/jpeg",
                        'name'=> 'test.jpg'
                    )
                )
            )
    );

$productData = json_encode(array('product' => $sampleProductData));

$setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $product_url);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$decode = json_decode($response);
logWrite(": On Process Completed");
echo "<pre>"; print_r($decode);exit;

function logWrite($msg,$filename='Test.txt')
{
    $path = $_SERVER['DOCUMENT_ROOT'];
    $urlpath = $path.'magento2api/'.$filename;
    // echo $urlpath;exit;
    $fp = fopen($urlpath,'a');
    $time = @date('[d/m/Y H:i:s] ');
    fwrite($fp, $time."\t".$msg.PHP_EOL);
    fclose($fp);
}
?>

Thank you in advance