Guest User

Untitled

a guest
Nov 18th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. {
  2. "productLink" :
  3. {
  4. "sku" : "TEST_SKU5",
  5. "position" : 0,
  6. "categoryId" : "8",
  7. "extensionAttributes": {}
  8.  
  9. }
  10. }
  11.  
  12. {
  13. "message": "Could not save product "%1" with position %2 to category %3",
  14. "parameters": [
  15. "144",
  16. 0,
  17. "8"
  18. ]
  19. }
  20.  
  21. 'custom_attributes' => array(
  22. array( 'attribute_code' => 'category_ids', 'value' => ["8"] ),
  23. array( 'attribute_code' => 'description', 'value' => 'Simple Description' ),
  24. array( 'attribute_code' => 'short_description', 'value' => 'Simple Short Description' ),
  25. )
  26.  
  27. "{"message":"URL key for specified store already exists."}"
  28.  
  29. <?php
  30. $url="http://127.0.0.1/magento2.1/"; //your custom site url...
  31. $token_url=$url."rest/V1/integration/admin/token";
  32.  
  33. //Below 9 is category id....
  34. $product_url=$url. "rest/V1/categories/9/products";
  35. $username="admin";
  36. $password="admin123";
  37. //Authentication rest API magento2, get access token
  38. $ch = curl_init();
  39. $data = array("username" => $username, "password" => $password);
  40. $data_string = json_encode($data);
  41.  
  42. $ch = curl_init($token_url);
  43. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  44. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  46. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  47. 'Content-Type: application/json',
  48. 'Content-Length: ' . strlen($data_string))
  49. );
  50. $token = curl_exec($ch);
  51. $adminToken= json_decode($token);
  52.  
  53. //9 is category id...
  54. $sampleProductData = array(
  55. "sku" => "24-MB01",
  56. "position" => 5,
  57. "category_id" => "9",
  58. "extension_attributes" => array()
  59. );
  60. $categoryData = json_encode(array('productLink' => $sampleProductData));
  61.  
  62. $setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
  63. $ch = curl_init();
  64. curl_setopt($ch,CURLOPT_URL, $product_url);
  65. curl_setopt($ch,CURLOPT_POSTFIELDS, $categoryData);
  66. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  67. curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
  68. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  69. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  70. $response = curl_exec($ch);
  71. curl_close($ch);
Add Comment
Please, Sign In to add comment