使用不带Mage的PHP以编程方式更新产品价格

时间:2018-09-24 20:00:15

标签: php api magento

我一直在寻找一种不用PHP即可使用Mage更新特定产品价格的方法。我一直在尝试使用curl进行此操作,但是,当我尝试使用curl时,会收到错误消息“ {{message”:“%fieldName是必填字段。”,“ parameters”:{“ fieldName”:“ product” }”。这是我的代码:

<?php
$productUpdateJson = '{
    "product": {
        "sku": "577617647",
        "price": 324.99,
        "extensionAttributes": {
            "stockItem": {
                "qty": 120,
                "isInStock": true
            }
        }
    },
    "saveOptions": true
}';
$productUpdateJson = json_encode($productUpdateJson);

        $productsJson = http_build_query(json_decode($productUpdateJson));
$ch = curl_init( "http://myserver.com/index.php/rest/V1/products" );

$curlOptions = array(
    CURLOPT_CUSTOMREQUEST  => "POST",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => $productsJson,
    CURLOPT_HTTPHEADER => array( "Content-type: application/json", "Authorization: bearer 1ydds7h1dgb3i1msk1xcvjaacjhr97ht" )
);

curl_setopt_array( $ch, $curlOptions );

$response = curl_exec( $ch );
var_dump($response);

有人收到此错误吗?如果是,您如何解决?

1 个答案:

答案 0 :(得分:0)

跳过json_encode json_decode http_build_query,然后将原始JSON字符串传递给cURL,如下所示:

CURLOPT_POSTFIELDS => $productUpdateJson