Basecamp: API Examples using PHP and cURL (GET)

I began working on a project today creating a type of work-request form. We use Basecamp for all of our project management, but since it’s a paid subscription and not something that we have control over, any extra functionality must be done elsewhere using Basecamp’s API.

The Project: I work for a web development company, and we have a good number of clients who request little updates/projects/additions on a fairly regular basis. We started running into the occasion more and more that a client would send us an email labeled “URGENT” and expect us to get something done for them within the hour. When you are busy with other things, that is an unreasonable expectation. So, we have since applied “Rush Jobs”. The standard turn-around guarantee is 10 business days. If they want it done in 5 days, or 2 days, they pay more (just like FedEx).

We sent out letters at the beginning of 2008 informing all of our clients of the new policy, however, it didn’t really sink in. So, our next thought (which is what I’m working on now) is to create a work-request form, forcing them to have to manually select the turn-around time, having the prices right there in front of their eyes, every time.

I won’t go into detail about how we’ve decided to architect the interface, and how it all works – but I will say that we are keeping the system up-do-date at all times with company, project, and person data from Basecamp using the available API. I had an very difficult time finding good examples of how to both gather, and post information to and from Basecamp. But, I managed to successfully do both, and I thought I’d share the wealth!

Getting All Projects

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$session = curl_init();
 
$basecamp_url = 'https://YOUR-BASECAMP-URL/';
$username = 'YOUR-BASECAMP-USERNAME';
$password = 'YOUR-BASECAMP-PASSWORD';
 
curl_setopt($session, CURLOPT_URL, $basecamp_url.'projects.xml');
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_HTTPGET, 1); 
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($session,CURLOPT_USERPWD,$username . ":" . $password);
 
if(ereg("^(https)",$request)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
 
$response = curl_exec($session);
curl_close($session);

Getting People By Company ID

You only need to change line 7 of the code above to:

1
curl_setopt($session, CURLOPT_URL, $basecamp_url.'contacts/people/COMPANY-ID');

Getting A Single Project

Again, you only need to change line 7 to:

1
curl_setopt($session, CURLOPT_URL, $basecamp_url.'projects/PROJECT-ID.xml');

As you can see, there’s not much to change to get different results of information. Though Basecamp’s API doesn’t really tell you how to set everything up in PHP very well, it does give you enough, once you’ve got it all setup, to figure out what you need to change line 7 to to give you the results you want.

The results that are returned are pure XML. You can choose to do whatever you want with that information. I personally have been using PHP’s Simple XML. If you echo out the results, you’ll see the XML and see all the available children, otherwise, you can also check out this page on Basecamp’s API for a quick-reference of everything.

Let’s say we used the API to return all of the people from a specific company id (2nd example above) to the variable $response. Here is an example of how to display some of the XML results that we specify using Simple XML:

1
2
3
4
5
6
7
8
 
$people_xml = new SimpleXMLElement($response);
 
foreach ($people_xml->person as $person) {
	echo "<strong>".$person->{'first-name'}." ".$person->{'last-name'}."</strong><br />";
	echo "Email: ".$person->{'email-address'}."<br />";
	echo "ID: ".$person->id."<br /><br />";
}

And that’s all there is to it. It’s pretty straight-forward. I’m currently working on a Basecamp PHP class that will at least handle all the functions that we’ll be using. Maybe once it’s done, I’ll post it for download. If you have any interest in a Basecamp PHP class, please post a comment and let me know – the more interest I get, the more motivated I’ll be to post it when I’m done.

I will be posting another article very soon about how to post information (messages, to-dos, file attachments) to Basecamp using the API. Look for that fairly soon.

7 Comments

JamesNovember 11th, 2008 at 4:58 am

Hi,

Did you ever create a php class for this? I have been looking all over!

Cheers

jerichDecember 22nd, 2008 at 4:20 pm

How do i create a project? is it available in your class?

Josh PrattDecember 22nd, 2008 at 4:22 pm

Unfortunately, creating projects is not something that is built into Basecamp’s API. It’s a frustrating fact that many people dislike – so, make it known to 37 Signal’s if you want it to be a feature!

ThomasJanuary 12th, 2009 at 7:31 am

Hi,
Could you please guide me to retrive the following using basecamp’s API ,
1. To do list item importing
2. Time importing
3. each to do list items to the relevant Basecamp URL for comments on the to do list item

looking forward

JeffJanuary 28th, 2009 at 7:58 am

Would love to see the PHP class.

perkMarch 18th, 2009 at 12:38 am

You can create your own class with the information so generously provided. Thanks !!!!!

dthawkinsSeptember 17th, 2009 at 9:12 pm

Excellent work! Thank you.

to everyone asking for the class; MAKE YOUR OWN!

use this as a starting point and use 37signals’s API docs for your reference.

Leave a comment

Your comment