Welcome Guest, Not a member yet? Register   Sign In
[Resolved] Is CI's XML-RPC class the solution for this problem?
#1

[eluser]Fenix[/eluser]
I am using ISBN DB for a book search.

With a URL like http://isbndb.com/api/books.xml?access_k...0596529963 I get an XML response like this:

Code:
<?xml version="1.0" encoding="UTF-8"?>

<ISBNdb server_time="2009-03-15T17:05:39Z">
    <BookList total_results="1" page_size="10" page_number="1" shown_results="1">
        <BookData book_id="web_2_0_a_strategy_guide" isbn="0596529961" isbn13="9780596529963">
            &lt;Title&gt;Web 2. 0&lt;/Title&gt;
            &lt;TitleLong&gt;Web 2. 0: a strategy guide&lt;/TitleLong&gt;
            <AuthorsText>Amy Shuen</AuthorsText>
            <PublisherText publisher_id="oreilly">Beijing ; O'Reilly, c2008.</PublisherText>
            <Details
                change_time="2009-01-14T06:44:38Z"
                price_time="2009-03-15T16:41:00Z"
                edition_info="(pbk.)"
                language="eng"
                physical_description_text="xxii, 243 p. : ill. ; 23 cm." lcc_number="HD30.2"
                dewey_decimal_normalized="658.4038011"
                dewey_decimal="658.4038011" />
        </BookData>
    </BookList>
</ISBNdb>

I have never parsed XML with CI before and I am wondering what the best way is. Ideally, all this XML would be accessible from an object like any other CI project.

As an example, I would want to be able to access the elements something like this:

Code:
$response->BookData->Title
$response->BookData->AuthorsText

Any suggestions?
#2

[eluser]pistolPete[/eluser]
Please read the user guide or look at xmlrpc.com, then you'll see that XML-RPC is not used to parse generic XML.

You'll need an XML parser like SimpleXML: http://php.net/manual/en/book.simplexml.php
#3

[eluser]Fenix[/eluser]
Right, I looked into both and wasn't sure if there was a CI way to do it.

Also, with SimpleXML I'm not sure how to load xml from an external website like this.

If anybody has done something like this before, an example would be nice.

Thanks
#4

[eluser]pistolPete[/eluser]
[quote author="Fenix" date="1237156196"]Also, with SimpleXML I'm not sure how to load xml from an external website like this.[/quote]

It's pretty simple:
Code:
$xml = simplexml_load_file('http://isbndb.com/api/books.xml?access_key=YS5KDLE8&results=details&index1=isbn&value1=9780596529963');

$bookdata = $xml->BookList->BookData;

echo '<pre>'.print_r($bookdata, TRUE).'</pre>';

output:
Code:
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [book_id] => web_2_0_a_strategy_guide
            [isbn] => 0596529961
            [isbn13] => 9780596529963
        )

    [Title] => Web 2. 0
    [TitleLong] => Web 2. 0: a strategy guide
    [AuthorsText] => Amy Shuen
    [PublisherText] => Beijing ; O'Reilly, c2008.
    [Details] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [change_time] => 2009-01-14T06:44:38Z
                    [price_time] => 2009-03-15T16:41:00Z
                    [edition_info] => (pbk.)
                    [language] => eng
                    [physical_description_text] => xxii, 243 p. : ill. ; 23 cm.
                    [lcc_number] => HD30.2
                    [dewey_decimal_normalized] => 658.4038011
                    [dewey_decimal] => 658.4038011
                )

        )

)
#5

[eluser]jedd[/eluser]
I thinkyou can pass a URL straight to the simplexml_load_file function, but have a [url="http://uk2.php.net/manual/en/function.simplexml-load-file.php"]read at the PHP.net [/url] because there's some issues to do with parsing of the URL. Do-able, though.

I've used simplexml against a local 6MB XML file - it's nice and fast and works mostly as you'd expect.

Two things confused me - working out the object versus array type references for things (but I think this was my own confusion regarding attributes versus 'things'). The other thing that might bite you, looking at your XML above, is that it didn't seem to respect whitespaces (CRs etc) within multi-line fields within the XML. I did some testing a while back and came to this conclusion, but I might have been doing something wrong.
#6

[eluser]Fenix[/eluser]
Thanks for the help so far.

I'm waiting on a MediaTemple support ticket right now because my php.ini file doesn't allow URL file access and for good reason.

Can you think of a workaround? Maybe temporarily store it some other way or something? I haven't worked with files much so I could use some help.

Thanks
#7

[eluser]pistolPete[/eluser]
Is fsockopen() or cURL enabled?
#8

[eluser]Fenix[/eluser]
Yes, cURL is enabled.
#9

[eluser]pistolPete[/eluser]
Code:
$ch = curl_init('http://isbndb.com/api/books.xml?access_key=YS5KDLE8&results=details&index1=isbn&value1=9780596529963');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);      
curl_close($ch);

$xml = simplexml_load_string($response);
$bookdata = $xml->BookList->BookData;

echo '<pre>'.print_r($bookdata, TRUE).'</pre>';


There are also several cURL wrapper classes which you could use:
- http://github.com/shuber/curl/tree/master
- http://codeigniter.com/wiki/Curl_library/
#10

[eluser]Fenix[/eluser]
This is great! It works! Thanks so much!




Theme © iAndrew 2016 - Forum software by © MyBB