Shopify admin REST api 无法创建缺少必需参数的产品
Shopfy admin REST api cannot create product missing required parameter
我正在尝试获取一个基本的 API 请求来创建适用于 shopify 的产品,但一直收到错误消息,提示我缺少必需的参数。 (唯一需要的参数是标题)
错误:
object(stdClass)#295 (1) { ["errors"]=> object(stdClass)#297 (1) { ["product"]=> string(37 )“必需的参数丢失或无效”} }
这是代码
$data = array();
$data['product'] = array(
'title' => 'Burton Custom Freestyle 151',
'product_type' => 'Category',
'published_scope' => 'web',
'status' => 'active',
'body_html' => '<strong>Good snowboard!</strong>',
);
$this->do_post('/admin/api/2020-10/products.json',$data);
private function do_post($end_point,$data)
{
$ch = curl_init($this->base_url_api.$end_point);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array(
'X-Shopify-Access-Token: '.$this->api_code_token,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$return = json_decode(curl_exec($ch));
var_dump($return);
return $return;
}
最佳做法 (IMO) 始终包含 header 以指定您要发送的内容类型。对于卷曲,它将是 -H "Content-Type: application/json"
。有些 API 甚至需要 charset=utf-8(比如 Slack API)。
为了进一步调查我最初的预感,我发现了这个可能有用的页面:https://community.shopify.com/c/Shopify-APIs-SDKs/Required-parameter-missing-or-invalid-on-variant-update/td-p/317216
我正在尝试获取一个基本的 API 请求来创建适用于 shopify 的产品,但一直收到错误消息,提示我缺少必需的参数。 (唯一需要的参数是标题)
错误:
object(stdClass)#295 (1) { ["errors"]=> object(stdClass)#297 (1) { ["product"]=> string(37 )“必需的参数丢失或无效”} }
这是代码
$data = array();
$data['product'] = array(
'title' => 'Burton Custom Freestyle 151',
'product_type' => 'Category',
'published_scope' => 'web',
'status' => 'active',
'body_html' => '<strong>Good snowboard!</strong>',
);
$this->do_post('/admin/api/2020-10/products.json',$data);
private function do_post($end_point,$data)
{
$ch = curl_init($this->base_url_api.$end_point);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array(
'X-Shopify-Access-Token: '.$this->api_code_token,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$return = json_decode(curl_exec($ch));
var_dump($return);
return $return;
}
最佳做法 (IMO) 始终包含 header 以指定您要发送的内容类型。对于卷曲,它将是 -H "Content-Type: application/json"
。有些 API 甚至需要 charset=utf-8(比如 Slack API)。
为了进一步调查我最初的预感,我发现了这个可能有用的页面:https://community.shopify.com/c/Shopify-APIs-SDKs/Required-parameter-missing-or-invalid-on-variant-update/td-p/317216