Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "productLink" :
- {
- "sku" : "TEST_SKU5",
- "position" : 0,
- "categoryId" : "8",
- "extensionAttributes": {}
- }
- }
- {
- "message": "Could not save product "%1" with position %2 to category %3",
- "parameters": [
- "144",
- 0,
- "8"
- ]
- }
- 'custom_attributes' => array(
- array( 'attribute_code' => 'category_ids', 'value' => ["8"] ),
- array( 'attribute_code' => 'description', 'value' => 'Simple Description' ),
- array( 'attribute_code' => 'short_description', 'value' => 'Simple Short Description' ),
- )
- "{"message":"URL key for specified store already exists."}"
- <?php
- $url="http://127.0.0.1/magento2.1/"; //your custom site url...
- $token_url=$url."rest/V1/integration/admin/token";
- //Below 9 is category id....
- $product_url=$url. "rest/V1/categories/9/products";
- $username="admin";
- $password="admin123";
- //Authentication rest API magento2, get access token
- $ch = curl_init();
- $data = array("username" => $username, "password" => $password);
- $data_string = json_encode($data);
- $ch = curl_init($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_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($data_string))
- );
- $token = curl_exec($ch);
- $adminToken= json_decode($token);
- //9 is category id...
- $sampleProductData = array(
- "sku" => "24-MB01",
- "position" => 5,
- "category_id" => "9",
- "extension_attributes" => array()
- );
- $categoryData = json_encode(array('productLink' => $sampleProductData));
- $setHaders = array('Content-Type:application/json','Authorization:Bearer '.$adminToken);
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_URL, $product_url);
- curl_setopt($ch,CURLOPT_POSTFIELDS, $categoryData);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($ch);
- curl_close($ch);
Add Comment
Please, Sign In to add comment