No title field in the XML for a work?

DiscussãoLibraryThing API Development

Aderi ao LibraryThing para poder publicar.

No title field in the XML for a work?

Este tópico está presentemente marcado como "adormecido"—a última mensagem tem mais de 90 dias. Pode acordar o tópico publicando uma resposta.

1spikeassociates
Out 30, 2009, 2:55 pm

I am building a simple loans system for Spike Associates, a small members group with a library (400 books). I would like to use the ISBN number of each book to link my database of loans information to LT using the API.

Currently I grab the XML data for a given ISBN in order to give the admin something a bit more useful than just an ISBN number to look at. I am not expecting more than two or three loans per day, so the request limit is prefectly sufficient.

As far as I can see there is no field in the XML file for the title of the work (perhaps it is assumed that if I know the ISBN number of a book, I probably know what it is called).

You can play with my creation here: http://ben.sandhost.com/assoclib

Regards,

Ben

2timspalding
Out 30, 2009, 3:42 pm

Which API are you using?

4spikeassociates
Nov 1, 2009, 10:34 pm

Also, I notice there is no ISBN tag in the XML for a work. Is there another way to get information on a book by using its ISBN number and Library Thing's generous APIs?

Ben

5timspalding
Nov 2, 2009, 12:58 am

The CK API isn't really what you want. I'm asking Chris to reply.

6spikeassociates
Nov 2, 2009, 6:35 am

Hello Tim,

I have been looking at JSON too, but my knowledge of Java is even worse than my knowledge of PHP.

If I have to pay to use Library Thing in this way, I am sure Spike Island (the people I am doing the internship with) will be happy to stump up!

Any advice will be greatly appreciated.

Ben

7conceptDawg
Editado: Nov 2, 2009, 1:03 pm

Yeah. You really don't want to be using the Common Knowledge API to do what you're doing. The CK API results are geared toward giving you just that information, not the holistic information about the book or work records themselves.

Based on the link you gave me you're not really dealing with "works." You're dealing with "books." There's a significant difference there. What you probably want to be using is Tim's JSON Books API:
http://www.librarything.com/wiki/index.php/LibraryThing_JSON_Books_API

It's JSON instead of XML but it shouldn't be too hard for you to deal with. As for the specifics, if you have any questions Tim will probably need to answer them since he wrote that API.

Edit: It's not really "Java" or Javscript per se. It's JSON, which is just a data format. You can easily decode it in PHP with json_decode() and it will turn it into a handy array for you.

8spikeassociates
Nov 2, 2009, 8:08 pm

Thanks for the post conceptDawg, I have been playing with the JSON API all evening, but I am getting "NULL" as the var_dump for my decoded JSON string. It is most upsetting.

Any known reason for PHP not liking the JSON string?

Ben

9conceptDawg
Nov 2, 2009, 11:15 pm

Nope. PHP loves itself some JSON. Shouldn't be a problem at all. We use it all the time.

I'm kind of embarrassed by the next statement, but: I've never actually used that API to do anything so I'm not really sure about the options (I said that Tim wrote it). ;)

It looks like it doesn't return JSON, but a Javascript variable that has JSON. I personally think that's a "wrong" way to do things but that's the way it is. I really thought there was an option to return the raw JSON data too but I don't see it in the documentation.

At any rate, you can get the raw data by just stripping off the
var widgetresults =

and the
;LibraryThing.bookAPI.displayWidgetContents(widgetResults, "LT_Content");


and then feeding the resulting string through json_decode.

The raw JSON data is the stuff in the curly brackets (including the brackets).

10spikeassociates
Nov 4, 2009, 8:23 pm

Hi conceptDawg,

Hope you don't mind me picking your brains some more. Your responces have been very useful.

I know its awful manners, but I'm going to dump my code on you:

It returns 'NULL' in big capital letters.

Can you see any obvious reason that this would not work? I have stripped what appears outside the curly brackets, escaped the quotes, etc. I cannot see the problem.

Or our JSON API has got a flat tyre???

Ben

11spikeassociates
Nov 4, 2009, 8:30 pm

Didn't seem to let me throw php around ...

I tried the method you explained, and also this one:

$lt_data = file_get_contents($url);
$lt_data = ltrim($lt_data,20);
$lt_data = rtrim($lt_data,72);
$json = json_decode($lt_data);
var_dump($json);

Both attempts ended in a NULL message.

Ben

12conceptDawg
Editado: Nov 4, 2009, 11:08 pm

Make sure that you have showstructure set to 0. If it is set to 1 it will screw up the output. Here's a quick stab at the handler:

$lt_data = file_get_contents($url);
preg_match("#{(.*)}#", $lt_data, $matches);
$data = $matches[0];
$json = json_decode($data, 1);

13spikeassociates
Nov 8, 2009, 7:54 am

Brilliant, I get a nice array returned with that (http://ben.sandhost.com/assoclib/json.php). This moves us onto my next issue: using a PHP variable in the JSON path.

I would like to be able to get the title of a book by using its LT work ID (as represented here by $wid)

$title = $json->books->$wid->title;
echo "$title";

I can see two possible problems with this, firstly $wid is a number and may not be a valid name for a variable, secondly the way I have done it may not be the correct method for dropping a variable into the JSON path.

Any info you can offer pertaining to this specific would be greatly appreciated.

Ben

14spikeassociates
Nov 8, 2009, 8:01 am

Sorry, I haven't expressed myself properly above. $wid is a number and this is an issue in the JSON path code.

Example:

$json->books->51054225->title;

Returns:

Parse error: syntax error, unexpected T_LNUMBER

I tried a few combinations of quotes/brackets/curly brackets, but couldn't get it to spit out the book title.

Ben

15spikeassociates
Nov 8, 2009, 10:39 pm

4:35am GMT

Proud to announce that I have solved my issue, by using a bunch of foreaches:

foreach ($json as $key => $value){
if($key == "books"){
foreach ($value as $subkey => $subvalue){
foreach ($subvalue as $key => $value){
if($value == $isbn){
$title = $subvalue"title";
$author = $subvalue"author_fl";
}
if (++$i == 1) break;
}
}
}
}

Don't know if that makes any sense. I can try and explain if anyone is interested.

Ben