001.
<?
002.
003.
004.
005.
header(
"Content-Type: text/html; Charset=utf-8"
);
006.
header(
"pragma: no-cache"
);
007.
header(
"Expires: Wed, 31 May 2000 14:59:58 GMT"
);
008.
header(
"Cache-control: no-cache"
);
009.
010.
011.
012.
013.
function
url_rfc3986(
$str
) {
014.
015.
return
str_replace
(
'%7E'
,
'~'
, rawurlencode(
$str
));
016.
}
017.
018.
019.
020.
021.
$postfields
=
array
();
022.
$postfields
[
'key'
] =
'twitpicのAPIキー'
;
023.
$postfields
[
'media'
] =
'@C:\\Documents and Settings\\lightbox\\My Documents\\My Pictures\\1262357623201606.png'
;
024.
$postfields
[
'message'
] =
'画像用のコメント投稿'
;
025.
026.
$twit_url
=
'http://api.twitpic.com/2/upload.json'
;
027.
$twitter_url
=
'https://api.twitter.com/1/account/verify_credentials.json'
;
028.
029.
030.
031.
032.
$oauth_consumer_key
=
"Consumer key"
;
033.
$oauth_consumer_secret
=
"Consumer secret"
;
034.
$oauth_token
=
"Access Token"
;
035.
$oauth_secret
=
"Access Token Secret"
;
036.
037.
038.
$mt
= microtime();
039.
$rand
= mt_rand();
040.
$oauth_nonce
= md5(
$mt
.
$rand
);
041.
042.
$oauth_signature_method
=
"HMAC-SHA1"
;
043.
$oauth_timestamp
=
mktime
();
044.
$oauth_version
=
"1.0"
;
045.
046.
047.
048.
049.
050.
051.
052.
053.
054.
055.
056.
057.
$base_string
=
"GET"
;
058.
$base_string
.=
"&"
. url_rfc3986(
$twitter_url
);
059.
$base_string
.=
"&"
;
060.
061.
$base_string
.= url_rfc3986(
"oauth_consumer_key"
).
"%3D"
.url_rfc3986(
$oauth_consumer_key
).
"%26"
;
062.
$base_string
.= url_rfc3986(
"oauth_nonce"
).
"%3D"
.url_rfc3986(
$oauth_nonce
).
"%26"
;
063.
$base_string
.= url_rfc3986(
"oauth_signature_method"
).
"%3D"
.url_rfc3986(
$oauth_signature_method
).
"%26"
;
064.
$base_string
.= url_rfc3986(
"oauth_timestamp"
).
"%3D"
.url_rfc3986(
$oauth_timestamp
).
"%26"
;
065.
$base_string
.= url_rfc3986(
"oauth_token"
).
"%3D"
.url_rfc3986(
$oauth_token
).
"%26"
;
066.
$base_string
.= url_rfc3986(
"oauth_version"
).
"%3D"
.url_rfc3986(
$oauth_version
);
067.
068.
069.
070.
071.
072.
073.
074.
075.
$oauth_signature
=
076.
base64_encode
( hash_hmac(
077.
"sha1"
,
078.
$base_string
,
079.
url_rfc3986(
$oauth_consumer_secret
) .
"&"
. url_rfc3986(
$oauth_secret
),
080.
true
081.
));
082.
083.
084.
085.
086.
$curl
= curl_init();
087.
curl_setopt(
$curl
, CURLOPT_CONNECTTIMEOUT, 30);
088.
curl_setopt(
$curl
, CURLOPT_HEADER, false);
089.
curl_setopt(
$curl
, CURLOPT_RETURNTRANSFER, true);
090.
curl_setopt(
$curl
, CURLOPT_BINARYTRANSFER, true);
091.
curl_setopt(
$curl
, CURLOPT_URL,
$twit_url
);
092.
093.
curl_setopt(
$curl
, CURLOPT_POST, true);
094.
curl_setopt(
$curl
, CURLOPT_POSTFIELDS,
$postfields
);
095.
096.
097.
098.
099.
$header
=
array
();
100.
101.
102.
$header
[] =
'X-Verify-Credentials-Authorization: OAuth realm="http://api.twitter.com/",'
.
103.
url_rfc3986(
"oauth_consumer_key"
).
"=\""
.url_rfc3986(
$oauth_consumer_key
).
"\","
.
104.
url_rfc3986(
"oauth_signature_method"
).
"=\""
.url_rfc3986(
$oauth_signature_method
).
"\","
.
105.
url_rfc3986(
"oauth_token"
).
"=\""
.url_rfc3986(
$oauth_token
).
"\","
.
106.
url_rfc3986(
"oauth_timestamp"
).
"=\""
.url_rfc3986(
$oauth_timestamp
).
"\","
.
107.
url_rfc3986(
"oauth_nonce"
).
"=\""
.url_rfc3986(
$oauth_nonce
).
"\","
.
108.
url_rfc3986(
"oauth_version"
).
"=\""
.url_rfc3986(
$oauth_version
).
"\","
.
109.
url_rfc3986(
"oauth_signature"
).
"=\""
.url_rfc3986(
$oauth_signature
).
"\""
;
110.
111.
112.
$header
[] =
'X-Auth-Service-Provider: '
.
$twitter_url
;
113.
114.
print
"<pre>"
;
115.
print_r(
$header
);
116.
print
"</pre>"
;
117.
118.
curl_setopt(
$curl
, CURLOPT_HTTPHEADER,
$header
);
119.
120.
121.
122.
123.
124.
125.
126.
curl_setopt(
$curl
, CURLOPT_VERBOSE, true);
127.
$handle
=
fopen
(
"./debug.txt"
,
"w"
);
128.
curl_setopt(
$curl
, CURLOPT_STDERR,
$handle
);
129.
$handle2
=
fopen
(
"./ret_header.txt"
,
"w"
);
130.
curl_setopt(
$curl
, CURLOPT_WRITEHEADER,
$handle2
);
131.
$result
= curl_exec(
$curl
);
132.
133.
134.
135.
136.
137.
print
"<br>"
;
138.
139.
if
(
$result
=== false) {
140.
$json
=
'Curl error: '
. curl_error(
$curl
);
141.
}
142.
else
{
143.
echo
'Operation completed without any errors'
;
144.
$json
= json_decode(
$result
);
145.
}
146.
curl_close(
$curl
);
147.
fclose(
$handle2
);
148.
fclose(
$handle
);
149.
150.
151.
152.
153.
154.
print
"<pre>"
;
155.
print_r(
$json
);
156.
print
"</pre>"
;
157.
158.
159.
?>