API cURL调用中的错误:{“错误”:{“ price_rule”:“必需的参数丢失或无效”}}

时间:2019-02-22 08:18:42

标签: php laravel shopify-app

基于所选产品的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。 提前致谢!

4 个答案:

答案 0 :(得分:7)

您可以这样更改代码吗

    $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);

答案 1 :(得分:2)

您的格式错误:entitled_product_ids应该是数组而不是对象

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

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

答案 2 :(得分:1)

首先:您需要使用'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
]

答案 3 :(得分:0)

entitled_product_ids应该采用以下格式:

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

此外,starts_at值应采用ISO 8601格式(请注意,末尾缺少一个0):

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