Created
July 12, 2010 18:21
-
-
Save mheadd/472849 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Some constants we will use to interact with the Twitter API. | |
define('FORMAT', '.json'); | |
define('TWITTER_API_ENDPOINT', 'http://api.twitter.com/1/statuses/show/'); | |
// Some constants we will use to save documents in CouchDB. | |
define('COUCHDB_HOST', 'http://127.0.0.1'); | |
define('COUCHDB_PORT', '5984'); | |
define('COUCHDB_DB_NAME', 'twittertest'); | |
// The ID of the Twitter status we want to fetch. | |
$status_id = $_REQUEST['status_id']; | |
// Function to get a public Twitter status by ID. | |
function getStatus($twitterUrl) { | |
$ch = curl_init($twitterUrl); | |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); | |
$status = curl_exec($ch); | |
curl_close($ch); | |
return $status; | |
} | |
// Function to save a Twitter status object to CouchDB. | |
function storeDocument($couchURL, $document) { | |
$ch = curl_init($couchURL); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: '.strlen($document))); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $document); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
return $response; | |
} | |
// Get the status update JSON from the Twitter API. | |
$status = json_decode(getStatus(TWITTER_API_ENDPOINT.$status_id.FORMAT)); | |
// Build the URL to save the status update to CouchDB. | |
$couchURL = COUCHDB_HOST.':'.COUCHDB_PORT.'/'.COUCHDB_DB_NAME.'/'.$status->id; | |
// Store the status update in CouchDB and print out the result. | |
echo storeDocument($couchURL, json_encode($status)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment