public function __construct($consumer_key, $consumer_secret, $signature_method = OAUTH_SIG_METHOD_HMACSHA1, $auth_type = 0) { if (!class_exists('OAuth')) { return ar_error::raiseError('OAuth PECL extension not installed', ar_exceptions::CONFIGURATION_ERROR); } $oauth = new OAuth($consumer_key, $consumer_secret, $signature_method, $auth_type); $oauth->setRequestEngine(OAUTH_REQENGINE_STREAMS); parent::__construct($oauth); }
function twitter_post($auth, $text, $short_url, $image = false) { global $globals; if (empty($auth['twitter_token']) || empty($auth['twitter_token_secret']) || empty($auth['twitter_consumer_key']) || empty($auth['twitter_consumer_secret'])) { return false; } if (!class_exists("OAuth")) { syslog(LOG_NOTICE, "Meneame: pecl/oauth is not installed"); return; } if (!$auth['twitter_consumer_key'] || !$auth['twitter_consumer_secret'] || !$auth['twitter_token'] || !$auth['twitter_token_secret']) { syslog(LOG_NOTICE, "Meneame: consumer_key, consumer_secret, token, or token_secret not defined"); return; } $req_url = 'https://api.twitter.com/oauth/request_token'; $acc_url = 'https://api.twitter.com/oauth/access_token'; $authurl = 'https://api.twitter.com/oauth/authorize'; $api_url = 'https://api.twitter.com/1.1/statuses/update.json'; $api_media_url = 'https://api.twitter.com/1.1/statuses/update_with_media.json'; $api_args = array("empty_param" => NULL); $maxlen = 140 - 24; // minus the url length if ($image) { $maxlen -= 24; echo "Adding image: {$image}\n"; $api_args['@media[]'] = '@' . $image; $url = $api_media_url; } else { $url = $api_url; } $msg = mb_substr(text_to_summary(html_entity_decode($text), $maxlen), 0, $maxlen); $msg_full = $msg . ' ' . $short_url; $api_args["status"] = $msg_full; $oauth = new OAuth($auth['twitter_consumer_key'], $auth['twitter_consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI); $oauth->debug = 1; $oauth->setRequestEngine(OAUTH_REQENGINE_CURL); // For posting images $oauth->setToken($auth['twitter_token'], $auth['twitter_token_secret']); try { $oauth->fetch($url, $api_args, OAUTH_HTTP_METHOD_POST, array("User-Agent" => "pecl/oauth")); } catch (Exception $e) { syslog(LOG_INFO, 'Menéame, Twitter caught exception: ' . $e->getMessage() . " in " . basename(__FILE__) . "\n"); echo "Twitter post failed: {$msg} " . mb_strlen($msg) . "\n"; return false; } // $response_info = $oauth->getLastResponseInfo(); // echo $oauth->getLastResponse() . "\n"; return true; }
private static function makeRequestAndPrintResponse($method, $params, $signature_method = OAUTH_SIG_METHOD_HMACSHA1) { $oauth = new OAuth(Settings::$USOSAPI_CONSUMER_KEY, Settings::$USOSAPI_CONSUMER_SECRET, $signature_method, OAUTH_AUTH_TYPE_URI); if ($signature_method == OAUTH_SIG_METHOD_PLAINTEXT) { $oauth->setRequestEngine(OAUTH_REQENGINE_CURL); } if (Settings::$DEBUG) { $oauth->enableDebug(); } $url = Settings::$USOSAPI_BASE_URL . $method; try { $oauth->fetch($url, $params, OAUTH_HTTP_METHOD_POST); } catch (OAuthException $E) { /* Ignored on purpose. $response_info will be filled either way. */ } $response_info = $oauth->getLastResponseInfo(); header("HTTP/1.0 {$response_info["http_code"]}"); header("Content-Type: {$response_info["content_type"]}"); print $oauth->getLastResponse(); }
/** * @see OAuthHanlder::GetSignedRequestParameters() */ public function GetSignedRequestParameters($credentials, $url, $method = NULL) { if (empty($method)) { $method = 'POST'; } $params = array(); $params['oauth_consumer_key'] = $credentials['oauth_consumer_key']; $params['oauth_token'] = $credentials['oauth_token']; $params['oauth_signature_method'] = 'HMAC-SHA1'; $params['oauth_timestamp'] = time(); $params['oauth_nonce'] = uniqid(); $params['oauth_version'] = '1.0a'; $oauth = new OAuth($credentials['oauth_consumer_key'], $credentials['oauth_consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION); $oauth->setRequestEngine(OAUTH_REQENGINE_CURL); $oauth->setVersion('1.0a'); $oauth->setToken($credentials['oauth_token'], $credentials['oauth_token_secret']); $oauth->setTimestamp($params['oauth_timestamp']); $oauth->setNonce($params['oauth_nonce']); $oauth->setVersion($params['oauth_version']); $signature = $oauth->generateSignature(self::$OAUTH_METHOD_ENUMS[$method], $url); $params['oauth_signature'] = $signature; return $params; }
<?php require 'server.inc'; $x = new OAuth('conskey', 'conssecret', OAUTH_SIG_METHOD_PLAINTEXT); $x->setRequestEngine(OAUTH_REQENGINE_STREAMS); $x->setTimestamp(12345); $x->setNonce('testing'); $port = random_free_port(); $pid = http_server("tcp://127.0.0.1:{$port}", array("HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 40\r\n\r\noauth_token=1234&oauth_token_secret=4567"), $output); $x->setAuthType(OAUTH_AUTH_TYPE_URI); $x->setToken("key", "secret"); var_dump($x->getAccessToken("http://127.0.0.1:{$port}/test")); fseek($output, 0, SEEK_SET); var_dump(stream_get_contents($output)); http_server_kill($pid);
/** * Constructs a new OAuth client object. * @param array $credentials the credentials to use * @param string $authorizationType the authorization type to use * @return OAuth a new OAuth client */ private function GetClient($credentials, $authorizationType = NULL) { $client = new OAuth($credentials['oauth_consumer_key'], $credentials['oauth_consumer_secret'], OAUTH_SIG_METHOD_HMACSHA1, $authorizationType); $client->setRequestEngine(OAUTH_REQENGINE_CURL); $client->setVersion('1.0a'); if (isset($credentials['oauth_token']) && isset($credentials['oauth_token_secret'])) { $client->setToken($credentials['oauth_token'], $credentials['oauth_token_secret']); } // SSL settings. if (defined('SSL_VERIFY_PEER') && SSL_VERIFY_PEER) { $client->setSSLChecks(OAUTH_SSLCHECK_PEER); } else { $client->setSSLChecks(OAUTH_SSLCHECK_NONE); } if (defined('SSL_VERIFY_HOST') && SSL_VERIFY_HOST) { if ($client->sslChecks == OAUTH_SSLCHECK_PEER) { $client->setSSLChecks(OAUTH_SSLCHECK_BOTH); } else { $client->setSSLChecks(OAUTH_SSLCHECK_HOST); } } if (defined('SSL_CA_PATH') && SSL_CA_PATH != '') { // The second parameter must be explicitly set to NULL due to a bug in // version 1.2.2 and earlier. See https://bugs.php.net/bug.php?id=60226 $client->setCAPath(SSL_CA_PATH, NULL); } if (defined('SSL_CA_FILE') && SSL_CA_FILE != '') { $client->setCAPath(NULL, SSL_CA_FILE); } return $client; }
<?php include "constants.php"; try { $oauth = new OAuth(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI); /* Uncomment the line below to get lots of debug */ $oauth->enableDebug(); /* multipart only works with the cURL engine at the moment */ $oauth->setRequestEngine(OAUTH_REQENGINE_CURL); $request_token_info = $oauth->getRequestToken(TWITTER_REQUEST_TOKEN_URL); printf("I think I got a valid request token, navigate your www client to:\n\n%s?oauth_token=%s\n\nOnce you finish authorizing, hit ENTER or INTERRUPT to exit\n\n", TWITTER_AUTHORIZE_URL, $request_token_info["oauth_token"]); $in = fopen("php://stdin", "r"); fgets($in, 255); printf("Grabbing an access token...\n"); /* grab the access token, which is your persistent token which you use for future requests */ $oauth->setToken($request_token_info["oauth_token"], $request_token_info["oauth_token_secret"]); $access_token_info = $oauth->getAccessToken(TWITTER_ACCESS_TOKEN_URL); printf("Access token: %s\n", $access_token_info["oauth_token"]); printf("Access token secret: %s\n", $access_token_info["oauth_token_secret"]); $oauth->setToken($access_token_info["oauth_token"], $access_token_info["oauth_token_secret"]); printf("Sending the background image...\n"); $oauth->fetch(TWITTER_UPDATE_PROFILE_BG_API, array("tile" => "true", "@image" => "@" . dirname(__FILE__) . "/php.jpg;filename=php.jpg;type=image/jpg"), OAUTH_HTTP_METHOD_POST); /* from this point on OAuth is over, now handling the JSON response is in order */ $res = json_decode($oauth->getLastResponse()); printf("Twitter background image URL: %s\n", $res->profile_background_image_url); } catch (OAuthException $E) { print_r($E); }