Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- //Authentication rest API magento2.Please change url accordingly your url
- //I HAVE CHANGED EXAMPLE.COM,USERNAME AND PASSWORD FOR SECURITY PURPOSES
- $adminUrl='https://example.com/index.php/rest/V1/integration/admin/token';
- $ch = curl_init();
- $data = array("username" => "THEUSERNAME", "password" => "THEPASSWORD");
- $data_string = json_encode($data);
- $ch = curl_init($adminUrl);
- 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);
- $token= json_decode($token);
- //Use above token into header
- $headers = array('Authorization=> Bearer $token');
- $requestUrl='https://example.com/index.php/rest/V1/products/{MY_SKU}/stockItems/827';
- //Please note 24-MB01 is sku
- $ch = curl_init();
- $ch = curl_init($requestUrl);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- $post ='{
- "stockItem": {
- "item_id": 12,
- "product_id": 0,
- "stock_id": 0,
- "qty": 4,
- "is_in_stock": true,
- "is_qty_decimal": true,
- "show_default_notification_message": true,
- "use_config_min_qty": true,
- "min_qty": 0,
- "use_config_min_sale_qty": 0,
- "min_sale_qty": 0,
- "use_config_max_sale_qty": true,
- "max_sale_qty": 0,
- "use_config_backorders": true,
- "backorders": 0,
- "use_config_notify_stock_qty": true,
- "notify_stock_qty": 0,
- "use_config_qty_increments": true,
- "qty_increments": 0,
- "use_config_enable_qty_inc": true,
- "enable_qty_increments": true,
- "use_config_manage_stock": true,
- "manage_stock": true,
- "low_stock_date": "string",
- "is_decimal_divided": true,
- "stock_status_changed_auto": 0,
- "extension_attributes": {}
- }
- }';
- $options = array(
- CURLOPT_URL=>$toURL,
- CURLOPT_HTTPHEADER=>array(
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($post)),
- CURLOPT_VERBOSE=>0,
- CURLOPT_RETURNTRANSFER=>true,
- CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible;)",
- CURLOPT_POST=>true,
- CURLOPT_POSTFIELDS=>$post,
- );
- curl_setopt_array($ch, $options);
- $result = curl_exec($ch);
- $result= json_decode($result);
- print_r($result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement