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


requesting XML through api returns string instead of xml object php

我被这个问题困了几个小时,还是没能弄清楚。

的情况是,我正在请求一个API的XML数据。API返回xml数据,但是当我转储响应时,它以字符串的形式显示。然而,当我使用邮差工具查看响应时,它显示在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;
}

这是我转储的响应

字符串(4977)bfa353700101 9781784080808 978-1-78408-080-8先生们《雪撬:100句名言中的灰烬史《雪橇:100句名言中的灰烬史》(作者)N罗伯·史密斯A01作者:罗伯·史密斯A01作者:罗伯·史密斯A01 By (author) N Rob Smyth 234 153 33 476 BB精装本288 8pp colillus宙斯之头宙斯之头联合王国267874 267874 G将军(美国:贸易)WSJC板球DC23 796.35865 T15.1球类运动:Field &户外板球最大的竞争故事,通过100句兼收并蓄的有趣语录背后的故事——从出生开始从1882年的灰烬队到2013-14赛季的系列赛从在英格兰队第一次被英格兰队击败后,庆祝了一篇模拟讣告1882年,澳大利亚主场,对球场上的侮辱(或"雪橇")时至今日,灰烬板球催生出的令人难忘的名言几乎与它有投球和得分。《绅士与雪橇131年来英澳板球命运的起起落落以及314场比赛,讲述100场令人难忘的灰烬背后的故事报价。从快速投球手杰夫·汤姆森的经典之作"我喜欢击球。击球手不仅仅是让他出局。我喜欢在球场上看到鲜血。"1975年,迈克尔·克拉克(Michael Clark)给吉米·安德森(Jimmy Anderson)的臭名昭著的建议是"get"准备好迎接2013年的"******断胳膊"吧,语录中包含了俏皮话,侮辱,雪橇的黑暗艺术的例子,甚至偶尔考虑板球判断。唤起难忘的时刻和比赛以及高潮和低潮在澳大利亚和英国最伟大的运动员,绅士和雪橇是一个非正式的,自由的,散漫的和有趣的固执己见的历史灰烬。罗布·史密斯是《卫报》和《每日电讯报》的体育记者《板球的精神:是什么让板球成为最伟大的运动》的作者也是《丹麦炸药:关于……的故事》的合著者足球界最受崇拜的球队。"一本精彩绝伦的书。提醒着板球的过去,一些伟大的历史片段得以重现有一些奇妙的照片…这是所有板球迷的必备品一本书,你可以沉浸在和退出不仅仅是在板球之夏。为期5天的《灰烬》系列赛保留了下来纵观体育界最史诗般的比赛之一罗布的书帮助我们理解"反击"的原因。GB的盟IE NZ ZA ROW CA US 20150521 04 Active Harper Collins 20150501 16.9916.99 20150223 02 RRP含税26.95 26.95地方税可适用01 RRP不含税19.99 18.17 20150227 02 RRP含税22.9919.99 20150227 02 RRP含税20 Available 20150514 21现货21现货20150513 21现货20150513麦克米伦分销(MDL)41449独立出版商集团乔纳森鲍尔出版商SA 74524哈珀柯林斯出版社股份有限公司115750哈珀娱乐分销服务262704哈珀柯林斯新西兰115751 BERT

这是API使用Postman工具返回的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

我刚刚遇到了完全相同的问题,它是来自提供者的格式不正确的XML。原因是:

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

去掉这个,或者把它移到整个字符串的开头,它就可以解析了。

不知道为什么它在XML字符串的中间,而它应该在开始。

我已经在尼尔森的支持下提出了这个问题,但有趣的是,3年前你也遇到过同样的问题!