Posting Messages with PHP
|
|
I have never done this kind of programming before – using APIs, or CURL, etc. I do program with PHP regularly though. I am making a work request form that our clients will fill out and we will get an email. I had the idea though – that it would be great it we could have it post the information as a message in Basecamp. By reading some of the API documentation (which doesn’t make a whole lot of sense to me) and reading through the forums, it looks like it is possible. I would love to have some sort of real/working example that I could modify, but I haven’t been able to find anything of the sort yet. Could anyone point me in the right direction – or post some working code that I could use to post messages? Any help would be greatly appreciated. Thanks. |
|
|
Well – with enough digging around, I’ve found out how to do it. For those of you that would like to do the same thing – just copy the following (replace the [blahblah] info with your info), place it in your file and run it on your server:
<?php
$project_number = "[project number]";
$title = "This is Awesome!";
$body = "Lorem ipsum dolor sit amet, consectetur adipisicing elit...";
$extendedbody = "";
$xml = "
<request>
<post>
<category-id>[category id]</category-id>
<title>$title</title>
<body>$body</body>
<extended-body>$extendedbody</extended-body>
<use-textile>0</use-textile>
<private>1</private>
</post>
</request>";
// If you have enabled HTTPS, use this - if not - take out the "s" and just use http://
$request = "https://[your basecamp url/projects/$project_number/msg/create";
error_reporting(E_ALL);
$session = curl_init();
$username = "[your basecamp username]";
$password = "[your basecamp password]";
curl_setopt($session, CURLOPT_URL, $request); // set url to post to
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($session, CURLOPT_POST, 1);
curl_setopt($session, CURLOPT_POSTFIELDS, $xml);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// provide credentials if they're established at the beginning of the script
if(!empty($username) && !empty($password)) curl_setopt($session,CURLOPT_USERPWD,$username . ":" . $password);
// tell cURL to graciously accept an SSL certificate if presented
if(ereg("^(https)",$request)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
echo $response;
?>
If anyone knows how to implement file uploads with PHP – please let me know! |
|
|
I’ve just written a php class that will let you easily upload files with your messages or comments, also you can create messages, comments and todo items,without or without email notifications The code is available on my blog here: http://blog.intheloftstudios.com/?p=3. Hope it helps! |
