【问题标题】:Shopify - API Error when adding a new product (Lexical JSON error)Shopify - 添加新产品时出现 API 错误(词法 JSON 错误)
【发布时间】:2012-09-21 17:57:52
【问题描述】:

当商家添加新产品时,只要在 body_html 字段中输入普通文本,效果就很好。但是,当他们尝试通过复制粘贴或将图像添加到所见即所得编辑器(具有“”)中添加一些 HTML 时,我们得到了著名的:

词法错误:json 文本中的字符无效。

现在,它们可能是从未知来源粘贴的,有没有人想出如何在将 body_html 发送到 ShopifyAPI 之前清理它?

顺便说一句,我正在使用 PHP 和 wcurl.php https://github.com/sandeepshetty/wcurl

更新:

词法错误:json 文本中的字符无效。

              "{"product":{"title":"Sample Event
    (right here) ------^

代码示例:

$shopify_data = array
(
   "product"=>array
   (
     "title"=>$rs->product_title,
     "body_html"=>$rs->product_details,
     "vendor"=>"My Companay",
     "product_type"=>"Laptop"
   )
);

foreach ($variant as $key => $value) {
  $shopify_data["product"]["variants"][$key] = array(
    "option1"=> $value->variant_name,
    "price"=> $value->price,
    "requires_shipping"=>'true',
    "inventory_management"=>"shopify",
    "inventory_quantity"=> $value->quantity
  );
}

// $shopify_data = json_encode($shopify_data);  // This does not work either.
$shopify_data = stripslashes(json_encode($shopify_data));

【问题讨论】:

    标签: php curl shopify


    【解决方案1】:

    如果我理解正确,解决方案是:

    stripslashes(json_encode($params))
    

    我在我的 Shopify 客户端中执行此操作: https://github.com/sandeepshetty/shopify_api/blob/1f538276e690bd7b95f9cbb4007576ecb2d3f6de/client.php#L52

    注意:PHP 5.4.0 在json_encode 中有一个选项。

    【讨论】:

    • 嗨桑迪普。我也试过了。我将代码示例放在问题的底部。
    • 嘿桑迪普。我刚刚意识到,我正在使用您的 Shopify 客户端代码为您完成此操作
    • 所以,如果我只传递没有 StripSlashes 或 Json_Encode 的 Array(),它就可以工作。这是预期的。但是,如果我将 HTML 放入产品 body_html 的正文中,它就会中断。
    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    相关资源
    最近更新 更多