Youtube API and cURL
Mar 28, 2012 Code
I was recently working with the YouTube API, and was having some frustrations getting a particular piece of data I was looking for. After a bit of Googling, I quickly discovered that other people were having a similar issue. I finally was able to dig up the answer, buried somewhere in an IBM document.
People were struggling with the way the XML was being interpreted by the standard PHP simplexml object, and couldn’t seem to accurately target the media or stats portions of the code. This means it’s difficult to get the video length and view counts.
What I ended up doing, was creating a function that would accept a YouTube username as a parameter, and return an array of video information for all videos that user has uploaded. Here’s that function:
function get_youtube_videos($max_number, $user_name) { $xml = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/' . $user_name . '/uploads?max-results=' . $max_number); $server_time = $xml->updated; $return = array(); foreach ($xml->entry as $video) { $vid = array(); $vid['id'] = substr($video->id,42); $vid['title'] = $video->title; $vid['date'] = $video->published; //$vid['desc'] = $video->content; // get nodes in media: namespace for media information $media = $video->children('http://search.yahoo.com/mrss/'); // get the video length $yt = $media->children('http://gdata.youtube.com/schemas/2007'); $attrs = $yt->duration->attributes(); $vid['length'] = $attrs['seconds']; // get video thumbnail $attrs = $media->group->thumbnail[0]->attributes(); $vid['thumb'] = $attrs['url']; // get <yt:stats> node for viewer statistics $yt = $video->children('http://gdata.youtube.com/schemas/2007'); if (isset($yt->statistics)) { $attrs = $yt->statistics->attributes(); $vid['views'] = $attrs['viewCount']; } else { $vid['views'] = "0"; } array_push($return, $vid); } return $return; }
And here’s the implementation:
$max_videos = 9; $user_name = 'thelonelyisland'; $videos = get_youtube_videos($max_videos, $user_name); foreach($videos as $video) { echo $video['title'] . '<br/>'; echo $video['id'] . '<br/>'; echo $video['date'] . '<br/>'; echo $video['views'] . '<br/>'; echo $video['thumb'] . '<br/>'; echo $video['length'] . '<br/>'; echo '<hr/>'; }
Update (30 Mar 12, 12:30 AM EST)
Fun fact: Just before this went to a client (IE one of the largest companies in Canada), we discovered it was broken. Turns out that videos with zero views were returning a null value somewhere, causing the function to explode. Since most of their videos where at least a month old, this made it through testing, until they happened to upload a new video just before launch. Close call. I have now updated the post to include the latest, greatest version of this function.
SIP Strings For Patron Accounts
Previously, we’ve looked at how to make our web server talk to our SIP server, and how to make PHP talk to our SIP server. Let’s start to retrieve some real data from SIP.
Sending some garbage text to SIP, and receiving a 96 code is great, but now let’s define some real codes. To do this, log into your SIP server. Navigate to where you have installed SIP. You should see a folder called ‘sirsidynix’. This folder will contain folders for each of the SIP connections you have a license for (so as an example, we have 8). Each one should be named in a fairly straightforward way. Go into the one that you will be using.
Now that you’re into the right folder for your connection, navigate into the bin folder. Here you should see a file called AcsTester.exe. Run that program and you should see something like this screen shot.
At the top of the program window, you’ll see a button labeled “Create Message”. Clicking that will give you a list of different messages that you can send to SIP. For this excercise, let’s use Patron Status Request (but also take note of the “Fee Paid” message – you should be connecting dots at this point).
As far as I know, what you will see at this point will vary slightly from connection to connection, based on the settings and setup for that connection. For example, requiring the patron’s pin may be turned off, requiring a transaction date may be turned off, and likely any other number of settings can affect this. I’m going to show a screen shot of what I entered, so that you can follow along when I generate my actual SIP message.
The end result is this message string:
2300120110501 102705|AO|AAmyBarcodeHere|AC|AD1234|AY0AZF001
Let’s feed this string into the code we created in the last article, and we should receive back a real patron’s status.
Basic Code
<?php $sip_server_address = 'MyServerName'; $sip_server_port = 2023; $fp = fsockopen($sip_server_address,$sip_server_port,$errno,$errstr); stream_set_blocking($fp, 0); $check = '2300120110501 102705|AO|AAmyBarcodeHere|AC|AD1234|AY0AZF001'; fputs($fp, "$check\r"); sleep(2); $response = fread($fp, 256); echo $response; ?>
This should return a code for the user. Again, this code will look a little different from system to system, but should be fairly recognizable regardless. If the user has no fines, it will typically have this code in it “BLY|CQY”. We’ll set up our message to be a bit more dynamic below, so you can try a few barcodes, with a few results to see how a patron with a block appears, a patron that owes fines, a patron that is in good standing, etc.
Break It Down Further
So we should know have the basic code working. Let’s separate out that $check variable and make it a bit more dynamic so that it’s built off of the current date, and with whatever barcode you want. Then we can start to trap our output and return some real messages.
$sip_server_address = 'MyServerName'; $sip_server_port = 2023; $barcode = '999999999'; $pin = '1234'; $fp = fsockopen($sip_server_address,$sip_server_port,$errno,$errstr); stream_set_blocking($fp, 0); $check = '23001' . date('dmY') . ' 102705|AO|AA' . $barcode . '|AC|AD' . $pin . '|AY0AZF001'; fputs($fp, "$check\r"); sleep(2); $response = fread($fp, 256); switch($response) { case (strpos($response,'BLY|CQY|AY')!==FALSE): echo '<p>You do not owe any fines!</p>'; break; case (strpos($response,'BLY|CQY|BH')!==FALSE): echo '<p>You owe fines!</p>'; break; case (strpos($response,'Incorrect password')!==FALSE): echo '<p>Your pin does not match!</p>'; break; case (strpos($response,'Unknown borrower barcode')!==FALSE): echo '<p>Your barcode was not found!</p>'; break; default: echo '<p>We need to make a definition for this message: ' . $response . '</p>'; break; }
Note For Other ILS’s
I want to be very clear that your mileage may vary with this code. This is what works for me, with a Horizon setup. I had a friend look into III’s setup, and he came back from the manuals that a lot of their SIP stuff is integrated with the WebPAC and Express Lane products only. They do offer the Fine Payment Web Service for this type of integration, but that comes at a price (cost is unknown, he didn’t go to his sales rep yet).
Sum Up
I hope this crash course in SIP programming has been beneficial to you. As always, please leave a comment to let me know how this is (or isn’t) working out for you.
Connect To SIP With PHP
Last time, we took a look at how to make sure your web server can see your SIP server. This time, we’re going to carry on with actually making the two talk to each other.
Conceptually, all that needs to happen, is we open a socket connection to SIP. This basically acts as a Telnet session, so we can send a message to the server, and it will reply back with a message. For this reason, we need to use a sleep command, to wait for the server to reply. Depending on your network setup, you may want to experiment with different sleep times to see how short you can go.
Basic Code
<?php $sip_server_address = 'MyServerName'; $sip_server_port = 2023; $fp = fsockopen($sip_server_address,$sip_server_port,$errno,$errstr); stream_set_blocking($fp, 0); $check = 'Our_SIP_String_Will_Go_Here'; fputs($fp, "$check\r"); sleep(2); $response = fread($fp, 256); echo $response; ?>
By taking the above code, and swapping in your server’s name (or IP address will work as well), and port number you should receive back a response of 96. As stated before code 96 is SIP’s way of saying it doesn’t understand what you’re asking for. If you get that, you are connected to SIP, and everything is working as it should.
Troubleshooting
If your page is timing out, you likely have a problem with your server name or port. Double check the port, and in a pinch try connecting via IP address. I actually have my server name defined in a hosts file on my server. This may be overkill, but it may also be just the tip you need to get things working. Another idea is to check that you still have a connection to SIP, to rule out network issues.
Next time, we’ll take a look at how to create a real message to SIP, so that we can receive account information back. If you have any success or failures with connection, please let me know by using the comments section.
How To Connect To SIP
This post is the start of a series. I’m going to be releasing all of the bits and pieces that make up the online payments system at my library. It should be a good model for most libraries to follow, you can use nearly any payment gateway, and all you need is SIP and PHP.
The first step is going to be to make sure our web server can actually connect to our SIP server. This will save you lot’s of time thinking your PHP is broken, when it’s really a port open/closed issue.
First, login to your web server, the one that will be connecting to SIP through PHP. Open up a command prompt and enter the command ‘telnet sip.mydomain.com 666’ where sip.mydomain.com is the address to your SIP server, and 666 is the port number you wish to connect into.
After you press enter, your screen should go totally blank. It will look really weird, but not to worry, this is normal. You’ve now started a telnet connection to your SIP server. You can type in anything you want (it won’t show up, this is something called localecho), and when you hit enter you should get a 96 as a response. The 96 response means that SIP didn’t understand the command. This does verify that you are connected into the server, however.
If you receive another message, like the one in this screen shot, then your webserver cannot see your SIP server. You’ll have to make the changes to the firewall to open the port.
Now we know that your webserver can actually connect to your SIP server. In the next post in this series, we’ll actually make the connection through SIP, and get some real data from SIP.
Hitler Get’s Hacked
Note, NSFW language in the subtitles.
This video has so much truth behind it, that I wanted to pass it along to other developers considering certain frameworks on the market.
It’s also hilarious.
Acknowledgement: I got this by way of a colleague’s blog, who got it from the Drupal site.
Enjoy!
Aggregating ISBNs From Horizon
I’m working on creating a spreadsheet that will be imported into another application. The format for the spreadsheet is fairly picky, as it expects to match titles, authors, ISBNs, etc. The problem is that it wants each bib to be a new line.
Sounds simple, right? Well, when you look beneath the hood (this is a Horizon ILS using a SQL Server backend), each ISBN (and author, and UPC, etc) is a separate line in the database. This is great from a normalization standpoint, but despite my many years of writing SQL queries for SQL Server, this never ceases to amaze me how much of a pain in the butt it can be.
MySQL has an awesome GROUP_CONCAT function that handles this perfectly. MSSQL does not.
Here’s a bit of code I used to make this work. You can easily modify it to pull off of any MARC field. My actual implementation uses the 592 note field to look for a tag that flags the title as being an upcoming bestseller for the next season, but for demonstration purposes, I made a couple examples that should be easier to follow.
592 Note Field
SELECT bib, LEFT(isbn_list, LEN(isbn_list)-1) AS isbn_list FROM ( SELECT bib, ( SELECT isbn.processed + ';' AS [text()] FROM isbn WHERE isbn.bib# = bibs_to_use.bib FOR XML PATH('') ) as isbn_list FROM ( SELECT b.bib# bib FROM bib b WHERE b.tag='592' AND b.text LIKE '%New and Forthcoming Fiction Winter 2011%' ) as bibs_to_use ) as outter_query;
082 Call Number
SELECT bib, LEFT(isbn_list, LEN(isbn_list)-1) AS isbn_list FROM ( SELECT bib, ( SELECT isbn.processed + ';' AS [text()] FROM isbn WHERE isbn.bib# = bibs_to_use.bib FOR XML PATH('') ) as isbn_list FROM ( SELECT b.bib# bib FROM bib b WHERE b.tag='082' AND b.text LIKE '%005%' ) as bibs_to_use ) as outter_query;
260 Publisher
SELECT bib, LEFT(isbn_list, LEN(isbn_list)-1) AS isbn_list FROM ( SELECT bib, ( SELECT isbn.processed + ';' AS [text()] FROM isbn WHERE isbn.bib# = bibs_to_use.bib FOR XML PATH('') ) as isbn_list FROM ( SELECT b.bib# bib FROM bib b WHERE b.tag='260' AND b.text LIKE '%Little, Brown and Co%' ) as bibs_to_use ) as outter_query;
To be fair, I used an old example from a blog called Rational Relational and then converted that over to our needs in Horizon.
Update – 21 Jan 11
Here’s a little update that adds in a similar method for authors. It is very common to need to list authors in a comma seperated list, and it was a little tricky for me, so I’d assume it could be very tough for some readers.
SELECT bib, LEFT(isbn_list, LEN(isbn_list)-1) AS isbn_list,LEFT(author_list, LEN(author_list)-1) AS author_list FROM ( SELECT bib, ( SELECT isbn.processed + ';' AS [text()] FROM isbn WHERE isbn.bib# = bibs_to_use.bib FOR XML PATH('') ) as isbn_list, ( SELECT a.processed + ';' AS [text()] FROM bib_auth ba, author a WHERE ba.bib# = bibs_to_use.bib AND a.auth# = ba.auth# FOR XML PATH('') ) as author_list FROM ( SELECT b.bib# bib FROM bib b WHERE b.tag='260' AND b.text LIKE '%Little, Brown and Co%' ) as bibs_to_use ) as outter_query;
Get Jacket Covers From BC API
Jan 3, 2011 BiblioCommons, Code, Library
Here is a little function for getting jacket covers from the BiblioCommons API.
I keep a folder with functions like this, so that I’m never re-writing code to retreive data from the API. It’s also handy in the event that something changes with the API, be it the data returned, or the format of the call.
The Code
function getJacket($bib_id, $min_width = 150) { $timeout = 3; $bib_id = trim(mysql_escape_string($bib_id)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://my_library.bibliocommons.com/api/ContentService/imageurl/' . urlencode($bib_id) .'/'. urlencode($min_width) .'/150'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $result = curl_exec($ch); curl_close($ch); $xml = simplexml_load_string($result); if($xml) { return $xml; } else { return 'images/book.jpg'; } }
Things To Remember
Remember to change my_library to your libraries sub-domain on BiblioCommons. If you are using this as an app developer, or a plugin/widget/add-on/module developer, you can swap out that entire line for this (making sure to set, or pass in, a $bc_key variable):
curl_setopt($ch, CURLOPT_URL, 'http://api.bibliocommons.com/api/ContentService/imageurl/' . urlencode($bib_id) .'/'. urlencode($min_width) .'/150?api_key=' . $bc_key);
This function requires that you have cURL installed and working correctly on your server. I use cURL to fetch the XML because then I am able to set a timeout.
You can change the $timeout variable to be whatever you want. I find 3 seconds is reasonable for my uses (including the front page of my library’s website).
The $bib_id variable is expecting a BiblioCommons item ID. This item ID can be made by appending your BiblioCommons library ID to the end of a Horizon item ID. So, my library ID is 001. I can make an item ID for Horizon item ID X as X001. Mileage may vary for non Horizon libraries.
Depending on the circumstance, an easier way to get an item ID is to pull it right from its URL in the catalog. Here is the URL to a title: http://opl.bibliocommons.com/item/show/612436001_harry_potter_and_the_deathly_hallows. The BC item ID is 612436001.
How It Works
This function calls the API imageurl function, passing in a few variables from our getJacket function call. We pass in the bib ID, and the width that we want the jackets set to.
The API call is fetched through cURL. If we don’t receive the results before our timeout, we will return a generic cover called book.jpg (feel free to change this line to match your path). The API may be slow, it may be down, your web server may be having issues with connection, DNS, or any other number of problems. For this reason, we set a timeout.
When I originally created this function, I had no timeout (and no cURL dependency). Every time we had an issue, this function would crash. This would cause the entire right column of our homepage, and countless other pages site wide, to not load. It would also cause the footer to not show up, as anything after this call would not load.
If the XML is returned, and loaded properly, we will return the URL that we receive. This may be for Amazon or Syndetics depending on your library’s setup. In the future, who knows where else these could come from (IMDB? LibraryThing?). We let the API handle that, and just interface with the API.
Calling the Function
Here’s two examples of how to call this function, both yielding the same results.
include('bc-api/get-item-jacket-function.php'); echo '<img alt="Books Title Here" src="'; echo getJacket($bib_id); echo '">';
include('bc-api/get-item-jacket-function.php'); $jacket_url = getJacket($bib_id); echo '<img alt="Books Title Here" src="' . $jacket_url . '">';
Edit/Update
After posting this, I received an email from one of the main developers at BiblioCommons (maybe the main developer, not sure your title Marty!). It turns out that this portion of the API won’t likely be available for your library. It will not be available in the “new” API, for select developers. For backwards compatibility, a few libraries will still have access to it, but it may not be available forever. Forward looking, any new libraries coming online with the service will not have access to this call.
I want this to be very clear for everyone that they won’t likely have access for it, I don’t want to mislead anyone. I will leave this post up, however, for those of us who do have it.