Created
May 12, 2010 18:20
-
-
Save nataliepo/398926 to your computer and use it in GitHub Desktop.
PHP authenticated comment snippets
This file contains 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 | |
/* These are excerpts from the http://github.com/nataliepo/Claire/tree/master/tp-libraries codebase. */ | |
/** mostly defined in tp-libraries/tp-config.php **/ | |
define ('CONSUMER_KEY', '67738f8572da988f'); | |
define ("ROOT_TYPEPAD_API_URL", "http://api.typepad.com"); | |
define ("ROOT_TYPEPAD_AUTH_API_URL", "https://api.typepad.com"); | |
/** in tp-libraries/tp-comment.php **/ | |
/** This method will POST a comment from an authenticated user. **/ | |
function post_authenticated_comment ($params) { | |
$typepad_url = get_comments_api_url ($params['post_xid'], 1); | |
$session = $params['session']; | |
$json = '{"content":"' . $params['content'] . '"}'; | |
$response = $session->make_authorized_request($typepad_url, $json); | |
} | |
/** in tp-libraries/tp-utilities.php **/ | |
function get_comments_api_url ($xid, $is_auth=0) { | |
$root = ""; | |
if ($is_auth) { | |
$root = ROOT_TYPEPAD_AUTH_API_URL; | |
} | |
else { | |
$root = ROOT_TYPEPAD_API_URL; | |
} | |
return $root . '/assets/' . $xid . '/comments.json'; | |
} | |
/** the TP Session object is defined in tp-oauth.php. It's created when a user loads the page because the TPSession PHP object checks to see if the user has a cookie for this site which helps to look up the OAuth token values to be able to sign TypePad Requests on their behalf. **/ | |
function make_authorized_request($url, $params="") { | |
// Make a dummy OAuth Request object so we can use its signed parameters | |
// $oauth = new OAuthRequester($this->get_api_endpoint('oauth-access-token-endpoint'), 'GET'); | |
$oauth = new OAuthRequester($this->get_api_endpoint(TP_OAUTH_ACCESS_TOKEN_URL), 'GET'); | |
// Grab the access secret_token | |
$r = $this->store->getServerToken(CONSUMER_KEY, $this->oauth_token, $this->user_id); | |
// this will croak on a string vs array object if we don't rewrite the current value | |
$signature_methods = array('PLAINTEXT'); | |
$r['signature_methods'] = $signature_methods; | |
$oauth->sign($this->user_id, $r); | |
$parameters = array('timestamp', 'nonce', 'consumer_key', | |
'version', 'signature_method', 'signature'); | |
// Build the Authorization value, starting with the realm. | |
$header_string = 'OAuth realm="api.typepad.com", '; | |
// Then append each value in the $parameters array... | |
foreach ($parameters as $parm) { | |
$header_string .= 'oauth_' . $parm . '="' . $oauth->getParam('oauth_' . $parm) . '", '; | |
} | |
// ...ending with the access token from the DB. | |
$header_string .= 'oauth_token="' . $r['token'] . '"'; | |
// Package up the Authorization value as an array, along with the expected Content-Type | |
$header_array = array('Authorization:' . $header_string, | |
'Content-Type: application/json;'); | |
var_ | |
// debug ("[update_author_record] Making the request at url = $url with Authorization: $header_string"); | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
if ($params) { | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
} | |
$response = claire_json_decode(curl_exec($ch)); | |
curl_close($ch); | |
return $response; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment