Convio Web Services in Python and PHP

Expanding on my last post about being able to successfully connect to Convio Web Services in three lines, I’d like to get a little more in depth and actually construct the necessary SOAP headers and make a simple call. I’ve written a small PHP implementation as well. I was able to make a successful log in to CWS in just 2 lines of PHP code!

Convio Logo

Now, as far as CWS goes, Python and PHP may not instantly be an obvious choice as far as languages go, mainly because they are both largely scripting languages. CWS is mainly for robust data syncing operations and those tasks generally go to languages like Java or C#, however, I’m a high level language guy, so I go with what I know. Also, Python and PHP both make SOAP exceedingly easy to work with, so there’s no real reason why you can’t, with a bit of discipline, write a perfectly excellent and robust data sync operation in either of these languages … especially Python.

So, here are my examples. They are exceedingly simple and easy once again. The Python example leverages suds once again and the PHP example is using PHP’s pretty impressive built in SOAP library. Each example shows you how to create the SOAP client using the Convio WSDL, make a successful log in to CWS, create a valid session header for subsequent calls, and how to make a call that grabs the number of constituents that have been added to the Convio database.

If you find either of these examples helpful, please let me know! Keep in mind these are just functional examples. There is no proper error handling or any sort of architecture here; they are just some bare bones examples on how to get data from CWS.

Python

from suds.client import Client
 
# Load the WSDL                                                                                                                                                                                                                                
client = Client('https://secureX.convio.net/1.0/CLIENT/wsdl')
 
# Log in to CWS                                                                                                                                                                                                                                
login = client.service.Login('apiadmin', 'password')
 
# Set the session header                                                                                                                                                                                                                       
session = client.factory.create('Session')
session.SessionId = login.SessionId
client.set_options(soapheaders=session)
 
# Get the number of constituents added since last sync                                                                                                                                                                                         
data = dict(PartitionId=1001, RecordType='Constituent', PageSize=1)
response = client.service.GetIncrementalInsertsCount(**data);
 
# Show the number of constituents added                                                                                                                                                                                                        
print '%d constituents have been added since the last sync.' % (int(response.RecordCount))

PHP

// Load the WSDL                                                                                                                                                                                                                               
$client = new SoapClient('https://secureX.convio.net/1.0/CLIENT/wsdl');
 
// Log in to CWS                                                                                                                                                                                                                               
$login = $client->Login(array('UserName'=>'apiadmin', 'Password'=>'password'));
 
// Set the session header                                                                                                                                                                                                                      
$headerBody = array('SessionId'=>$login->Result->SessionId);
$header     = new SoapHeader('urn:soap.convio.com', 'Session', $headerBody);
$client->__setSoapHeaders($header);
 
// Get the number of constituents added since last sync                                                                                                                                                                                        
$data = array
(
    'PartitionId' => 1001,
    'RecordType'  => 'Constituent',
    'PageSize'    => 1
);
$response = $client->GetIncrementalInsertsCount($data);
 
// Show the number of constituents added                                                                                                                                                                                                       
print(sprintf("%d constituents have been added since the last sync\n", (int) $response->Result->RecordCount));

Log In to Convio Web Services with three lines of Python

I was on the look out for a good SOAP library for Python now that I am the new Convio “API Guy.”

Convio Logo

In my search I discovered this library:

Suds

To test it out I tried making successful connection to Convio Web Services.

1
2
3
from suds.client import Client
client = Client('https://secureX.convio.net/1.0/CLIENT/wsdl')
result = client.service.Login('apiadmin', 'password')

Boom. Straight in, no fuss no muss. Pretty impressive. I’m going to play with Suds and CWS a bit more to see if there is actually a robust Python based syncing application that can be made here. I’m led to believe Suds is not great for creating SOAP servers, but it makes for a very “Pythonic” and easy to understand way to create a SOAP client.

PHP Library For Convio Open API

If any of you out there use Convio for your non-profit’s on-line presence, ie website, donations, email campaigns, I’ve created a PHP library for connecting to their Open API.

Convio Logo

I say it’s a “Library”… it’s really just a single wrapper file that aids in connecting to, making call to, and handling responses from, the Convio Open APIs.

There’s no documentation to speak of, but the two files included in the zip archive are pretty solidly commented, so you will probably be able to get a lot from them. The two files included in the zip are the library file itself and an example of how to implement it.

If you end up using it, let me know what you think!

Download the PHP Library for Convio Open API

JProQuiz: Javascript Quiz Software

A couple of weeks ago a friend asked me if I’d be interested doing a little contract work for the company he works for. The project was to come up with an on line quiz, mimicking that of FreeRice.com. Reasonably simple I thought, so I figured it might be good for a laugh and few pennies. The caveat being, if possible, for it to be entirely browser-side, as his company’s software did not support server-side code. Now that sounded interesting.

Being the impulsive code-monkey that I am, instead of waiting for the finalization of my contract to come through, I simply went ahead and coded the whole thing. Naturally, they decided to give this to someone internal and not hire me, so their loss is your gain. I didn’t have anything to do with this software after I’d written it, so I figured I would publish it here on my blog for the whole world to use.

The software itself is incredibly simple. Actually, I hesitate to call it software at all. It’s just one Javascript class file, some specifically name HTML elements and a single Javascript call to start the whole thing up. I like the Prototype framework, so I decided to use that to power JProQuiz, hence the “Pro” part. I also used the Scriptaculous builder so I wouldn’t have too much messy HTML in the class file. The entire class file (quiz.js) is less than 300 lines long and the majority of those are comments. (Can you tell I’m a little proud of it?)

The basic idea is a single class that acts as an engine and using a JSON configuration file, which contains all the quiz questions, options, and correct answers, drives the entire quiz experience for the user. Now, you’re probably wondering: “How do you keep the JSON config file obfuscated so that people can’t just look at the answers?” Err… well… you can’t. This quiz is entirely browser-side, so the data has to be available to the front end. The JSON file is sort of buried in the class file, so it’s definitely not immediately obvious where the questions are coming from. It would take someone who knew their way around Javascript to figure out what was going on. And if someone really wants to dig through your code and find your JSON file and read all the answers so they can get a 100% on your little web quiz, well, boogaloo for them. So, don’t use JProQuiz to power the new on line SATs, just use it for fun little quizzes.

You can download JProQuiz here.

And you can see a live example here.

If you use JProQuiz please let me know by commenting below. Or if you have any questions, suggestions, or bugs . . . bring ‘em on!

Last.FM PHP Image Screen Scraper

I’m pretty avid user of Last.FM. I’m not big on this social networking fad, even if I do work in the industry, but I’ve found Last.FM very useful, mainly for finding new music. I’m currently building a web-based media manger in PHP and wanted a way to automatically get images for the artists.

I originally used the discogs.com API and although it was pretty easy to use and reasonably comprehensive, I was really disappointed in the quality of the images, not to mention I was constantly getting back images of electronic and ambient artists who had names similar to the rock bands in my collection, rather than the rock band themselves. I’m sorry, but when make a call to get images for “Cream” I don’t expect to get back some 14-year-old kid with a turntable and a DAT machine; I want freaking Clapton, Bruce, and Baker.

So, discogs.com left me a bit cold. But I soon learned about Last.FM and was delighted when I discovered their API. The image quality at Last.FM is superb, so I knew I wouldn’t be disappointed there. I started researching their API and realized that they didn’t really give me any info I needed other than the images and I didn’t want to have to sign up for an API key if all I wanted to do was download pictures of bands. Also, they only appear to provide three sizes of images and for many bands they have a whole slew of images available: large, medium, small, smaller, square; I wanted them all, not just the three provided by their API.

So, in the end I decided not to sign up for a Last.FM API key and instead wrote a very simple little PHP screen scraper that would bring back all of the images I wanted. Last.FM actually aided me in my goal by embedding a JSON string with just exactly what I needed right onto the artist page. It worked very well, so I decided to stick with it.

You can download my simple screen scraper script here. The zip file includes the class file and an example file.

It’ an incredibly simple script. Just a basic class file with only one useable method. It’s written so it can be expanded, but as Last.FM’s API is under constant development, it probably won’t be. But if you just want a simple script to grab artist images from Last.FM, please download my script and let me know if you like it.