我尝试更新一个变体option1
。如果您查看以下代码:函数getVariant
将返回此变体的json,因此基本调用以及对该应用程序接口的身份验证都有效。然而,函数updateVariant
仅返回:{"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;
}
发布于 2019-09-06 22:24:47
正如我所看到的,您正在使用私有应用程序概念从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;
}
有关私人应用程序工作的更多知识,请单击此链接
发布于 2019-09-05 23:46:39
您不能期望GET数据结构只作为PUT数据结构工作。取而代之的是,使用GET到映射到PUT的数据,遵循PUT on a variant的指导原则,文档清楚地向您展示了这一点。简单地说option1等于'test‘是行不通的,那Shopify应该怎么做呢?更具体一点。例如,提供一个ID作为开始。
https://stackoverflow.com/questions/57808862
复制相似问题