nikemen Posted March 5, 2007 Share Posted March 5, 2007 I'm trying to customize a php library administration system to fit my needs. In the system, when I choose to add a book, I'm taken to a page asking to enter ISBN(ASIN). I type it and I accept, then I'm taken to a page to fill the rest of the info about the book. Well, the whole question is about lazyness: I need the system to check ISBNdb.com (a public ISBN database) for that book and fill the data for me. Till now, I've managed to check the information, but I'm an absolute newbie and I don't know how can I ask php to analyze the XML answer from ISDNdb and drop the information on an array. I don't even know how to define an array, indeed. XML answer is something like that: <ISBNdb server_time="2007-03-05T01:40:40Z"> <BookList total_results="1" page_size="10" page_number="1" shown_results="1"> <BookData book_id="works_of_oscar_wilde" isbn="1851705384"> <Title>Works of Oscar Wilde</Title> <TitleLong/> <AuthorsText>Oscar Wilde, Timothy Gaynor (Editor)</AuthorsText> <PublisherText publisher_id="senate">Senate</PublisherText> <Details dewey_decimal="" physical_description_text="800 pages" language="" edition_info="Unknown Binding; 1997-07-25" dewey_decimal_normalized="" lcc_number="" change_time="2007-03-05T00:10:26Z" price_time="2007-03-05T00:10:47Z"/> </BookData> </BookList> </ISBNdb> I need, then, to have the text between <Title> and </Title> in $o_livre->Title the one between <AuthorsText> and </AuthorsText> in $o_livre->AuthorsText the publisher in $o_livre->PublisherText the text after edition_info=" and before the ; (Unknown Binding) in $o_livre->Lieu and the date after the ; in $o_livre->ReleaseDate I'm calling to a function in the main php file this way: $o_livre=get_book_isbn($rechcode) and the function, in functions.inc, is: function get_book_isbn($rechcode) { $return=readfile('http://isbndb.com/api/books.xml?access_key=XXXXXXXX&results=details&index1=isbn&value1='.$rechcode); return($return); } HELP PLEASE! Many thanks for your help! Quote Link to comment Share on other sites More sharing options...
btherl Posted March 5, 2007 Share Posted March 5, 2007 Are you using php 4 or 5? If 5, then you can use this: http://sg.php.net/manual/en/ref.simplexml.php If 4, then it's messier.. Quote Link to comment Share on other sites More sharing options...
nikemen Posted March 6, 2007 Author Share Posted March 6, 2007 I'm using PHP5 and I gave a look to the function you said... the code I have now is: function get_book_isbn($rechcode) { $url = 'http://isbndb.com/api/books.xml?access_key=XXXXXXXX&results=details&index1=isbn&value1='.$rechcode); $xml = simplexml_load_file($url); return($xml); } Giving back no results... I'm actually a newbie and I can't figure out how to do that... Many thanks for your help. Quote Link to comment Share on other sites More sharing options...
btherl Posted March 6, 2007 Share Posted March 6, 2007 There's this note in the docs for that function: "Note: Libxml 2 unescapes the URI, so if you want to pass e.g. b&c as the URI parameter a, you have to call simplexml_load_file(rawurlencode('http://example.com/?a=' . urlencode('b&c'))). Since PHP 5.1.0 you don't need to do this because PHP will do it for you." But easier might be to grab the whole thing and then pass it to simplexml. Try this: $xmlstr = file_get_contents('http://isbndb.com/api/books.xml?access_key=XXXXXXXX&results=details&index1=isbn&value1='.$rechcode); $xml = new SimpleXMLElement($xmlstr);[/code] Quote Link to comment Share on other sites More sharing options...
nikemen Posted March 6, 2007 Author Share Posted March 6, 2007 Still nothing. Does that automatically save the different entities of the xml doc into different parts of an array? Here is an abstract of the code of the addbook php file: $o_livre=get_book_isbn($rechcode); ?> <center><H3> <?php echo _("Ajouter un livre"); ?> </H3> <form action=admin.php METHOD=POST> <table border=0> <tr> <td> <?php echo _("Titre"); ?> :</td><td><input type="text" name="liv_nom" size=30 value="<?php if ($soap) echo utf8_decode($o_livre->Title); ?>"></td></tr> <tr> <td> <?php echo _("Volume"); ?> :</td><td><input type="text" name="liv_volume" size=4 value=""></td></tr> <tr> <td> <?php echo _("Numéro"); ?> :</td><td><input type="text" name="liv_num" size=16" value=<?php echo '978' . $rechcode; ?>></td></tr> <tr> <td> <?php echo _("Étiqueté"); ?> :</td><td><input type="checkbox" name="liv_etiquete" value=1></td></tr> <tr> <td> <?php echo _("Auteur"); ?> :</td><td><input type="text" name="liv_auteur" size=30 value="<?php if ($soap) echo utf8_decode($o_livre->AuthorsText); ?>"></td> </td> <tr> <td> <?php echo _("Collection"); ?> :</td><td><input type="text" name="liv_coll" size=30></td> <tr> <td> <?php echo _("Edition"); ?> :</td><td><input type="text" name="liv_ed" size=15 value="<?php if ($soap) echo utf8_decode($o_livre->PublisherText); ?>"></td> <tr> <td> <?php echo _("Lieu d'edition"); ?> :</td><td><input type="text" name="liv_lieu" size=15 value="<?php if ($soap) echo utf8_decode($o_livre->Manufacturer); ?>"></td> <tr> <td> <?php echo _("Date"); ?> :</td><td><input type="text" name="liv_date" size=10 value="<?php if ($soap) echo substr($o_livre->ReleaseDate,-4); ?>"></td> </td> And here the actual text of the function in functions.inc: function get_book_isbn($rechcode) { $xmlstr = file_get_contents('http://isbndb.com/api/books.xml?access_key=XXXXXXXX&results=details&index1=isbn&value1='.$rechcode); $xml = new SimpleXMLElement($xmlstr); return($xml); } What can I do? Quote Link to comment Share on other sites More sharing options...
artacus Posted March 6, 2007 Share Posted March 6, 2007 echo out $xmlstr and be sure you are getting data back. Quote Link to comment Share on other sites More sharing options...
nikemen Posted March 6, 2007 Author Share Posted March 6, 2007 If I echo $xmlstr I get data from xml printed to screen, but I don't know how can I get it (in function get_book_isbn($rechcode)) into an array and apply it later as a default value on the php file... Thanks for your help! Quote Link to comment Share on other sites More sharing options...
btherl Posted March 6, 2007 Share Posted March 6, 2007 Actually SimpleXML gives you an object.. if you want an array I think you will have to convert it yourself unfortunately. Or you could alter your code to use objects instead. Quote Link to comment Share on other sites More sharing options...
nikemen Posted March 6, 2007 Author Share Posted March 6, 2007 Well... how can I convert it or... incase using objects is easyer: what modification should I do to the next piece of code to use an object instead of $o_livre array?: $o_livre=get_book_isbn($rechcode); ?> <center><H3> <?php echo _("Ajouter un livre"); ?> </H3> <form action=admin.php METHOD=POST> <table border=0> <tr> <td> <?php echo _("Titre"); ?> :</td><td><input type="text" name="liv_nom" size=30 value="<?php echo utf8_decode($o_livre->Title); ?>"></td></tr> <tr> <td> <?php echo _("Auteur"); ?> :</td><td><input type="text" name="liv_auteur" size=30 value="<?php echo utf8_decode($o_livre->AuthorsText); ?>"></td> </td> MANY THANKS! Quote Link to comment Share on other sites More sharing options...
Barand Posted March 6, 2007 Share Posted March 6, 2007 Using the sample you posted (saved as book1.xml) <?php $xml = simplexml_load_file('book1.xml'); $isbn = $xml->BookList->BookData['isbn'] ; $title = $xml->BookList->BookData->Title ; echo "$isbn : $title"; ?> --> 1851705384 : Works of Oscar Wilde Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.