001.
<?
002.
session_start();
003.
header(
"Content-Type: text/html; Charset=utf-8"
);
004.
header(
"pragma: no-cache"
);
005.
header(
"Expires: Wed, 31 May 2000 14:59:58 GMT"
);
006.
header(
"Cache-control: no-cache"
);
007.
008.
009.
010.
function
url_rfc3986(
$str
) {
011.
012.
return
str_replace
(
'%7E'
,
'~'
, rawurlencode(
$str
));
013.
}
014.
015.
print
"<pre>"
;
016.
print_r(
$_GET
);
017.
print_r(
$_SESSION
);
018.
print
"</pre>"
;
019.
020.
if
(
$_SERVER
[
'REQUEST_METHOD'
] !=
'POST'
) {
021.
022.
?>
023.
024.
<form method=
"POST"
>
025.
<input type=
"submit"
value=
"送信"
><br>
026.
027.
oauth_token :
028.
<input
029.
type=
"text"
030.
name=
"oauth_token"
031.
value=
"<?= $_GET['oauth_token'] ?>"
032.
size=
"80"
033.
><br>
034.
oauth_verifier :
035.
036.
<input
037.
type=
"text"
038.
name=
"oauth_verifier"
039.
value=
"<?= $_GET['oauth_verifier'] ?>"
040.
size=
"80"
041.
><br>
042.
043.
oauth_token_secret :
044.
<input
045.
type=
"text"
046.
name=
"oauth_token_secret"
047.
value=
"<?= $_SESSION['oauth_token_secret'] ?>"
048.
size=
"80"
049.
><br>
050.
051.
</form>
052.
053.
<?
054.
exit
();
055.
}
056.
057.
058.
059.
060.
061.
062.
063.
064.
$twitter_url
=
'https://api.twitter.com/oauth/access_token'
;
065.
066.
067.
068.
069.
$oauth_consumer_key
=
"Consumer key"
;
070.
$oauth_consumer_secret
=
"Consumer secret"
;
071.
$oauth_token
=
$_POST
[
'oauth_token'
];
072.
$oauth_secret
=
$_POST
[
'oauth_token_secret'
];
073.
074.
075.
$mt
= microtime();
076.
$rand
= mt_rand();
077.
$oauth_nonce
= md5(
$mt
.
$rand
);
078.
079.
$oauth_signature_method
=
"HMAC-SHA1"
;
080.
$oauth_timestamp
=
mktime
();
081.
082.
$oauth_version
=
"1.0"
;
083.
084.
085.
086.
/*
087.
httpMethod +
"&"
+
088.
url_encode( base_uri ) +
"&"
+
089.
sorted_query_params.each { | k, v |
090.
url_encode ( k ) +
"%3D"
+
091.
url_encode ( v )
092.
}.join(
"%26"
)
093.
*/
094.
095.
$base_string
=
"POST"
;
096.
$base_string
.=
"&"
. url_rfc3986(
$twitter_url
);
097.
$base_string
.=
"&"
;
098.
099.
$base_string
.= url_rfc3986(
"oauth_consumer_key"
).
"%3D"
.url_rfc3986(
$oauth_consumer_key
).
"%26"
;
100.
$base_string
.= url_rfc3986(
"oauth_nonce"
).
"%3D"
.url_rfc3986(
$oauth_nonce
).
"%26"
;
101.
$base_string
.= url_rfc3986(
"oauth_signature_method"
).
"%3D"
.url_rfc3986(
$oauth_signature_method
).
"%26"
;
102.
$base_string
.= url_rfc3986(
"oauth_timestamp"
).
"%3D"
.url_rfc3986(
$oauth_timestamp
).
"%26"
;
103.
$base_string
.= url_rfc3986(
"oauth_token"
).
"%3D"
.url_rfc3986(
$oauth_token
).
"%26"
;
104.
$base_string
.= url_rfc3986(
"oauth_verifier"
).
"%3D"
.url_rfc3986(
$_POST
[
'oauth_verifier'
]);
105.
$base_string
.= url_rfc3986(
"oauth_version"
).
"%3D"
.url_rfc3986(
$oauth_version
);
106.
107.
108.
109.
110.
111.
112.
113.
114.
$oauth_signature
=
115.
base64_encode
( hash_hmac(
116.
"sha1"
,
117.
$base_string
,
118.
url_rfc3986(
$oauth_consumer_secret
) .
"&"
. url_rfc3986(
$oauth_secret
),
119.
true
120.
));
121.
122.
123.
124.
125.
$curl
= curl_init();
126.
curl_setopt(
$curl
, CURLOPT_CONNECTTIMEOUT, 30);
127.
curl_setopt(
$curl
, CURLOPT_HEADER, false);
128.
curl_setopt(
$curl
, CURLOPT_RETURNTRANSFER, true);
129.
curl_setopt(
$curl
, CURLOPT_BINARYTRANSFER, true);
130.
curl_setopt(
$curl
, CURLOPT_URL,
$twitter_url
);
131.
curl_setopt(
$curl
, CURLOPT_POST, 1);
132.
curl_setopt(
$curl
, CURLOPT_POSTFIELDS,
""
);
133.
134.
135.
136.
137.
$header
=
array
();
138.
$header
[] =
'Expect:'
;
139.
$header
[] =
'Authorization: OAuth '
.
140.
url_rfc3986(
"oauth_consumer_key"
).
"=\""
.url_rfc3986(
$oauth_consumer_key
).
"\","
.
141.
url_rfc3986(
"oauth_nonce"
).
"=\""
.url_rfc3986(
$oauth_nonce
).
"\","
.
142.
url_rfc3986(
"oauth_signature_method"
).
"=\""
.url_rfc3986(
$oauth_signature_method
).
"\","
.
143.
url_rfc3986(
"oauth_timestamp"
).
"=\""
.url_rfc3986(
$oauth_timestamp
).
"\","
.
144.
url_rfc3986(
"oauth_token"
).
"=\""
.url_rfc3986(
$oauth_token
).
"\","
.
145.
url_rfc3986(
"oauth_verifier"
).
"=\""
.url_rfc3986(
$_POST
[
'oauth_verifier'
]).
"\","
.
146.
url_rfc3986(
"oauth_signature"
).
"=\""
.url_rfc3986(
$oauth_signature
).
"\","
.
147.
url_rfc3986(
"oauth_version"
).
"=\""
.url_rfc3986(
$oauth_version
).
"\""
;
148.
149.
curl_setopt(
$curl
, CURLOPT_HTTPHEADER,
$header
);
150.
151.
152.
153.
154.
curl_setopt(
$curl
, CURLOPT_SSL_VERIFYPEER, false);
155.
curl_setopt(
$curl
, CURLOPT_SSL_VERIFYHOST, 1);
156.
157.
158.
159.
160.
$result
= curl_exec(
$curl
);
161.
162.
163.
164.
165.
print
"<br>"
;
166.
167.
if
(
$result
=== false) {
168.
echo
'Curl error: '
. curl_error(
$curl
);
169.
}
170.
else
{
171.
echo
'Operation completed without any errors'
;
172.
}
173.
curl_close(
$curl
);
174.
175.
print
"<br>"
;
176.
177.
parse_str
(
$result
,
$arr
);
178.
print
"<pre>"
;
179.
print_r(
$arr
);
180.
print
"</pre>"
;
181.
182.
?>