【问题标题】:Ebay item description destroy my page structureEbay 商品描述破坏了我的页面结构
【发布时间】:2019-11-06 07:32:07
【问题描述】:

我正在使用 ebay api 通过callname=GetSingleItem 将项目提取到我的网站,一切正常。

请查看以下我用来获取项目详细信息的代码段

    $res = $api -> get ( "http://open.api.ebay.com/shopping?"
                        . "callname=GetSingleItem&"
                        . 
   "IncludeSelector=Description,ItemSpecifics,Details&"
                        . "ItemID=" . $listing -> platform_key . '&'
                        . "appid=$ebayAppId&"
                        . "version=".$version ) ;

                    $xml = simplexml_load_string ( $res -> getBody () ) ;
                    $json = json_encode ( $xml ) ;
                    $jsonResult = json_decode ( $json , TRUE ) ;

然后我通过下面的代码段获取它的详细信息

     $ebayItemArray[ "description" ] = ( $jsonResult[ "Item" ][ "Description" ] 
   )

请注意,以上代码段只是示例代码

所以我通过Laravel blade.php中的以下代码段使用此描述

      <div class=container text-left">
                            {!!$ebayItemArray[ "description" ]!!}
                        </div>

所以这很好对于某些产品没有任何错误,但对于某些产品,当我尝试显示描述时它会破坏我的布局样式如下

当我从刀片文件中删除描述变量时,一切正常

【问题讨论】:

    标签: laravel-blade ebay-api ebay-design-templates


    【解决方案1】:

    根据文档:

    如果使用了描述值,则返回完整的描述,以及卖家在列表中使用的所有 HTML、XML 或 CSS 标记(如果有)。仅查看列表描述的实际文本(无标记标签),应使用 TextDescription 值。

    所以可能是额外的 CSS 破坏了您的页面结构。

    你可以选择

    1. 将描述值转换为纯文本
    // Not tested...
    $description = $jsonResult[ "Item" ][ "Description" ];
    $doc = new DOMDocument();
    $doc->loadHTML($description); // Load as HTML
    removeElementsByTagName('style', $doc); // Remove the <style> Tag
    $description = strip_tags($doc->textContent); // To plain Text
    $ebayItemArray[ "description" ] = $description;
    
    1. 使用IncludeSelector=TextDescription
    $res = $api -> get ( "http://open.api.ebay.com/shopping?"
                            . "callname=GetSingleItem&"
                            . 
       "IncludeSelector=TextDescription,ItemSpecifics,Details&"
                            . "ItemID=" . $listing -> platform_key . '&'
                            . "appid=$ebayAppId&"
                            . "version=".$version ) ;
    
    1. 在插入说明的地方使用 iFrame
    <iframe 
      title="Description" 
      srcdoc="{!!$ebayItemArray[ "description" ]!!}"
      width="300px" height="300px"
    ></iframe>
    

    使用 iFrame,您还需要自动调整 iFrame 的高度。

    【讨论】:

    • 非常感谢您的回答,如果您也能提供一些代码示例,那就太好了
    • 非常感谢您的帮助???