<?
//Authentication rest API magento2.Please change url accordingly your url
$adminUrl='http://localhost/magento/index.php/rest/V1/integration/admin/token';
$ch = curl_init();
$data = array("username" => "", "password" => "");
$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='http://127.0.0.1/magento/index.php/rest/V1/products';
//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 ='{
"product": {
"sku": "MY_SKU",
"name": "My Product",
"attributeSetId": "4",
"price": 20,
"status": 1,
"visibility": 4,
"typeId": "virtual",
"weight": 0,
"extensionAttributes": {
"stockItem": {
"stockId": 1,
"qty": 20,
"isInStock": true,
"isQtyDecimal": false,
"useConfigMinQty": true,
"minQty": 0,
"useConfigMinSaleQty": 0,
"minSaleQty": 0,
"useConfigMaxSaleQty": true,
"maxSaleQty": 0,
"useConfigBackorders": false,
"backorders": 0,
"useConfigNotifyStockQty": true,
"notifyStockQty": 20,
"useConfigQtyIncrements": false,
"qtyIncrements": 0,
"useConfigEnableQtyInc": false,
"enableQtyIncrements": false,
"useConfigManageStock": true,
"manageStock": true,
"lowStockDate": "string",
"isDecimalDivided": true,
"stockStatusChangedAuto": 0,
"extensionAttributes": {}
}
},
"options": [],
"tierPrices": [],
"customAttributes": [
]
},
"saveOptions": true
}';
$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);
?>
上面的代码无法添加产品,我已经尝试了很多方法,仍然无法通过rest api添加产品,有谁知道如何通过rest api添加产品?请帮助,谢谢。
上面的代码无法添加产品,我已经尝试了很多方法,仍然无法通过rest api添加产品,有谁知道如何通过rest api添加产品?请帮助,谢谢。
答案 0 :(得分:1)
<?
$url = domainname;
$token_url=$url."rest/V1/integration/admin/token";
$product_url=$url. "rest/V1/products";
$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);
$sampleProductData = array(
'sku' => $isbn13,
'name' => $name,
'visibility' => 4,
'type_id' => 'simple',
'price' => $price,
'status' => 1,
'attribute_set_id' => 4,
'weight' => $product_weight,
'extension_attributes' => array(
"stock_item"=>array(
'qty' => $inventory_stock,'is_in_stock' => 1,'manage_stock' => 1,'use_config_manage_stock' => 1,'min_qty' => 0,'use_config_min_qty' => 1,'min_sale_qty' => 1,'use_config_min_sale_qty' => 1,'max_sale_qty ' => 10,'use_config_max_sale_qty' => 1,'is_qty_decimal' => 0,'backorders' => 0,'use_config_backorders' => 1,'notify_stock_qty' => 1,'use_config_notify_stock_qty' => 1
),
),
'custom_attributes' => array(
array( 'attribute_code' => 'category_ids', 'value' => ["43"] ),
array( 'attribute_code' => 'description', 'value' => $description ),
array( 'attribute_code' => 'short_description', 'value' => $short_description ),
array( 'attribute_code' => 'meta_title', 'value' => $meta_title),
array( 'attribute_code' => 'meta_keyword', 'value' => $meta_keyword),
array( 'attribute_code' => 'meta_description', 'value' => $meta_description),
),
);
$productData = json_encode(array('product' => $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, $productData);
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);
这是用于在magento 2上进行产品上传停止api描述=仅复制代码并将代码放在一个php文件中并将chagne域名放到(https://--------.com/)并在magento 2中创建用户和角色后({{ 3}})并将该用户名字段中的用户名也放在此代码和密码中,并运行此代码替换所有动态变量。(如$ isbn13,$ name ...等,包含您的产品详细信息)请参阅产品创建或更新magento 2
答案 1 :(得分:0)
$adminUrl = 'http://localhost/magento2.1.2/index.php/rest/V1/integration/admin/token/'; $ch = curl_init(); $data = array("username" => "", "password" => ""); $ch = curl_init($adminUrl); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type=> application/json')); $token = curl_exec($ch); $token = json_decode($token);
使用您的magento管理员用户名和密码并获取令牌ID。
将$ post json变量传递给此cURL调用。
$curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "http://localhost/magento2.1.2/index.php/rest/V1/products", CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $post, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_HTTPHEADER => array( "authorization: Bearer $token", "content-type: application/json" ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; }
答案 2 :(得分:-2)
请求网址$ requestUrl =&#39; http://127.0.0.1/magento/index.php/rest/V1/products&#39;改变&#34;产品&#34;产品和尝试。