我尝试更新变体option1
。如果您看下面的代码:函数getVariant
将返回此变体的json,因此基本调用以及对API的身份验证均有效。函数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;
}
答案 0 :(得分:1)
如我所见,您正在使用私有应用程序概念从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;
}
有关私有应用工作的更多信息,请通过此链接
答案 1 :(得分:-1)
您不能期望GET数据结构仅能用作PUT数据结构。取而代之的是,按照变体上PUT的准则,使用GET中的数据映射到PUT,文档会清楚地向您显示这些内容。简单地说option1等于'test'永远行不通,Shopify应该怎么做?更加详细一些。例如提供一个ID作为开始。