Jump to content

Array from XML


nikemen

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.