last blog: Writing A Twitter Client Using PHP (1): Consumer Requests Request Token
Here is the step B Service Provider Grants Request Token.
At the last blog, I got url, something as below:
https://twitter.com/oauth/request_token?oauth_callback=http%3A%2F%2Flocalhost%2Fitwitter%2F&oauth_consumer_key=fwwaw3m5sQq4L3M6aXV1jg
&oauth_nonce=817d10a677547378dbef3547c5545fdb&oauth_signature=Hv48dvKEbWAn4tG4JSEs0UbM1so%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1295839203&oauth_version=1.0a
Here curl is used. The function is as below:
function http( $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,true);
curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)”);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($ch, CURLOPT_COOKIEJAR, ‘cookie.txt’);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
Then use echo http($url);
Then I gets the string like below from the Twitter server:
oauth_token=sNwJ4Km398PwLdOOjxBcER7KQ2U8P9uPocLblUaAY
&oauth_token_secret=SMVXvhsBWX9qbwbZNsukn5qc2Ex3vLUV405sQyrrU&oauth_callback_confirmed=true
Then turn this string to an Array and put in a variable:
$oauth_responses = http($url);
$oauth_responses = split(‘&’, $oauth_responses);
$parsed_parameters = array();
foreach ($oauth_responses as $oauth_response) {
$split = split(‘=’, $oauth_response, 2);
$parameter = $split[0];
$value = $split[1];
if (isset($parsed_parameters[$parameter])) {
if (is_scalar($parsed_parameters[$parameter])) {
$parsed_parameters[$parameter] = array($parsed_parameters[$parameter]);
}
$parsed_parameters[$parameter][] = $value;
} else {
$parsed_parameters[$parameter] = $value;
}
}
The value of $parsed_parameters is something like below:
Array ( [oauth_token] => sNwJ4Km398PwLdOOjxBcER7KQ2U8P9uPocLblUaAY [oauth_token_secret] => SMVXvhsBWX9qbwbZNsukn5qc2Ex3vLUV405sQyrrU [oauth_callback_confirmed] => true )
Then get the value of oauth_token, and add it behind the request. The code is as below:
$oauth_token = $parsed_parameters[‘oauth_token’];
$url2 = “http://api.twitter.com/oauth/authorize?oauth_token=” . $oauth_token;
This is the step C in the diagram above.