如果您确定没有选项可以取回 xml 或 json,您可以这样做。
从响应中创建一个数组:
<?php
$str = 'Book ID: 524072799
Author: George R. R. Martin
Title: A Song of Ice and Fire, 7 Volumes
Description: Harper Voyager, 2012. New and in stock.. Paperback. New. Book.
ISBN: 9780007477159
Publisher: Harper Voyager
Date: 2012
Currency: GBP
Price: 55.00
Bookseller: El Pinarillo Books
Bookseller_Location: GBR
URL: http://www.biblio.com/details.php?dcx=524072799
Shipping Ground: 3.15
Shipping Expedited: 3.95
';
$lines = explode('<br />',nl2br($str));
$array = array();
foreach($lines as $line){
$line = trim($line);
if(empty($line)){continue;}
$line_parts = explode(':',$line);
$array[$line_parts[0]] = $line_parts[1];
}
/*
You now have Array
(
[Book ID] => 524072799
[Author] => George R. R. Martin
[Title] => A Song of Ice and Fire, 7 Volumes
[Description] => Harper Voyager, 2012. New and in stock.. Paperback. New. Book.
[ISBN] => 9780007477159
[Publisher] => Harper Voyager
[Date] => 2012
[Currency] => GBP
[Price] => 55.00
[Bookseller] => El Pinarillo Books
[Bookseller_Location] => GBR
[URL] => http
[Shipping Ground] => 3.15
[Shipping Expedited] => 3.95
)
*/
?>
然后从该数组创建 XML:
<?php
//To create and output xml from the given array
header('Content-Type: text/xml');
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><books/>');
$node = $xml->addChild('book');
foreach($array as $key=>$value){
$node->addChild(str_replace(' ','_',$key), trim($value));
}
//DOMDocument to format code output
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo $dom->saveXML();
/*
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<Book_ID>524072799</Book_ID>
<Author>George R. R. Martin</Author>
<Title>A Song of Ice and Fire, 7 Volumes</Title>
<Description>Harper Voyager, 2012. New and in stock.. Paperback. New. Book.</Description>
<ISBN>9780007477159</ISBN>
<Publisher>Harper Voyager</Publisher>
<Date>2012</Date>
<Currency>GBP</Currency>
<Price>55.00</Price>
<Bookseller>El Pinarillo Books</Bookseller>
<Bookseller_Location>GBR</Bookseller_Location>
<URL>http</URL>
<Shipping_Ground>3.15</Shipping_Ground>
<Shipping_Expedited>3.95</Shipping_Expedited>
</book>
</books>
*/
?>