Image adding using methods from mrbellek/twitterbot
[EroTweet.git] / tweet.php
blob232239eddde932b0f922f0a0e017719b857c4d72
1 <?php
2 require_once('lib/twitteroauth.php');
4 function statusEncodeCustom($value){
5 $value = rawurlencode($value);
7 //$value = str_replace('.', '%2E', $value);
8 $value = str_replace('-', '%2D', $value);
9 $value = str_replace(':', '%3A', $value);
10 $value = str_replace('/', '%2F', $value);
11 $value = str_replace('=', '%3D', $value);
12 $value = str_replace('+', '%2B', $value);
13 $value = str_replace(',', '%2C', $value);
14 $value = str_replace('!', '%21', $value);
15 $value = str_replace('?', '%3F', $value);
16 $value = str_replace('&', '%26', $value);
18 return $value;
20 function urlEncodeCustom($value){
21 $value = rawurlencode($value);
23 $value = str_replace(':', '%3A', $value);
24 $value = str_replace('/', '%2F', $value);
25 $value = str_replace('!', '%21', $value);
26 $value = str_replace('=', '%3D', $value);
27 $value = str_replace('&', '%26', $value);
29 return $value;
31 function generateSingature($request, $message, $params, $secrets){
32 // BUILD SIGNATURE
33 $request_method = strtoupper($request["request_method"]);
34 $base_url = urlEncodeCustom($request["base_url"]);
35 $paramter_string = urlEncodeCustom("include_entities=". $message["include_entities"] . "&media_ids=" . $message["media_ids"] . "&oauth_consumer_key=" . $params["oauth_consumer_key"]
36 . "&oauth_nonce=" . $params["oauth_nonce"] . "&oauth_signature_method=" . $params["oauth_signature_method"] . "&oauth_timestamp=" . $params["oauth_timestamp"]."&oauth_token=" . $params["oauth_token"] .
37 "&oauth_version=" .$params["oauth_version"] . "&status=" . $message["status"]) . "";
39 $base_string = ($request_method . "&" . $base_url . "&" . $paramter_string);
41 $secret_string = $secrets["consumer_secret"] . "&" . $secrets["oauth_secret"];
43 $signature = strtoupper(hash_hmac("SHA1",$base_string, $secret_string));
45 $split_hex_signature = "";
46 foreach(str_split($signature, 2) as $split){
47 $split_hex_signature .= chr(hexdec($split));
49 return base64_encode($split_hex_signature);
51 function generateMediaSingatureINIT($request, $message, $params, $secrets){
52 // BUILD SIGNATURE
53 $request_method = strtoupper($request["request_method"]);
54 $base_url = urlEncodeCustom($request["base_url"]);
55 $paramter_string = urlEncodeCustom("command=" . $message["command"] . "&media_type=" . $message["media_type"] . "&oauth_consumer_key=" . $params["oauth_consumer_key"]
56 . "&oauth_nonce=" . $params["oauth_nonce"] . "&oauth_signature_method=" . $params["oauth_signature_method"] . "&oauth_timestamp=" . $params["oauth_timestamp"]."&oauth_token=" . $params["oauth_token"] .
57 "&oauth_version=" .$params["oauth_version"] . "&total_bytes=" . $message["total_bytes"]);
59 $base_string = ($request_method . "&" . $base_url . "&" . $paramter_string);
61 $secret_string = $secrets["consumer_secret"] . "&" . $secrets["oauth_secret"];
63 //using binary production
64 $signature = (hash_hmac("SHA1",$base_string, $secret_string, true));
65 $signature = urlEncodeCustom(base64_encode($signature));
66 return $signature;
68 function generateMediaSingatureAPPEND($request, $message, $params, $secrets){
69 // BUILD SIGNATURE
70 $request_method = strtoupper($request["request_method"]);
71 $base_url = urlEncodeCustom($request["base_url"]);
72 //$message["media_data"] = 123;
73 $paramter_string = urlEncodeCustom("command=" . $message["command"] . "&media=" . $message["media"] . "&media_id=" . $message["media_id"] . "&oauth_consumer_key=" . $params["oauth_consumer_key"]
74 . "&oauth_nonce=" . $params["oauth_nonce"] . "&oauth_signature_method=" . $params["oauth_signature_method"] . "&oauth_timestamp=" . $params["oauth_timestamp"]."&oauth_token=" . $params["oauth_token"] .
75 "&oauth_version=" .$params["oauth_version"] . "&segment_index=" . $message["segment_index"]);
78 // echo ("command=" . $message["command"] . "&media=" . $message["media"] . "&media_id=" . $message["media_id"] . "&oauth_consumer_key=" . $params["oauth_consumer_key"]
79 // . "&oauth_nonce=" . $params["oauth_nonce"] . "&oauth_signature_method=" . $params["oauth_signature_method"] . "&oauth_timestamp=" . $params["oauth_timestamp"]."&oauth_token=" . $params["oauth_token"] .
80 // "&oauth_version=" .$params["oauth_version"] . "&segment_index=" . $message["segment_index"]) . "<br><br>";
83 // echo "<hr>" . $message["media"] . "<hr>";
86 $base_string = ($request_method . "&" . $base_url . "&" . $paramter_string);
88 $secret_string = $secrets["consumer_secret"] . "&" . $secrets["oauth_secret"];
90 $signature = strtoupper(hash_hmac("SHA1",$base_string, $secret_string));
92 $split_hex_signature = "";
93 foreach(str_split($signature, 2) as $split){
94 $split_hex_signature .= chr(hexdec($split));
97 $signature = urlEncodeCustom(base64_encode($split_hex_signature));
98 return $signature;
100 function generateMediaSingatureCOMBINED($request, $message, $params, $secrets){
101 // BUILD SIGNATURE
102 $request_method = strtoupper($request["request_method"]);
103 $base_url = urlEncodeCustom($request["base_url"]);
104 $paramter_string = urlEncodeCustom("media=" . $message["media"] . "&oauth_consumer_key=" . $params["oauth_consumer_key"]
105 . "&oauth_nonce=" . $params["oauth_nonce"] . "&oauth_signature_method=" . $params["oauth_signature_method"] . "&oauth_timestamp=" . $params["oauth_timestamp"]."&oauth_token=" . $params["oauth_token"] .
106 "&oauth_version=" .$params["oauth_version"]);
108 $base_string = ($request_method . "&" . $base_url . "&" . $paramter_string);
110 $secret_string = $secrets["consumer_secret"] . "&" . $secrets["oauth_secret"];
112 //using binary production
113 echo "$base_string<hr>";
114 $signature = (hash_hmac("SHA1",$base_string, $secret_string, true));
115 $signature = urlEncodeCustom(base64_encode($signature));
116 return $signature;
118 function getMediaID($binary_file, $size, $mime){
119 $media_api = "https://upload.twitter.com/1.1/media/upload.json";
121 $settings = fopen("settings/keys.ini","r");
122 $keys = array();
123 while(!feof($settings)){
124 $line = fgets($settings);
125 $key = substr($line,0,strpos($line, ":"));
126 //eat last character
127 $value = trim(substr($line, strpos($line, ":")+1));
128 $keys[$key] = $value;
130 $consumer_key = $keys["consumer_key"];
131 $random_value = substr(( base64_encode(rand(1000000000,10000000000000)) . base64_encode(rand(1000000000,10000000000000)) . base64_encode(rand(1000000000000,100000000000000))), 0, 32);
132 $random_value = str_replace("/", "5", str_replace("=", "2", $random_value));
133 $method = "HMAC-SHA1";
134 $timestamp = time();
135 $access_token = $keys["access_token"];
136 $oauth_version = "1.0";
138 //Secrets
139 $consumer_secret = $keys["consumer_secret"];
140 $oauth_secret = $keys["oauth_secret"];
143 echo "<hr/>";
144 /////////////MAKE INIT////////////
145 //post data
146 $command = "INIT";
147 $postfield_string = "command=$command&total_bytes=$size&media_type=$mime";
148 $msg_len = (strlen($postfield_string));
150 //header data
151 // BUILD SIGNATURE
152 $signature = generateMediaSingatureINIT(array(
153 "base_url" => $media_api,
154 "request_method" => "POST"
156 array(
157 "command" => "$command",
158 "total_bytes" => "$size",
159 "media_type" => "$mime"
161 array(
162 "oauth_version" => "$oauth_version",
163 "oauth_nonce" => "$random_value",
164 "oauth_token" => "$access_token",
165 "oauth_timestamp" => "$timestamp",
166 "oauth_consumer_key" => "$consumer_key",
167 "oauth_signature_method" => "$method"
169 array(
170 "consumer_secret" => "$consumer_secret",
171 "oauth_secret" => "$oauth_secret"
177 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,"Content-Transfer-Encoding: binary",
178 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
179 "Content-Length: $msg_len", "Host: upload.twitter.com",
180 "Authorization: OAuth oauth_consumer_key=\"$consumer_key\",oauth_nonce=\"$random_value\",oauth_signature=\"$signature\",oauth_signature_method=\"$method\",oauth_timestamp=\"$timestamp\",oauth_token=\"$access_token\",oauth_version=\"$oauth_version\""
182 //request
183 $curl = curl_init($media_api);
184 curl_setopt($curl, CURLOPT_POST, 1);
185 curl_setopt($curl, CURLOPT_HTTPHEADER, $header_data);
186 curl_setopt($curl, CURLOPT_POSTFIELDS, $postfield_string);
187 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
188 print_r ($header_data) . "++";
189 $media_id = json_decode(curl_exec($curl), true);
190 print_r ($media_id);
191 $media_id = $media_id["media_id_string"];
194 echo "<hr/>";
195 /////////////MAKE APPEND////////////
196 //post data
197 $command = "APPEND";
198 $segment_index = 0;
199 $postfield_string = "command=$command&media_id=$media_id&segment_index=$segment_index&media=$binary_file";
200 $msg_len = (strlen($postfield_string));
201 //header data
202 // BUILD SIGNATURE
203 $signature = generateMediaSingatureAppend(array(
204 "base_url" => $media_api,
205 "request_method" => "POST"
207 array(
208 "command" => "$command",
209 "media" => "$binary_file",
210 "media_id"=>"$media_id",
211 "segment_index"=>"$segment_index"
213 array(
214 "oauth_version" => "$oauth_version",
215 "oauth_nonce" => "$random_value",
216 "oauth_token" => "$access_token",
217 "oauth_timestamp" => "$timestamp",
218 "oauth_consumer_key" => "$consumer_key",
219 "oauth_signature_method" => "$method"
221 array(
222 "consumer_secret" => "$consumer_secret",
223 "oauth_secret" => "$oauth_secret"
229 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,"Content-Transfer-Encoding: binary",
230 "Content-Type: multipart/form-data;charset=UTF-8",
231 "Content-Length: $msg_len", "Host: upload.twitter.com",
232 "Authorization: OAuth oauth_consumer_key=\"$consumer_key\",oauth_nonce=\"$random_value\",oauth_signature=\"$signature\",oauth_signature_method=\"$method\",oauth_timestamp=\"$timestamp\",oauth_token=\"$access_token\",oauth_version=\"$oauth_version\""
234 //request
235 $curl = curl_init($media_api);
236 curl_setopt($curl, CURLOPT_POST, 1);
237 curl_setopt($curl, CURLOPT_HTTPHEADER, $header_data);
238 curl_setopt($curl, CURLOPT_POSTFIELDS, $postfield_string);
239 curl_setopt($curl, CURLOPT_HEADER , true); // we want headers
240 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
241 print_r ($header_data) . "++";
242 $http_response = curl_exec($curl);
243 echo $http_response;
245 echo "<hr/>";
246 /////////////MAKE FINAL/
248 function getMediaIDCOMBINED($binary_file, $size, $mime){
249 $media_api = "https://upload.twitter.com/1.1/media/upload.json";
251 $settings = fopen("settings/keys.ini","r");
252 $keys = array();
253 while(!feof($settings)){
254 $line = fgets($settings);
255 $key = substr($line,0,strpos($line, ":"));
256 //eat last character
257 $value = trim(substr($line, strpos($line, ":")+1));
258 $keys[$key] = $value;
260 $consumer_key = $keys["consumer_key"];
261 $random_value = substr(( base64_encode(rand(1000000000,10000000000000)) . base64_encode(rand(1000000000,10000000000000)) . base64_encode(rand(1000000000000,100000000000000))), 0, 32);
262 $random_value = str_replace("/", "5", str_replace("=", "2", $random_value));
263 $method = "HMAC-SHA1";
264 $timestamp = time();
265 $access_token = $keys["access_token"];
266 $oauth_version = "1.0";
268 //Secrets
269 $consumer_secret = $keys["consumer_secret"];
270 $oauth_secret = $keys["oauth_secret"];
272 echo "<hr/>";
273 /////////////MAKE COMBO////////////
274 //post data
275 $command = "APPEND";
276 $segment_index = 0;
277 $postfield_string = "media=$binary_file";
278 $msg_len = (strlen($postfield_string));
279 //header data
280 // BUILD SIGNATURE
281 $signature = generateMediaSingatureCOMBINED(array(
282 "base_url" => $media_api,
283 "request_method" => "POST"
285 array(
286 "media" => "$binary_file",
288 array(
289 "oauth_version" => "$oauth_version",
290 "oauth_nonce" => "$random_value",
291 "oauth_token" => "$access_token",
292 "oauth_timestamp" => "$timestamp",
293 "oauth_consumer_key" => "$consumer_key",
294 "oauth_signature_method" => "$method"
296 array(
297 "consumer_secret" => "$consumer_secret",
298 "oauth_secret" => "$oauth_secret"
304 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,"Content-Transfer-Encoding: binary",
305 "Content-Type: multipart/form-data;charset=UTF-8",
306 "Content-Length: $msg_len", "Host: upload.twitter.com",
307 "Authorization: OAuth oauth_consumer_key=\"$consumer_key\",oauth_nonce=\"$random_value\",oauth_signature=\"$signature\",oauth_signature_method=\"$method\",oauth_timestamp=\"$timestamp\",oauth_token=\"$access_token\",oauth_version=\"$oauth_version\""
309 //request
310 $curl = curl_init($media_api);
311 curl_setopt($curl, CURLOPT_POST, 1);
312 curl_setopt($curl, CURLOPT_HTTPHEADER, $header_data);
313 curl_setopt($curl, CURLOPT_POSTFIELDS, $postfield_string);
314 curl_setopt($curl, CURLOPT_HEADER , true); // we want headers
315 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
316 print_r ($header_data) . "++";
317 $http_response = curl_exec($curl);
318 echo $http_response;
320 echo "<hr/>";
323 function makeTweet($comment, $file_arr){
325 1. Create a set of media ID's.
326 2. Add to $postfield_string if there's an image
327 3. Authenticate post and submit it to Twitter
330 //image info
331 $image_string = "";//delimited by ',' commas
332 for($file = 0 ; $file < 4 ; $file++){
333 if($file_arr[$file] != "0"){
334 echo "<hr/>$file: $file_arr[$file] -- ";
335 //create data in binary/b64
336 $type = pathinfo($file_arr[$file], PATHINFO_EXTENSION);
337 $binary = file_get_contents($file_arr[$file]);
338 //$base64 = 'data:image/' . $type . ';base64,' . base64_encode($binary);
339 $base64 = base64_encode($binary);
340 echo $base64;
341 //upload file to twitter and get id for use in files?
342 $size = filesize($file_arr[$file]);
343 //$image_string = getMediaID($base64, $size, 'image/' . $type);
344 //getMediaIDCOMBINED($base64, $size, 'image/' . $type);
361 //////////////////////////---------------------========================================----------------------------//////////////////////////////////////////
365 //authorization details
366 $settings = fopen("settings/keys.ini","r");
367 $keys = array();
368 while(!feof($settings)){
369 $line = fgets($settings);
370 $key = substr($line,0,strpos($line, ":"));
371 //eat last character
372 $value = trim(substr($line, strpos($line, ":")+1));
373 $keys[$key] = $value;
375 $consumer_key = $keys["consumer_key"];
376 $random_value = substr(( base64_encode(rand(1000000000,10000000000000)) . base64_encode(rand(1000000000,10000000000000)) . base64_encode(rand(1000000000000,100000000000000))), 0, 32);
377 $random_value = str_replace("/", "5", str_replace("=", "2", $random_value));
378 $method = "HMAC-SHA1";
379 $timestamp = time();
380 $access_token = $keys["access_token"];
381 $oauth_version = "1.0";
383 //Secrets
384 $consumer_secret = $keys["consumer_secret"];
385 $oauth_secret = $keys["oauth_secret"];
391 //connect to twitter
392 $oTwitter = new TwitterOAuth($keys["consumer_key"], $consumer_secret, $access_token, $oauth_secret);
393 $oTwitter->host = "https://api.twitter.com/1.1/";
395 //make output visible in browser
396 if (!empty($_SERVER['HTTP_HOST'])) {
397 echo '<pre>';
400 //load args
402 define('DS', DIRECTORY_SEPARATOR);
406 //upload image and save media id to attach to tweet
407 printf("Uploading to Twitter: %s\n", $file_arr[$file]);
408 $sImageBinary = base64_encode(file_get_contents($file_arr[$file]));
409 if ($sImageBinary && strlen($sImageBinary) < 15 * pow(1024, 2)) { //max size is 15MB
411 $oRet = $oTwitter->upload('media/upload', array('media' => $sImageBinary));
412 if (isset($oRet->errors)) {
413 printf('Twitter API call failed: media/upload (%s)', $oRet->errors[0]->message);
414 return FALSE;
415 } else {
416 printf("- uploaded %s to attach to next tweet\n", $file_arr[$file]);
419 print_r ($oRet);
420 $image_string .= $oRet->media_id_string;
421 if($file > 0 && $file < sizeof($file_arr) - 1) $image_string .= ",";
423 } else {
424 printf("- picture is too large!\n");
430 //////////////////////////---------------------========================================----------------------------//////////////////////////////////////////
442 echo ("IMAGE-STRING: " . $image_string);
443 $image_string = urlencode($image_string);
445 //access info
446 $access_url = "https://api.twitter.com/1.1/statuses/update.json";
447 $request_method = "POST";
449 //message info
450 $encode_tweet_msg = statusEncodeCustom($comment);
451 $include_entities = "true";
453 //append to postfield_string the media code via media_ids=$media_id
454 $postfield_string = "include_entities=$include_entities&status=$encode_tweet_msg&media_ids=$image_string";
455 $msg_len = (strlen($postfield_string));
458 echo " <br/>$postfield_string<br/>";
460 //authorization details
461 $settings = fopen("settings/keys.ini","r");
462 $keys = array();
463 while(!feof($settings)){
464 $line = fgets($settings);
465 $key = substr($line,0,strpos($line, ":"));
466 //eat last character
467 $value = trim(substr($line, strpos($line, ":")+1));
468 $keys[$key] = $value;
470 $consumer_key = $keys["consumer_key"];
471 $random_value = substr(( base64_encode(rand(1000000000,10000000000000)) . base64_encode(rand(1000000000,10000000000000)) . base64_encode(rand(1000000000000,100000000000000))), 0, 32);
472 $random_value = str_replace("/", "5", str_replace("=", "2", $random_value));
473 $method = "HMAC-SHA1";
474 $timestamp = time();
475 $access_token = $keys["access_token"];
476 $oauth_version = "1.0";
478 //Secrets
479 $consumer_secret = $keys["consumer_secret"];
480 $oauth_secret = $keys["oauth_secret"];
482 //add media id to the signature
483 $signature = urlEncodeCustom(generateSingature(array(
484 "base_url" => $access_url,
485 "request_method" => $request_method
487 array(
488 "include_entities" => "$include_entities",
489 "status" => "$encode_tweet_msg",
490 "media_ids" => "$image_string"
492 array(
493 "oauth_version" => "$oauth_version",
494 "oauth_nonce"=>"$random_value",
495 "oauth_token"=>"$access_token",
496 "oauth_timestamp" => "$timestamp",
497 "oauth_consumer_key" => "$consumer_key",
498 "oauth_signature_method" => "$method"
500 array(
501 "consumer_secret" => "$consumer_secret",
502 "oauth_secret" => "$oauth_secret"
506 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,
507 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
508 "Content-Length: $msg_len", "Host: api.twitter.com",
510 "Authorization: OAuth oauth_consumer_key=\"$consumer_key\",oauth_nonce=\"$random_value\",oauth_signature=\"$signature\",oauth_signature_method=\"$method\",oauth_timestamp=\"$timestamp\",oauth_token=\"$access_token\",oauth_version=\"$oauth_version\""
513 //request
514 $curl = curl_init($access_url);
515 curl_setopt($curl, CURLOPT_POST, 1);
516 curl_setopt($curl, CURLOPT_HTTPHEADER, $header_data);
517 curl_setopt($curl, CURLOPT_POSTFIELDS, $postfield_string);
518 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
519 echo "--<hr/>";
520 $content = curl_exec($curl);
521 echo $content;
523 echo"run script from externals";