//hang up //print_r($response); //exit; return $response; } //end sendRequest() //get request token $baseURI = 'https://api.twitter.com/oauth/request_token'; $nonce = time(); $timestamp = time(); $oauth = array('oauth_callback' => '', 'oauth_consumer_key' => '204uQ3bn1qIKWIGY0Krdw', 'oauth_nonce' => $nonce, 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => $timestamp, 'oauth_version' => '1.0'); $consumerSecret = 'WZjDcjXp2jdSzEKMZkDIQJVGOhNwnQDGn483t7OvI'; //put your actual consumer secret here, it will look something like 'MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98' $baseString = buildBaseString($baseURI, $oauth); //build the base string $compositeKey = getCompositeKey($consumerSecret, null); //first request, no request token yet $oauth_signature = base64_encode(hash_hmac('sha1', $baseString, $compositeKey, true)); //sign the base string $oauth['oauth_signature'] = $oauth_signature; //add the signature to our oauth array /*echo "<pre>"; print_r($oauth); echo "</pre>"; echo "<pre>"; print_r($baseURI); echo "</pre>";*/ $response = sendRequest($oauth, $baseURI); //make the call //parse response into associative array
public function generate_twitter_oauth_token() { function buildBaseString($baseURI, $params) { $r = array(); ksort($params); foreach ($params as $key => $value) { $r[] = "{$key}=" . rawurlencode($value); } return "POST&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r)); } function getCompositeKey($consumerSecret, $requestToken) { return rawurlencode($consumerSecret) . '&' . rawurlencode($requestToken); } function buildAuthorizationHeader($oauth) { $r = 'Authorization: OAuth '; $values = array(); foreach ($oauth as $key => $value) { $values[] = "{$key}=\"" . rawurlencode($value) . "\""; } //encode key=value string $r .= implode(', ', $values); return $r; } function sendRequest($oauth, $baseURI) { $header = array(buildAuthorizationHeader($oauth), 'Expect:'); $options = array(CURLOPT_HTTPHEADER => $header, CURLOPT_HEADER => false, CURLOPT_URL => $baseURI, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false); $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); curl_close($ch); var_dump($response); exit; return $response; } $baseURI = 'https://api.twitter.com/oauth/request_token'; $nonce = time(); $timestamp = time(); // $oauth_callback = "http://merlinleads.net/demo/twitter/callback"; $oauth_callback = "http://127.0.0.1/merlinleads/twitter/callback"; $oauth = array('oauth_callback' => $oauth_callback, 'oauth_consumer_key' => TWITTER_KEY, 'oauth_nonce' => $nonce, 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => $timestamp, 'oauth_version' => '1.0'); $consumerSecret = TWITTER_SECRET_KEY; $baseString = buildBaseString($baseURI, $oauth); $compositeKey = getCompositeKey($consumerSecret, null); $oauth_signature = base64_encode(hash_hmac('sha1', $baseString, $compositeKey, true)); $oauth['oauth_signature'] = $oauth_signature; $response = sendRequest($oauth, $baseURI); if (!$response) { return null; } $responseArray = array(); $parts = explode('&', $response); foreach ($parts as $p) { $p = explode('=', $p); $responseArray[$p[0]] = $p[1]; } $oauth_token = $responseArray['oauth_token']; $this->session->set_userdata('twitter_oauth_token', $oauth_token); return $oauth_token; }