Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. <?
  2. //Authentication rest API magento2.Please change url accordingly your url
  3.  
  4. //I HAVE CHANGED EXAMPLE.COM,USERNAME AND PASSWORD FOR SECURITY PURPOSES
  5.  
  6. $adminUrl='https://example.com/index.php/rest/V1/integration/admin/token';
  7. $ch = curl_init();
  8. $data = array("username" => "THEUSERNAME", "password" => "THEPASSWORD");
  9. $data_string = json_encode($data);
  10. $ch = curl_init($adminUrl);
  11. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  12. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  14. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  15. 'Content-Type=> application/json',
  16. 'Content-Length=> ' . strlen($data_string))
  17. );
  18. $token = curl_exec($ch);
  19. $token= json_decode($token);
  20.  
  21. //Use above token into header
  22. $headers = array('Authorization=> Bearer $token');
  23.  
  24. $requestUrl='https://example.com/index.php/rest/V1/products/{MY_SKU}/stockItems/827';
  25. //Please note 24-MB01 is sku
  26.  
  27. $ch = curl_init();
  28. $ch = curl_init($requestUrl);
  29. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  30. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  31. curl_setopt($ch, CURLOPT_POST, true);
  32. $post ='{
  33. "stockItem": {
  34. "item_id": 12,
  35. "product_id": 0,
  36. "stock_id": 0,
  37. "qty": 4,
  38. "is_in_stock": true,
  39. "is_qty_decimal": true,
  40. "show_default_notification_message": true,
  41. "use_config_min_qty": true,
  42. "min_qty": 0,
  43. "use_config_min_sale_qty": 0,
  44. "min_sale_qty": 0,
  45. "use_config_max_sale_qty": true,
  46. "max_sale_qty": 0,
  47. "use_config_backorders": true,
  48. "backorders": 0,
  49. "use_config_notify_stock_qty": true,
  50. "notify_stock_qty": 0,
  51. "use_config_qty_increments": true,
  52. "qty_increments": 0,
  53. "use_config_enable_qty_inc": true,
  54. "enable_qty_increments": true,
  55. "use_config_manage_stock": true,
  56. "manage_stock": true,
  57. "low_stock_date": "string",
  58. "is_decimal_divided": true,
  59. "stock_status_changed_auto": 0,
  60. "extension_attributes": {}
  61. }
  62. }';
  63.  
  64. $options = array(
  65. CURLOPT_URL=>$toURL,
  66. CURLOPT_HTTPHEADER=>array(
  67. 'Content-Type: application/json',
  68. 'Content-Length: ' . strlen($post)),
  69. CURLOPT_VERBOSE=>0,
  70. CURLOPT_RETURNTRANSFER=>true,
  71. CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible;)",
  72. CURLOPT_POST=>true,
  73. CURLOPT_POSTFIELDS=>$post,
  74.  
  75. );
  76. curl_setopt_array($ch, $options);
  77. $result = curl_exec($ch);
  78. $result= json_decode($result);
  79. print_r($result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement