2012-09-21 91 views
2

当商家添加新产品时,只要他们在body_html字段中键入普通文本,它就会很好。但是,当他们尝试从复制粘贴或添加图像到所见即所得编辑器(具有“”)时添加一些HTML时,我们会得到以下着名的代码:Shopify - 添加新产品时出现API错误(Lexical JSON错误)

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

现在,他们可能会粘贴来自未知来源,有没有办法找到我可以在发送到ShopifyAPI之前清理body_html?

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

UPDATE:

词法错误:在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)); 

回答

2

如果我理解这个正确的解决办法是:

stripslashes(json_encode($params)) 

我在Shopify客户做到这一点: https://github.com/sandeepshetty/shopify_api/blob/1f538276e690bd7b95f9cbb4007576ecb2d3f6de/client.php#L52

注:PHP 5.4.0有一个o这在json_encode

+0

嗨Sandeep。我也尝试过。我把代码示例放在我的问题的底部。 –

+0

嘿Sandeep。我刚刚意识到,我正在使用Shopify客户端代码,它为您做好了准备 –

+0

因此,如果我只是在没有StripSlashes或Json_Encode的情况下传递Array(),它就可以工作。这是预期的。但是,如果我将HTML放入产品Body_html的正文中,则会中断。 –