我尝试通过 API 更新变体参数 'option1' - 获取 {"errors":{"variant":"Required parameter missing or invalid"}}

I try to update a variants parameter 'option1' via API - getting {"errors":{"variant":"Required parameter missing or invalid"}}

我尝试更新一个变体 option1。如果您查看下面的代码:函数 getVariant 将 return 此变体的 json,因此基本调用以及对 API 的身份验证都有效。函数 updateVariant 然而只有 returns: {"errors":{"variant":"Required parameter missing or invalid"}}

大多数 google 结果表明要解决此错误,我必须设置 Content-Type,我确实这样做了。但这并没有改变任何东西。我在这里想念什么?

我尝试重现此 api 参考中的调用:https://help.shopify.com/en/api/reference/products/product-variant#update-2019-07

$varianturl ="https://".$api_key.":".$password."@".$shop."/admin/api/2019-07/variants/15990192209979.json";

print_r(getVariant($varianturl));
print_r(updateVariant($varianturl));


function getVariant($url) {
    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

    $data = curl_exec($ch);
    curl_close($ch);
    return $data;

}

function updateVariant($url) {
    $ch = curl_init();
    $params = array(
    "id"=> 15990192209979,
    "option1"=> "Not Pink",
    "price"=> "99.00"
    );

    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;

}

您不能指望 GET 数据结构只能用作 PUT 数据结构。相反,使用 GET 中的数据映射到 PUT,遵循变体上的 PUT 指南,文档清楚地向您展示了这一点。简单地说 option1 等于 'test' 永远不会起作用,Shopify 应该用它做什么?更加详细一些。例如提供一个 ID 作为开始。

据我所知,您正在使用私人应用概念从 Shopify 获取和更新数据。

请用此代码替换您的代码

    $varianturl ="https://".$api_key.":".$password."@".$shop."/admin/api/2019-07/variants/15990192209979.json"; //15990192209979 this is a variant id
    print_r(getVariant($varianturl));
    print_r(updateVariant($varianturl));die;


    function getVariant($url) {
        $headers = [];
        $headers[] = "Authorization: Basic ".base64_encode($api_key.":".$password)."";
        $headers[] = 'X-Shopify-Access-Token:'.$password;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        $data = curl_exec($ch);
        curl_close($ch);
         list($message_headers, $message_body) = preg_split("/\r\n\r\n|\n\n|\r\r/", $data, 2);
        return $message_body;

    }

    function updateVariant($url) {
        $params = [];
        $params['variant'] = array(
        "id"=> 15990192209979, //this is product id not variant id
        "option1"=> "Not Pink",
        "price"=> "99.00"
        );
        $headers = [];
        $headers = array("Content-Type: application/json; charset=utf-8", 'Expect:');
        $headers[] = "Authorization: Basic ".base64_encode($api_key.":".$password)."";
        $headers[] = 'X-Shopify-Access-Token:'.$password;

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//deepak
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_USERAGENT, 'ohShopify-php-api-client');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
        curl_setopt($ch, CURLOPT_TIMEOUT, 100);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
        $data = curl_exec($ch);
        curl_close($ch);
          list($message_headers, $message_body) = preg_split("/\r\n\r\n|\n\n|\r\r/", $data, 2);
        return $message_body;

    }

有关私人应用程序工作的更多信息,请阅读此link

https://help.shopify.com/en/api/getting-started/authentication/private-authentication#make-authenticated-requests?