Go to Admin > System > Extensions > Integrations and create a new integration and use token id from here.
PHP code example to fetch / create / update magento 2 product will all attribute and image data
error_reporting(E_ALL);
ini_set('display_errors', 1);
$token = 'upp1cde4igj33bfx0aunlj0vn5vjf2a1';
$m2 = "http://localhost/M2/ce/magento217ce_1";
//get product data
/*$ch = curl_init($m2."/index.php/rest/all/V1/products/SIMPLE-101");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer ". $token));
$result = curl_exec($ch);
$result = json_decode($result, 1);
$error = curl_error($ch);
echo $error;
print_r($result);
*/
// create/update product
// media images
$path = "/var/www/html/M2/ce/magento217ce_1/pub/media/catalog/product/m/b/mb02-gray-0.jpg";
$data = file_get_contents($path);
$mediaName1 = "mb02-gray-0.jpg";
$type1 = mime_content_type($path);
$base64Media1 = base64_encode($data);
$path = "/var/www/html/M2/ce/magento217ce_1/pub/media/catalog/product/m/b/mb02-blue-0.jpg";
$data = file_get_contents($path);
$mediaName2 = "mb02-blue-0.jpg";
$type2 = mime_content_type($path);
$base64Media2 = base64_encode($data);
$data =
[
"sku" => "02-682-557",
"name" => "Fisherbrand Free-Standing Microcentrifuge Tubes with Screw Caps",
"attribute_set_id" => 4,
"price" => 34,
"status" => 1,
"visibility" => 4,
"type_id" => "simple",
"weight" => 1,
"extension_attributes" =>
[
"stock_item" =>
[
"qty" => 100,
"is_in_stock" => 1,
"is_qty_decimal" => "",
"show_default_notification_message" => "",
"use_config_min_qty" => 1,
"min_qty" => 0,
"use_config_min_sale_qty" => 1,
"min_sale_qty" => 1,
"use_config_max_sale_qty" => 1,
"max_sale_qty" => 10000,
"use_config_backorders" => 1,
"backorders" => 0,
"use_config_notify_stock_qty" => 1,
"notify_stock_qty" => 1,
"use_config_qty_increments" => 1,
"qty_increments" => 0,
"use_config_enable_qty_inc" => 1,
"enable_qty_increments" => 0,
"use_config_manage_stock" => 1,
"manage_stock" => 0,
"low_stock_date" => "",
"is_decimal_divided" => "",
"stock_status_changed_auto" => 0,
],
],
"product_links" =>
[
[
"sku" => "02-682-557",
"link_type" => "related",
"linked_product_sku" => "24-WG087",
"linkedProductType" => "simple",
"position" => 1,
"extension_attributes" => [],
],
[
"sku" => "02-682-557",
"link_type" => "related",
"linked_product_sku" => "24-WG086",
"linked_product_type" => "simple",
"position" => 2,
"extension_attributes" => [],
],
[
"sku" => "02-682-557",
"link_type" => "crosssell",
"linked_product_sku" => "24-WG087",
"linked_product_type" => "simple",
"position" => 1,
"extension_attributes" => [],
],
[
"sku" => "02-682-557",
"link_type" => "crosssell",
"linked_product_sku" => "24-WG086",
"linked_product_type" => "simple",
"position" => 2,
"extension_attributes" => [],
],
[
"sku" => "02-682-557",
"link_type" => "upsell",
"linked_product_sku" => "24-WG087",
"linked_product_type" => "simple",
"position" => 1,
"extension_attributes" => [],
],
[
"sku" => "02-682-557",
"link_type" => "upsell",
"linked_product_sku" => "24-WG086",
"linked_product_type" => "simple",
"position" => 2,
"extension_attributes" => [],
],
],
"tier_prices" =>
[
[
"customer_group_id" => 32000,
"qty" => 10,
"value" => 28,
],
[
"customer_group_id" => 32000,
"qty" => 25,
"value" => 27,
],
[
"customer_group_id" => 32000,
"qty" => 50,
"value" => 25,
],
],
"options" =>
[
[
"product_sku" => "02-682-557",
//"option_id" => 1, // use while update
"title" => "Custom Yoga Option",
"type" => "drop_down",
"sort_order" => 1,
"is_require" => 0,
"max_characters" => 0,
"image_size_x" => 0,
"image_size_y" => 0,
"values" =>
[
[
"title" => "Gold",
"sort_order" => 1,
"price" => 10,
"price_type" => "fixed",
//"option_type_id" => 1, // use while update
],
[
"title" => "Silver",
"sort_order" => 2,
"price" => 10,
"price_type" => "fixed",
//"option_type_id" => 2, // use while update
],
[
"title" => "Platinum",
"sort_order" => 3,
"price" => 10,
"price_type" => "fixed",
//"option_type_id" => 3, // use while update
],
],
],
],
"media_gallery_entries" =>
[
[
"media_type" => "image",
"label" => "Image",
"position" => 1,
"disabled" => false,
"types" => ["image", "small_image", "thumbnail"],
"content" =>
[
"base64_encoded_data" => $base64Media1,
"type" => $type1 ,
"name"=> $mediaName1,
],
],
[
"media_type" => "image",
"label" => "Image",
"position" => 2,
"disabled" => false,
"types" => [],
"content" =>
[
"base64_encoded_data" => $base64Media2,
"type" => $type2 ,
"name"=> $mediaName2,
],
],
],
"custom_attributes" =>
[
[
"attribute_code" => "description",
"value" => "Capacity: 1.5mL",
],
[
"attribute_code" => "short_description",
"value" => "Fisherbrand Free-Standing Microcentrifuge Tubes with Screw Caps",
],
[
"attribute_code"=> "special_price",
"value"=> 29.0000,
],
[
"attribute_code"=> "special_from_date",
"value"=> "2017-11-01 00:00:00",
],
[
"attribute_code"=> "special_to_date",
"value"=> "2017-11-30 00:00:00",
],
[
"attribute_code"=> "cost",
"value"=> 19.0000,
],
[
"attribute_code"=> "meta_title",
"value"=> "Meta Title",
],
[
"attribute_code"=> "meta_keyword",
"value"=> "meta1, meta2, meta3",
],
[
"attribute_code"=> "meta_description",
"value"=> "meta description",
],
[
"attribute_code"=> "news_from_date",
"value"=> "2017-11-01 00:00:00",
],
[
"attribute_code"=> "news_to_date",
"value"=> "2017-11-30 00:00:00",
],
[
"attribute_code" => "category_ids",
"value" => [3, 4],
],
[
"attribute_code"=> "country_of_manufacture",
"value"=> "AL",
],
[
"attribute_code"=> "url_key",
"value"=> "fisherbrand-free-standing-microcentrifuge-tubes-with-screw-caps",
],
[
"attribute_code"=> "tax_class_id",
"value"=> 2,
],
],
];
// create / update products data
$ch = curl_init($m2."/index.php/rest/all/V1/products/");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["product" => $data]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer ". $token));
$result = curl_exec($ch);
$result = json_decode($result, 1);
$error = curl_error($ch);
echo $error;
print_r($result);
// assign created product into a website
$websiteLinkData = [
"sku" => "02-682-557",
"website_id" => 1,
];
$ch = curl_init($m2."/index.php/rest/all/V1/products/02-682-557/websites/");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["product_website_link" => $websiteLinkData]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer ". $token));
$result = curl_exec($ch);
$result = json_decode($result, 1);
$error = curl_error($ch);
echo $error;
print_r($result);