API cURL 调用错误:{"errors":{"price_rule":"Required parameter missing or invalid"}}

Error In API cURL Call: {"errors":{"price_rule":"Required parameter missing or invalid"}}

我在 cURL 调用基于所选产品的价格规则时遇到错误。 以下是我的错误:

{
   "errors":{
      "price_rule":"Required parameter missing or invalid"
   }
}

以下是我在 cURL 调用中发送的正文参数:

{
   "price_rule":{
      "title":"sss",
      "target_type":"line_item",
      "target_selection":"entitled",
      "allocation_method":"across",
      "value_type":"fixed_amount",
      "value":"-434",
      "customer_selection":"all",
      "prerequisite_quantity_range":{
         "greater_than_or_equal_to":"2"
      },
      "entitled_product_ids":{
         "0":"2397130326093",
         "1":"2397129965645",
         "2":"2397131898957",
         "3":"2397132324941"
      },
      "starts_at":"2019-02-22T08:08:53.000Z"
   }
}

以下是我的 cURL 调用:

$ch = curl_init();    
// curl_setopt($ch, CURLOPT_POST, count($newrule));
curl_setopt($ch, CURLOPT_URL, $access_token_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($pro_ids11));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Shopify-Access-Token: '.$shops_token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (curl_error($ch)) {
    $error_msg = curl_error($ch);
}
curl_close($ch);
if (isset($error_msg)) {
    echo $error_msg;
}

请注意,我在 PHP 工作。 提前致谢!

entitled_product_ids 应采用以下格式:

      "entitled_product_ids":{
         2397130326093,
         2397129965645,
         2397131898957,
         2397132324941
      },

此外,starts_at值应为ISO 8601格式(注意末尾少了一个0):

"starts_at":"2019-02-22T08:08:53.00Z"

你的格式有误:entitled_product_ids 应该是数组而不是对象

"entitled_collection_ids": [
    2397130326093,
    2397129965645,
    2397131898957,
    2397132324941
],

参考文档:https://help.shopify.com/en/api/reference/discounts/pricerule#create

你能把你的代码改成这样吗

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, TRUE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl, CURLOPT_USERAGENT, 'My New Shopify App v.1');
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');

    // Setup headers
    $request_headers[] = "X-Shopify-Access-Token: " . $token;
    $request_headers[]='Content-Type: application/json'

    curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);  
    $query = json_encode($query);
    curl_setopt ($curl, CURLOPT_POSTFIELDS, $query);
    $result = curl_exec($ch);

首先:您需要使用 'Content-Type: application/json' 发送数据并确保

其次:您必须将 entitled_product_ids 作为数组而不是对象发送

curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Shopify-Access-Token: '.$shops_token, 
'Content-Type: application/json'));

"entitled_product_ids": [
  2397130326093,
  2397129965645,
  2397131898957,
  2397131898957
]