4 A NOTE ON TWITTER ERRORS:
5 ERROR 215 TYPICALLY MEANS YOU GOT THE AUTHENTICATION STRING FORAMT WRONG
6 ERROR 32 MEANS YOU GOT THE VALUES WRONG
9 function removeExtraSymbols($string){
10 $string = str_replace(".", "a", $string);
11 $string = str_replace("%", "b", $string);
12 $string = str_replace("/", "c", $string);
16 require_once("class/oauth-random.php");
17 require_once("class/queue-database-construction.php");
18 class TwitterConnection
{
19 private $oauth_data = array();
20 private $user_info = array();
21 private $post_properties = array();
23 private $media_api = "https://upload.twitter.com/1.1/media/upload.json";
24 private $status_api = "https://api.twitter.com/1.1/statuses/update.json";
25 private $user_timeline_api = "https://api.twitter.com/1.1/statuses/user_timeline.json";
26 private $mention_timeline_api = "https://api.twitter.com/1.1/statuses/mentions_timeline.json";
27 private $oembed_api = "https://publish.twitter.com/oembed";
28 private $default_status_string = "https://twitter.com/:USERNAME/status/";
30 function __construct(){
31 $this->oauth_data
= $this->getIniFile("settings/keys.ini");
32 $this->user_info
= $this->getIniFile("settings/userinfo.ini");
33 $this->post_properties
= $this->getIniFile("settings/postproperties.ini");
34 $this->buildStatusString();
38 function buildStatusString(){
39 $this->default_status_string
= str_replace(":USERNAME", $this->user_info
["User-Name"], $this->default_status_string
);
42 function getIniFile($path){
43 $path_string = fopen($path,"r");
44 $return_array = array();
45 while(!feof($path_string)){
46 $line = fgets($path_string);
47 $key = substr($line,0,strpos($line, "="));
49 $value = trim(substr($line, strpos($line, "=")+
1));
51 $return_array[$key] = $value;
56 function getEmbededTweet($post_id){
57 $query_string = "url=" . rawurlencode($this->default_status_string
. "$post_id") . "&maxwidth=" . 550;
58 $curl = curl_init($this->oembed_api
. "?$query_string");
59 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
60 $content = curl_exec($curl);
62 return json_decode($content,true);
65 function retrieveTimeline(){
66 $highest_post_id = -1;
68 $timeline_arr = $this->getUserTimeline($this->post_properties
["TopPostNo"]);
69 $timeline_database_arr = array();
71 var_dump($timeline_arr);
73 if($timeline_arr["errors"][0]["code"] == null && sizeof($timeline_arr) != 0){
74 foreach ($timeline_arr as $timeline_item){
75 $post_id = $timeline_item["id"];
76 $post_text = $timeline_item["text"];
77 $post_image_string = $this->grabTwitterImage($timeline_item);
78 array_push($timeline_database_arr, [$post_id, $post_text, $post_image_string]);
80 $highest_post_id = $post_id > $highest_post_id ?
$post_id : $highest_post_id;
83 else echo $timeline_arr["errors"][0]["code"] . "Tim<br/>";
85 echo "<hr>"; //From the post ID try and find any replies
87 $reply_arr = $this->getTweetReplies($this->post_properties
["TopPostNo"]);
88 $reply_arr_container = array();
90 if($reply_arr["errors"][0]["code"] == null && sizeof($reply_arr) != 0){
92 foreach($reply_arr as $reply_item){
93 $reply_id = $reply_item["id"];
94 $reply_text = $reply_item["text"];
95 $reply_image_string = $this->grabTwitterImage($reply_item);
96 $responding_to_id = $reply_item['in_reply_to_status_id'];
97 echo "$lowest_post_id -- $responding_to_id<hr/>";
98 array_push($reply_arr_container, [$reply_id, $reply_text, $reply_image_string, $responding_to_id]);
100 $highest_post_id = $reply_id > $highest_post_id ?
$reply_id : $highest_post_id;
103 else echo $reply_arr["errors"][0]["code"] . "Rep<br/>";
105 if(sizeof($timeline_database_arr) +
sizeof($reply_arr_container) == 0){
106 echo "No Updates<hr/>";
110 $postfile = fopen("settings/postproperties.ini", "w");
111 echo "TopPostNo=$highest_post_id<br/>";
112 //fwrite($postfile, "TopPostNo=$highest_post_id\n" . "Catalog-Size=" . $this->post_properties["Catalog-Size"]);
115 $combined_database_arr = array_merge ($timeline_database_arr, $reply_arr_container);
116 $database_connection = new QueueDatabaseConstruction(true);
120 $this->recursiveEchoJson($combined_database_arr,0);
121 echo sizeof($timeline_database_arr) . "<pre>";
122 if(sizeof($timeline_database_arr) != 0){
123 $this->addTimelineTweetsToDatabase($combined_database_arr, $database_connection);
127 $database_connection = null;
130 function deleteExpiredEntries(){
131 $database_connection = new QueueDatabaseConstruction(true);
133 $threads = $database_connection->getThreads();
135 foreach($threads as $thread){
137 echo(var_dump($this->post_properties
));
138 if($thread_count > $this->post_properties
["Catalog-Size"]){
140 $database_connection->deleteFromUnprocessedImageString($thread["ImageURL"]);
141 $database_connection->recursiveDeleteResponses($thread[0]);
142 $database_connection->deleteThread("Tweet", $thread[0]);//0 is the most relevant PostID
145 $database_connection = null;
149 function endConnection(){
150 $this->database_connection
= null;
153 function grabTwitterImage($tweet_array){
155 $image_url_string = null;
157 if($tweet_array["extended_entities"] != null){
158 foreach($tweet_array["extended_entities"] ["media"] as $entity){
160 if(isset($entity["video_info"])){
161 $filename_url = $entity["video_info"]["variants"][0]["url"];
162 $extention = pathinfo($filename_url, PATHINFO_EXTENSION
);
163 echo "$filename_url $extention";
166 $filename_url = $entity["media_url_https"];
167 $extention = pathinfo($filename_url , PATHINFO_EXTENSION
);
170 $filename = "images/" . (microtime(true) * 10000) . (rand(0,10000)) . ".$extention";
173 $this->uploadMedia($filename, $filename_url);
176 $image_url_string = rawurlencode($filename);
178 else $image_url_string .= "," . rawurlencode($filename);
182 return $image_url_string;
185 function recursiveEchoJson($json, $indents){
187 foreach($json as $key => $attribute){
188 if(is_array ($attribute)){
189 $this->makeIndents($indents);
192 $this->recursiveEchoJson($attribute, ++
$indents);
194 $this->makeIndents(--$indents);
198 $this->makeIndents($indents);
199 echo "$key = $attribute \n";
203 if($indents == 1) echo "<hr/>";
206 function makeIndents($indent_count){
207 for ($i = 0; $i < $indent_count ; $i++
){ echo "\t"; }
210 function addTimelineTweetsToDatabase($timeline_database_arr, $database_connection){
211 var_dump($timeline_database_arr);
212 foreach($timeline_database_arr as $key => $timeline_item){
213 $database_connection->addToTable("Tweet", array("PostID"=>$timeline_item[0],
214 "PostText"=> $timeline_item[1], "ImageURL"=> $timeline_item[2]));
215 if($timeline_item[3] !== null)
216 $database_connection->addToTable("Response", array("PostID"=>$timeline_item[0], "RepliesTo"=>$timeline_item[3]));
219 function uploadMedia($filename,$url){ echo($filename . " " . $url);
220 file_put_contents($filename, fopen($url, 'r'));
223 function getUserTimeline($since_id = 976628662446551043, $count = 100){
225 $random_value = OauthRandom
::randomAlphaNumet(32);
226 $method = "HMAC-SHA1";
227 $oauth_version = "1.0";
229 $reply_exclude = "false";
231 echo $this->user_info
["User-ID"];
233 $get_fields = "since_id=" . $since_id . "&count=" . $count . "&include_rts=false&exclude_replies=$reply_exclude&user_id=" . $this->user_info
["User-ID"];
234 //$msg_len = (strlen($this->user_timeline_api . "?$get_fields")); //GET REQUESTS HAVE NO DYNAMIC LENGTH
236 $param_array = array( "user_id" => $this->user_info
["User-ID"],
237 "since_id" => "$since_id",
238 "exclude_replies"=>"$reply_exclude",
240 "include_rts" => "false",
241 "oauth_version" => "$oauth_version",
242 "oauth_nonce"=>"$random_value",
243 "oauth_token"=> $this->oauth_data
["oauth_token"],
244 "oauth_timestamp" => "$timestamp",
245 "oauth_consumer_key" => $this->oauth_data
["oauth_consumer_key"],
246 "oauth_signature_method" => "$method"
249 $signature = rawurlencode($this->generateSignature(array(
250 "base_url" => $this->user_timeline_api
,
251 "request_method" => "GET"),
254 "consumer_secret" => $this->oauth_data
["consumer_secret"],
255 "oauth_secret" => $this->oauth_data
["oauth_secret"]
259 $param_array["oauth_signature"] = $signature;
260 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,
261 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", "Host: api.twitter.com",
262 $this->buildAuthorizationString($param_array));
266 $curl = curl_init($this->user_timeline_api
. "?$get_fields");
267 curl_setopt($curl, CURLOPT_HTTPGET
, 1);
268 curl_setopt($curl, CURLOPT_HTTPHEADER
, $header_data);
269 curl_setopt($curl, CURLOPT_HEADER
, false);
270 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
271 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, false);
273 echo "<br/>-- Fin -- <hr/>";
274 $content = curl_exec($curl);
275 return json_decode(($content), true);
280 function getTweetReplies($current_post_id, $max_post_id = -1){
281 $random_value = OauthRandom
::randomAlphaNumet(32);
282 $method = "HMAC-SHA1";
283 $oauth_version = "1.0";
287 $get_fields = "since_id=" . $current_post_id;
288 $param_array = array(
289 "since_id" => "$current_post_id",
290 "oauth_version" => "$oauth_version",
291 "oauth_nonce"=>"$random_value",
292 "oauth_token"=> $this->oauth_data
["oauth_token"],
293 "oauth_timestamp" => "$timestamp",
294 "oauth_consumer_key" => $this->oauth_data
["oauth_consumer_key"],
295 "oauth_signature_method" => "$method"
297 if($max_post_id > 0){
298 $get_fields .= $max_post_id > 0 ?
"&max_id=" . $max_post_id : "";
299 $param_array["max_id"] = "$max_post_id";
302 $signature = rawurlencode($this->generateSignature(array(
303 "base_url" => $this->mention_timeline_api
,
304 "request_method" => "GET"),
307 "consumer_secret" => $this->oauth_data
["consumer_secret"],
308 "oauth_secret" => $this->oauth_data
["oauth_secret"]
311 $param_array["oauth_signature"] = $signature;
312 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,
313 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", "Host: api.twitter.com",
314 $this->buildAuthorizationString($param_array));
317 $curl = curl_init($this->mention_timeline_api
. "?$get_fields");
318 curl_setopt($curl, CURLOPT_HTTPGET
, 1);
319 curl_setopt($curl, CURLOPT_HTTPHEADER
, $header_data);
320 curl_setopt($curl, CURLOPT_HEADER
, false);
321 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
322 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, false);
323 $content = curl_exec($curl);
324 return json_decode(($content), true);
330 function buildAuthorizationString($parameters){
331 $authorization_string = 'Authorization: OAuth ';
334 $is_first_join = false;
335 foreach($parameters as $key => $value){
337 $is_first_join = true;
338 $authorization_string .= $key . '="' . $value . '"';
341 $authorization_string .= "," . $key . '="' . $value . '"';
344 return $authorization_string;
347 function makeTweet($comment, $file_arr){
348 $image_string = $this->addTweetMedia($file_arr);
351 $request_method = "POST";
354 $encode_tweet_msg = rawurlencode($comment);
355 $include_entities = "true";
357 //append to postfield_string the media code via media_ids=$media_id
358 $postfield_string = "include_entities=$include_entities&status=$encode_tweet_msg&media_ids=$image_string";
359 $msg_len = (strlen($postfield_string));
361 $random_value = OauthRandom
::randomAlphaNumet(32);
362 $method = "HMAC-SHA1";
363 $oauth_version = "1.0";
366 $param_array = array("include_entities" => "$include_entities",
367 "status" => "$encode_tweet_msg",
368 "media_ids" => "$image_string",
369 "oauth_version" => "$oauth_version",
370 "oauth_nonce"=>"$random_value",
371 "oauth_token"=> $this->oauth_data
["oauth_token"],
372 "oauth_timestamp" => "$timestamp",
373 "oauth_consumer_key" => $this->oauth_data
["oauth_consumer_key"],
374 "oauth_signature_method" => "$method"
377 //add media id to the signature
378 $signature = rawurlencode($this->generateSignature(array(
379 "base_url" => $this->status_api
,
380 "request_method" => $request_method),
383 "consumer_secret" => $this->oauth_data
["consumer_secret"],
384 "oauth_secret" => $this->oauth_data
["oauth_secret"]
388 $param_array["oauth_signature"] = $signature;
389 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,
390 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
391 "Content-Length: $msg_len", "Host: api.twitter.com",
392 $this->buildAuthorizationString($param_array));
396 $curl = curl_init($this->status_api
);
397 curl_setopt($curl, CURLOPT_POST
, 1);
398 curl_setopt($curl, CURLOPT_HTTPHEADER
, $header_data);
399 curl_setopt($curl, CURLOPT_POSTFIELDS
, $postfield_string);
400 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
401 echo "<br/>-- Fin -- <hr/>";
402 $content = curl_exec($curl);
403 var_dump (json_decode($content, true));
404 return json_decode($content, true);
407 function addTweetMedia($file_arr){
410 $image_string = "";//delimited by ',' commas
411 for($file = 0 ; $file < count($file_arr) ; $file++
){
412 if($file_arr[$file] != ""){
413 //create data in binary/b64
414 $mime_type = pathinfo($file_arr[$file], PATHINFO_EXTENSION
);
415 $file_arr[$file] = urldecode($file_arr[$file]);
416 $binary = file_get_contents($file_arr[$file]);
418 $base64 = base64_encode($binary);
420 //upload file to twitter and get id for use in files
421 $size = filesize($file_arr[$file]);
423 $image_string = $this->getMediaID($base64, $size, 'image/' . $mime_type);
425 $image_string .= "," . $this->getMediaID($base64, $size, 'image/' . $mime_type);
428 return rawurlencode($image_string);
431 function getMediaID($base64, $size, $mime_type){
432 $random_value = OAuthRandom
::randomAlphaNumet(32);
436 /////////////MAKE INIT////////////
438 $media_id = $this->mediaInit($size, $mime_type, $random_value, $timestamp);
442 /////////////MAKE APPEND////////////
444 $this->mediaAppend($base64, $media_id, $random_value, $timestamp);
448 /////////////MAKE FINAL/
449 $this->makeFinal($media_id, $random_value, $timestamp);
455 function mediaInit($size, $mime, $random_value, $timestamp){
457 $method = "HMAC-SHA1";
458 $oauth_version = "1.0";
460 $postfield_string = "command=$command&total_bytes=$size&media_type=$mime";
462 $msg_len = (strlen($postfield_string));
465 $signature = rawurlencode($this->generateSignature(array(
466 "base_url" => $this->media_api
,
467 "request_method" => "POST"),
469 "command" => "$command",
470 "total_bytes" => "$size",
471 "media_type" => "$mime",
472 "oauth_version" => "$oauth_version",
473 "oauth_nonce"=>"$random_value",
474 "oauth_token"=> $this->oauth_data
["oauth_token"],
475 "oauth_timestamp" => "$timestamp",
476 "oauth_consumer_key" => $this->oauth_data
["oauth_consumer_key"],
477 "oauth_signature_method" => "$method",
480 "consumer_secret" => $this->oauth_data
["consumer_secret"],
481 "oauth_secret" => $this->oauth_data
["oauth_secret"]
487 $header_data = array("Accept: */*", "Connection: close","User-Agent: VerniyXYZ-CURL" ,"Content-Transfer-Encoding: binary",
488 "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
489 "Content-Length: $msg_len", "Host: upload.twitter.com",
490 'Authorization: OAuth oauth_consumer_key="' . $this->oauth_data
["oauth_consumer_key"] .'",oauth_nonce="' . $random_value . '",oauth_signature="' .
491 $signature . '",oauth_signature_method="' .$method . '"' . ',oauth_timestamp="' . $timestamp . '",oauth_token="' . $this->oauth_data
["oauth_token"] . '",oauth_version="' . $oauth_version . '"'
495 $curl = curl_init($this->media_api
);
496 curl_setopt($curl, CURLOPT_POST
, 1);
497 curl_setopt($curl, CURLOPT_HTTPHEADER
, $header_data);
498 curl_setopt($curl, CURLOPT_POSTFIELDS
, $postfield_string);
499 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
500 $media_id_arr = json_decode(curl_exec($curl), true);
501 print_r ($media_id_arr);
502 return $media_id_arr["media_id_string"];
505 function mediaAppend(&$binary_file, $media_id, $random_value, $timestamp){
507 $method = "HMAC-SHA1";
508 $oauth_version = "1.0";
514 $signature = rawurlencode($this->generateSignature(array(
515 "base_url" => $this->media_api
,
516 "request_method" => "POST"),
518 "command" => "$command",
519 "media" => "$binary_file",
520 "media_id"=>"$media_id",
521 "segment_index"=>"$segment_index",
522 "oauth_version" => "$oauth_version",
523 "oauth_nonce"=>"$random_value",
524 "oauth_token"=> $this->oauth_data
["oauth_token"],
525 "oauth_timestamp" => "$timestamp",
526 "oauth_consumer_key" => $this->oauth_data
["oauth_consumer_key"],
527 "oauth_signature_method" => "$method",
530 "consumer_secret" => $this->oauth_data
["consumer_secret"],
531 "oauth_secret" => $this->oauth_data
["oauth_secret"]
536 $postfield_string = "media=" . rawurlencode($binary_file) . "&command=$command&media_id=$media_id&segment_index=$segment_index" ;
537 $msg_len = (strlen($postfield_string));
538 $header_data = array("Except:", "Connection: close","User-Agent: VerniyXYZ-CURL" ,"Content-Transfer-Encoding: binary",
539 "Content-Type: application/x-www-form-urlencoded",
540 "Content-Length: $msg_len", "Host: upload.twitter.com",
541 'Authorization: OAuth oauth_consumer_key="' . $this->oauth_data
["oauth_consumer_key"] .'",oauth_nonce="' . $random_value . '",oauth_signature="' .
542 $signature . '",oauth_signature_method="' .$method . '"' . ',oauth_timestamp="' . $timestamp . '",oauth_token="' . $this->oauth_data
["oauth_token"] . '",oauth_version="' . $oauth_version . '"'
545 $curl = curl_init($this->media_api
);
546 curl_setopt($curl, CURLOPT_POST
, 1);
547 curl_setopt($curl, CURLOPT_HTTPHEADER
, $header_data);
548 curl_setopt($curl, CURLOPT_POSTFIELDS
, $postfield_string);
549 curl_setopt($curl, CURLOPT_HEADER
, true);
550 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
551 $http_response = curl_exec($curl);
555 function makeFinal($media_id, $random_value, $timestamp){
556 $command = "FINALIZE";
557 $method = "HMAC-SHA1";
558 $oauth_version = "1.0";
560 $signature = rawurlencode($this->generateSignature(array(
561 "base_url" => $this->media_api
,
562 "request_method" => "POST"),
564 "command" => "$command",
565 "media_id"=>"$media_id",
566 "oauth_version" => "$oauth_version",
567 "oauth_nonce"=>"$random_value",
568 "oauth_token"=> $this->oauth_data
["oauth_token"],
569 "oauth_timestamp" => "$timestamp",
570 "oauth_consumer_key" => $this->oauth_data
["oauth_consumer_key"],
571 "oauth_signature_method" => "$method",
574 "consumer_secret" => $this->oauth_data
["consumer_secret"],
575 "oauth_secret" => $this->oauth_data
["oauth_secret"]
578 $postfield_string = "command=$command&media_id=$media_id" ;
579 $msg_len = (strlen($postfield_string));
580 $header_data = array("Except:", "Connection: close","User-Agent: VerniyXYZ-CURL" ,"Content-Transfer-Encoding: binary",
581 "Content-Type: application/x-www-form-urlencoded",
582 "Content-Length: $msg_len", "Host: upload.twitter.com",
583 'Authorization: OAuth oauth_consumer_key="' . $this->oauth_data
["oauth_consumer_key"] .'",oauth_nonce="' . $random_value . '",oauth_signature="' .
584 $signature . '",oauth_signature_method="' .$method . '"' . ',oauth_timestamp="' . $timestamp . '",oauth_token="' . $this->oauth_data
["oauth_token"] . '",oauth_version="' . $oauth_version . '"'
587 $curl = curl_init($this->media_api
);
588 curl_setopt($curl, CURLOPT_POST
, 1);
589 curl_setopt($curl, CURLOPT_HTTPHEADER
, $header_data);
590 curl_setopt($curl, CURLOPT_POSTFIELDS
, $postfield_string);
591 curl_setopt($curl, CURLOPT_HEADER
, true);
592 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
593 $http_response = curl_exec($curl);
597 function generateSignature($request_arr, $paramater_arr, $secret_arr){
599 $request_method = strtoupper($request_arr["request_method"]);
600 $base_url = rawurlencode($request_arr["base_url"]);
602 ksort($paramater_arr);
603 if(isset($paramater_arr["media"])) $paramater_arr["media"] = rawurlencode($paramater_arr["media"]);
604 $paramter_string = $this->buildOAuthParamaterString($paramater_arr);
606 $base_string = ($request_method . "&" . $base_url . "&" . $paramter_string);
607 $secret_string = $secret_arr["consumer_secret"] . "&" . $secret_arr["oauth_secret"];
608 $signature = (base64_encode(hash_hmac("SHA1",$base_string, $secret_string, true)));
613 function buildOAuthParamaterString($paramater_arr){
615 $join_param_by_amphersand = false;
616 foreach($paramater_arr as $key => $param){
617 if(!$join_param_by_amphersand){
618 $join_param_by_amphersand=true;
621 $param_string .= rawurlencode("&");
623 $param_string .= rawurlencode($key . "=" . $param);
625 return $param_string;
630 echo"run script from externals<br/>";