2012-09-21 89 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的正文中,則會中斷。 –