通过api请求XML返回字符串而不是xml对象php

时间:2015-08-13 08:44:47

标签: php xml api magento

我坚持了几个小时并且无法解决这个问题。

案例是我正在请求XML数据的API .API返回xml数据,但当我转储该响应时,它以字符串的形式显示。当我使用postman工具查看响应时,它显示在xml中

我的代码是:

    $response = file_get_contents('http://ws.nielsenbookdataonline.com/BDOLRest/RESTwebServices/BDOLrequest?clientId=XXXX&password=YYYY&from=0&to=20&indexType=0&format=7&resultView=2&field0=1&value0=9781784080808');
var_dump($response);die();
$xmlObj = new Varien_Simplexml_Config($response);
$xmlData = $xmlObj->getNode();
$data = xml2array($xmlData);

function xml2array ( $xmlObject, $out = array () )
{

        foreach ( (array) $xmlObject as $index => $node )
            $out[$index] = ( is_object ( $node ) ||  is_array ( $node ) ) ? xml2array ( $node ) : $node;

        return $out;
}

这是我转储的回复

  

string(4977)“BFAU353700101 9781784080808 978-1-78408-080-8先生们   和Sledgers:100个语录中的灰烬史绅士和绅士   Sledgers 100 of Quhesations中的灰烬历史A01(作者)N   Smyth,Rob A01(作者)N Rob Smyth A01(作者)N Smyth,Rob   A01 By(作者)N Rob Smyth 234 153 33 476 BB精装本288 8pp col   Illus Zeus英国宙斯负责人267874 267874 G General   (美国:贸易)WSJC Cricket DC23 796.35865 T15.1 Ball Games:Field&   户外板球最大的竞争故事,通过   从出生开始,100种不拘一格和有趣的语录背后的故事   1882年的灰烬到2013-14系列的戏剧性。来自   在英格兰首次遭遇失败之后,这是一个着名的模仿ob告   1882年澳大利亚在本土,对场上的侮辱(或'雪橇')   今天,Ashes板球已经产生了几乎令人难忘的报价   它有球保龄球和得分。 GENTLEMEN和SLEDGERS表示   131年来英澳板球运动的潮起潮落   通过讲述100个令人难忘的灰烬背后的故事来进行314场比赛   报价。来自快速投球手杰夫汤姆森的经典'我喜欢击中一个   击球手不仅仅是让他出局。我喜欢在球场上看到鲜血'   1975年,迈克尔克拉克对吉米安德森提出的臭名昭着的建议   在2013年准备好了一个f ******断臂',报价包含了讽刺,   侮辱,黑暗的雪橇艺术的例子 - 甚至是   偶尔考虑上市判断。唤起难忘的时刻   澳大利亚和澳大利亚的职业生涯以及比赛以及高潮和低谷   英格兰最伟大的球员,GENTLEMEN和SLEDGERS是非正式的,   随心所欲,充满乐观和有趣的历史   灰烬。 Rob Smyth是一名卫报和电报体育记者和   “板球之灵”的作者:什么使板球成为最伟大的比赛   在地球上,丹麦炸药的合着者:The Story of the   足球最伟大的邪教队。 “一本优秀的精彩书籍   提醒板球的过去,一些伟大的历史支持   有一些很棒的照片......对于所有板球迷来说都是必须的   一本书,你可以浸泡进出,不仅仅是在   夏天的板球'Goodreads。 '为期5天的5测试Ashes系列仍然存在   在体育和世界最具史诗般的比赛之一   Rob的书帮助我们了解'Counterfire'的原因。 GB AU   IE NZ ZA ROW CA US 20150521 04 Active Harper Collins 20150501 16.99   16.99 20150223 02 RRP含税26.95 26.95地方税可适用01 RRP不含税19.99 18.17 20150227 02 RRP含税22.99   19.99 20150227 02 RRP含税20可用20150514 21有货21有货20150513 21有货20150513 Macmillan Distribution(MDL)   41449独立出版商集团Jonathan Ball Publishers SA 74524   HarperCollins Publishers Pty Ltd 115750 Harper Entertainment   分销服务262704 HarperCollins新西兰115751 BERT   GARD ING Y“

这是使用Postman工具从API返回的XML:

<Result>
    <clientId>XXXXX</clientId>
    <format>7</format>
    <resultCode>00</resultCode>
    <hits>1</hits>
    <from>0</from>
    <to>1</to>
    <data>
        <?xml version="1.0" encoding="ISO-8859-1"?>
        <data>
            <record>
                <ISBN13>9781784080808</ISBN13>
                <ISBN13H>978-1-78408-080-8</ISBN13H>
                <FTS>Gentlemen and Sledgers: A History of the Ashes in 100 Quotations</FTS>
                <TL>Gentlemen and Sledgers</TL>
                <ST>A History of the Ashes in 100 Quotations</ST>
                <CR1>A01</CR1>
                <CRT1>By (author)</CRT1>
                <CCI1>N</CCI1>
                <CNSI1>Smyth, Rob</CNSI1>
                <CR1>A01</CR1>
                <CRT1>By (author)</CRT1>
                <CCI1>N</CCI1>
                <CNS1>Rob Smyth</CNS1>
                <CR1>A01</CR1>
                <CRT1>By (author)</CRT1>
                <CCI1>N</CCI1>
                <CNI1>Smyth, Rob</CNI1>
                <CR1>A01</CR1>
                <CRT1>By (author)</CRT1>
                <CCI1>N</CCI1>
                <CNF1>Rob Smyth</CNF1>
                <HMM>234</HMM>
                <WMM>153</WMM>
                <SMM>33</SMM>
                <WG>476</WG>
                <PFC>BB</PFC>
                <PFCT>Hardback</PFCT>
                <PAGNUM>288</PAGNUM>
                <ILL>8pp col illus</ILL>
                <IMPN>Head of Zeus</IMPN>
                <PUBN>Head of Zeus</PUBN>
                <COP>United Kingdom</COP>
                <IMPID>267874</IMPID>
                <PUBID>267874</PUBID>
                <NAC1>G</NAC1>
                <NAT1>General (US: Trade)</NAT1>
                <BIC2SC1>WSJC</BIC2SC1>
                <BIC2ST1>Cricket</BIC2ST1>
                <DEWS1>DC23</DEWS1>
                <DEWEY1>796.35865</DEWEY1>
                <PRODCC>T15.1</PRODCC>
                <PRODCT>Ball Games: Field &amp; Outdoor</PRODCT>
                <AUSSD>The story of cricket's greatest rivalry, told through the tales behind 100 eclectic and entertaining quotations - from the birth of the Ashes in 1882 to the dramatic 2013-14 series.</AUSSD>
                <AUSLD>From the celebrated mock obituary following England's first-ever defeat by Australia on home soil in 1882, to the on-pitch insults (or 'sledges') of today, Ashes cricket has spawned nearly as many memorable quotes as it has balls bowled and runs scored. GENTLEMEN AND SLEDGERScharts the ebb and flow of Anglo-Australian cricketing fortunes across 131 years and 314 matches by telling the stories behind 100 memorable Ashes quotations. From fast bowler Jeff Thomson's classic 'I enjoy hitting a batsman more than getting him out. I like to see blood on the pitch' in 1975, to Michael Clark's notorious advice to Jimmy Anderson to 'get ready for a f****** broken arm' in 2013, the quotations embrace quips, insults, examples of the dark art of sledging - and even the occasional considered cricketing judgement. Evoking memorable moments and matches as well as highs and lows in the careers of Australia and England's greatest players, GENTLEMEN AND SLEDGERS is an informal, freewheeling, discursive and entertainingly opinionated history of the Ashes.</AUSLD>
                <AUSBIOG>Rob Smyth is a Guardian and Telegraph sports journalist and the author of The Spirit of Cricket: What Makes Cricket the Greatest Game on Earth, and the co-author of Danish Dynamite: The Story of Football's Greatest Cult Team.</AUSBIOG>
                <AUSREV>'A wonderful book that is an excellent reminder of cricket's past, some great slices of history backed up with some fantastic pictures ... a must have for all cricket fans and a book that you are able to dip in and out of not just during the summer of cricket' Goodreads. 'The 5-day 5-test Ashes series remained throughout one of the most epic contests in the world of sport and Rob's book helps us to appreciate the reasons why' Counterfire.</AUSREV>
                <ERSL>GB AU IE NZ ZA ROW</ERSL>
                <NFSRSL>CA US</NFSRSL>
                <PUBPD>20150521</PUBPD>
                <PUBSC>04</PUBSC>
                <PUBST>Active</PUBST>
                <AUSLPUBNAME>Harper Collins</AUSLPUBNAME>
                <AUSLPUBD>20150501</AUSLPUBD>
                <GBPCCPRRRP>16.99</GBPCCPRRRP>
                <GBPCCPRRRPLT>16.99</GBPCCPRRRPLT>
                <GBPCCPLCD>20150223</GBPCCPLCD>
                <GBPCCPTC>02</GBPCCPTC>
                <GBPCCPTD>RRP including tax</GBPCCPTD>
                <USDCCPRRRP>26.95</USDCCPRRRP>
                <USDCCPRRRPLT>26.95</USDCCPRRRPLT>
                <USDCCPRPN>Local taxes may apply</USDCCPRPN>
                <USDCCPTC>01</USDCCPTC>
                <USDCCPTD>RRP excluding tax</USDCCPTD>
                <AUDCCPRRRP>19.99</AUDCCPRRRP>
                <AUDCCPRRRPLT>18.17</AUDCCPRRRPLT>
                <AUDCCPLCD>20150227</AUDCCPLCD>
                <AUDCCPTC>02</AUDCCPTC>
                <AUDCCPTD>RRP including tax</AUDCCPTD>
                <NZDCCPRRRP>22.99</NZDCCPRRRP>
                <NZDCCPRRRPLT>19.99</NZDCCPRRRPLT>
                <NZDCCPLCD>20150227</NZDCCPLCD>
                <NZDCCPTC>02</NZDCCPTC>
                <NZDCCPTD>RRP including tax</NZDCCPTD>
                <UKNBDPAC>20</UKNBDPAC>
                <UKNBDPAT>Available</UKNBDPAT>
                <UKNBDPASLCD>20150514</UKNBDPASLCD>
                <USNBDPAC>21</USNBDPAC>
                <USNBDPAT>In stock</USNBDPAT>
                <AUSNBDPAC>21</AUSNBDPAC>
                <AUSNBDPAT>In stock</AUSNBDPAT>
                <AUSNBDPASLCD>20150513</AUSNBDPASLCD>
                <NZNBDPAC>21</NZNBDPAC>
                <NZNBDPAT>In stock</NZNBDPAT>
                <NZNBDPASLCD>20150513</NZNBDPASLCD>
                <UKADN1>Macmillan Distribution (MDL)</UKADN1>
                <UKADI1>41449</UKADI1>
                <USADN1>Independent Publishers Group</USADN1>
                <SAADN1>Jonathan Ball Publishers SA</SAADN1>
                <SAADI1>74524</SAADI1>
                <AUSADN1>HarperCollins Publishers Pty Ltd</AUSADN1>
                <AUSADI1>115750</AUSADI1>
                <NZADN1>Harper Entertainment Distribution Services</NZADN1>
                <NZADI1>262704</NZADI1>
                <NZADN2>HarperCollins New Zealand</NZADN2>
                <NZADI2>115751</NZADI2>
                <WSLRUK>BERT GARD</WSLRUK>
                <WSLREXUK>ING</WSLREXUK>
                <IMAGFLAG>Y</IMAGFLAG>
            </record>
        </data>
    </data>
</Result>

当我转储$ data时,它返回false。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我刚刚遇到了完全相同的问题,它是来自提供商的格式错误的XML。 原因是这一行:

<?xml version="1.0" encoding="ISO-8859-1"?>

删除它,或将其移动到整个字符串的开头,它将解析好。

没有想法为什么它应该在开始时位于XML字符串的中间。

我已经用尼尔森的支持提出了它,但是看到你3年前遇到同样的问题很有意思!