Skip to content

Instantly share code, notes, and snippets.

@Jeeva-Rathinam
Created October 22, 2020 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jeeva-Rathinam/8bfba0f484f746dfbca9dc782e0216e6 to your computer and use it in GitHub Desktop.
Save Jeeva-Rathinam/8bfba0f484f746dfbca9dc782e0216e6 to your computer and use it in GitHub Desktop.
magento 2.3 - Import Product Using Rest API
<?php
$url = "https://test.com/rest";
$token_url= $url."/V1/integration/admin/token";
$username= "test";
$password= "@test";
//Authentication REST API magento 2,
$ch = curl_init();
$data = array("username" => $username, "password" => $password);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
$token = curl_exec($ch);
$adminToken= json_decode($token);
$headers = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
// Createt Product REST API URL
$apiUrl = $url."/V1/products";
$ch = curl_init();
$data = [
"product" => [
"sku" => "Test Product 1",
"name" => "Test Product 1",
"attribute_set_id" => 4,
"price" => 99,
"status" => 1,
"visibility" => 2,
"type_id" => "simple",
"weight" => "1",
"extension_attributes" => [
"category_links" => [
[
"position" => 0,
"category_id" => "5"
],
[
"position" => 1,
"category_id" => "7"
]
],
"stock_item" => [
"qty" => "1000",
"is_in_stock" => true
]
],
"custom_attributes" => [
[
"attribute_code" => "description",
"value" => "Description of product here"
],
[
"attribute_code" => "short_description",
"value" => "short description of product"
]
]
]
];
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$response = json_decode($response, TRUE);
curl_close($ch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment