Context.IO PHP Library Now Available on GitHub.com

We’ve just released the PHP library for Context.IO on GitHub.com. Check it out: https://github.com/contextio/PHP-ContextIO.

Here are some simple examples of how to play with the API using PHP. To get started, you’ll first need to get your Context.IO API keys and add at least one mailbox to your key. Adding mailboxes can be done from the developer console or through the /imap/addaccount.json API call.

The first example shows you how to pull the 20 most recent emails sent to “bill@widgets.com” and print the subject line.

include_once("class.contextio.php");
$mailboxToQuery = "jim@acme.com";
$contextIO = new ContextIO(CONTEXTIO_CONSUMER_KEY, CONTEXTIO_CONSUMER_SECRET);
$r = $contextIO->contactMessages($mailboxToQuery, array('to' => 'bill@widgets.com', 'limit' => 20));
foreach ($r->getData() as $message) {
	echo "Subject: ".$message['subject']."\n";
}

Here’s how you can download all versions of the last 2 attachments exchanged with “bill@widgets.com”:

include_once("class.contextio.php");
$mailboxToQuery = "jim@acme.com";
$contextIO = new ContextIO(CONTEXTIO_CONSUMER_KEY, CONTEXTIO_CONSUMER_SECRET);
$r = $contextIO->contactFiles($mailboxToQuery, array('email' => 'bill@widgets.com', 'limit' => 2));
foreach ($r->getData() as $document) {
	foreach ($document['occurrences'] as $attachment) {
		$contextIO->downloadFile(
			$mailboxToQuery,
			array('fileId' => $attachment['fileId']),
			"/path/to/destination/dir/". $attachment['fileName']
		);
	}
}

Want to download any attachment with a filename containing the word “proposal”? Easy:

include_once("class.contextio.php");
$mailboxToQuery = "jim@acme.com";
$contextIO = new ContextIO(CONTEXTIO_CONSUMER_KEY, CONTEXTIO_CONSUMER_SECRET);
$r = $contextIO->fileSearch($mailboxToQuery, array('filename' => 'proposal'));
foreach ($r->getData() as $attachment) {
	$contextIO->downloadFile(
		$mailboxToQuery,
		array('fileId'=>$attachment['fileId']),
		"/path/to/destination/dir/" . $attachment['fileName']
	);
}

So what are you waiting for? Check out our full documentation at http://developer.context.io and get started right away!

Need access to the console? Just follow and tweet us @ContextIO and we’ll hook you up with an access code.

Trackbacks