Skip to content

Instantly share code, notes, and snippets.

@soh335
Created August 20, 2009 15:09
Show Gist options
  • Save soh335/171111 to your computer and use it in GitHub Desktop.
Save soh335/171111 to your computer and use it in GitHub Desktop.
<?php
$test_server = new TestOAuthServer(new MockOAuthDataStore());
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
$test_server->add_signature_method($hmac_method);
$sig_methods = $test_server->get_signature_methods();
$sig_method = $sig_methods['HMAC-SHA1'];
#https://twitter.com/oauth/request_tokenを叩く時
$test_consumer = new OAuthConsumer("oauth_consumer_key", "oauth_consumer_secret", NULL);
$req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", "https://twitter.com/oauth/request_token");
$req_req->sign_request($sig_method, $test_consumer, NULL);
$res = file_get_contents($req_req->to_url());
#https://twitter.com/oauth/access_tokenを叩く時
$test_consumer = new OAuthConsumer("oauth_consumer_key", "oauth_consumer_secret", NULL);
$req_token = new OAuthConsumer("oauth_token", "oauth_token_secret");#oauth_token, oauth_token_secretはrequest_tokenで取得したもの
$acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $req_token, "GET", "https://twitter.com/oauth/access_token");
$acc_req->sign_request($sig_method, $test_consumer, $req_token);
$res = file_get_contents($acc_req->to_url());
#updateのapiを叩く時
$test_consumer = new OAuthConsumer("oauth_consumer_key", "oauth_consumer_secret", NULL);
$tokner = new OAuthConsumer("oauth_token", "oauth_token_secret", 1);#oauth_token, oauth_token_secretはaccess_tokenで取得したもの
$access = OAuthRequest::from_consumer_and_token($test_consumer, $tokner, "POST", "http://twitter.com/statuses/update.xml", array('status' => "test"));
$access->sign_request($sig_method, $test_consumer, $tokner);
$ch = curl_init();
curl_setopt ($ch,CURLOPT_URL,$oauth_request->get_normalized_http_url());
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $oauth_request->to_postdata());
curl_exec($ch);
curl_close ($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment