nxs_class_SNAP_TW

The NextScripts: Social Networks Auto-Poster nxs class SNAP TW class.

Defined (1)

The class is defined in the following location(s).

/inc-cl/tw.api.php  
  1. if (!class_exists("nxs_class_SNAP_TW")) { class nxs_class_SNAP_TW { 
  2.  
  3. var $ntCode = 'TW'; 
  4. var $ntLCode = 'tw'; 
  5.  
  6. function doPost($options, $message) { if (!is_array($options)) return false; $out = array(); // return false; 
  7. foreach ($options as $ii=>$ntOpts) $out[$ii] = $this->doPostToNT($ntOpts, $message); 
  8. return $out; 
  9. function doPostToNT($options, $message) { global $nxs_urlLen; $badOut = array('pgID'=>'', 'isPosted'=>0, 'pDate'=>date('Y-m-d H:i:s'), 'Error'=>'');  
  10. if (!function_exists('nxs_remote_get') && function_exists('wp_remote_get')) { function nxs_remote_get($url) {return wp_remote_get($url);} } 
  11. if (!function_exists('is_nxs_error') && function_exists('is_wp_error')) { function is_nxs_error($a) {return is_wp_error($a);} } 
  12. //## Check settings 
  13. if (!is_array($options)) { $badOut['Error'] = 'No Options'; return $badOut; }  
  14. if (!isset($options['twAccToken']) || trim($options['twAccToken'])=='') { $badOut['Error'] = 'No Auth Token Found'; return $badOut; } 
  15. if (empty($options['imgSize'])) $options['imgSize'] = ''; 
  16. //## Old Settings Fix 
  17. if ($options['attchImg']=='1') $options['attchImg'] = 'large'; if ($options['attchImg']=='0') $options['attchImg'] = false; 
  18. if (isset($message['img']) && is_string($message['img']) ) $img = trim($message['img']); else $img = '';  
  19. //## Format Post 
  20. if (!empty($message['pText'])) $msg = $message['pText']; else $msg = nxs_doFormatMsg($options['twMsgFormat'], $message);  
  21. if ($options['attchImg']!=false) { if (isset($message['imageURL'])) $imgURL = trim(nxs_getImgfrOpt($message['imageURL'], $options['imgSize'])); else $imgURL = ''; } 
  22. if (empty($imgURL) && $img=='') $options['attchImg'] = false;  
  23. //## Make Post 
  24. //$options['attchImg']='1'; $imgURL = 'http://ecx.images-amazon.com/images/I/41caE5Uc5ML._AA160_.jpg'; 
  25. $hdrsArr['User-Agent']='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0'; $advSet=array('headers'=>$hdrsArr, 'httpversion'=>'1.1', 'timeout'=>45, 'sslverify'=>false); 
  26. //$msg = $message['message']; $imgURL = trim($message['imageURL']); $img = trim($message['img']); $nxs_urlLen = $message['urlLength'];  
  27. if ($options['attchImg']!=false && $img=='' && $imgURL!='' ) { $imgURL = str_replace(' ', '%20', $imgURL); 
  28. if( ini_get('allow_url_fopen') ) { if (getimagesize($imgURL)!==false) { $img = nxs_remote_get($imgURL, $advSet); if(is_nxs_error($img)) $options['attchImg'] = false; else $img = $img['body']; } else $options['attchImg'] = false; }  
  29. else { $img = nxs_remote_get($imgURL, $advSet); if(is_nxs_error($img)) $options['attchImg'] = false; elseif (isset($img['body'])&& trim($img['body'])!='') $img = $img['body']; else $options['attchImg'] = false; }  
  30. }  
  31. if ($options['attchImg']!=false && $img!='') $twLim = 118; else $twLim = 140;  
  32.  
  33. require_once ('apis/tmhOAuth.php'); if ($nxs_urlLen>0) { $msg = nsTrnc($msg, $twLim-22+$nxs_urlLen); } else $msg = nsTrnc($msg, $twLim); //prr($msg); die('TTWWW'); 
  34. if (substr($msg, 0, 1)=='@') $msg = ' '.$msg; //prr(urlencode($msg)); $msg = html_entity_decode($msg); prr(urlencode($msg)); die();  
  35. $tmhOAuth = new NXS_tmhOAuth(array( 'consumer_key' => $options['twConsKey'], 'consumer_secret' => $options['twConsSec'], 'user_token' => $options['twAccToken'], 'user_secret' => $options['twAccTokenSec']));  
  36. if ($options['attchImg']!=false && $img!='') $params_array =array( 'media[]' => $img, 'status' => $msg); else $params_array = array('status' =>$msg); 
  37. if (!empty($options['in_reply_to_id'])) $params_array['in_reply_to_status_id'] = $options['in_reply_to_id'];  
  38. if ($options['attchImg']!=false && $img!='') $code = $tmhOAuth -> request('POST', 'https://api.twitter.com/1.1/statuses/update_with_media.json', $params_array, true, true);  
  39. else $code = $tmhOAuth->request('POST', $tmhOAuth->url('1.1/statuses/update'), $params_array); //prr($msg); 
  40.  
  41. if ( $code=='403' && stripos($tmhOAuth->response['response'], 'User is over daily photo limit')!==false && $options['attchImg']!=false && $img!='') {  
  42. $badOut['Error'] .= "User is over daily photo limit. Will post without image\r\n"; $code = $tmhOAuth->request('POST', $tmhOAuth->url('1.1/statuses/update'), array('status' =>$msg)); 
  43. }  
  44. if ($code == 200) { 
  45. $twResp = json_decode($tmhOAuth->response['response'], true); if (is_array($twResp) && isset($twResp['id_str'])) $twNewPostID = $twResp['id_str'];  
  46. if (is_array($twResp) && isset($twResp['user'])) $twPageID = $twResp['user']['screen_name']; 
  47. return array('postID'=>$twNewPostID, 'isPosted'=>1, 'postURL'=>'https://twitter.com/'.$twPageID.'/status/'.$twNewPostID, 'pDate'=>date('Y-m-d H:i:s'));  
  48. } else { $badOut['Error'] .= "Resp: ".print_r($tmhOAuth->response['response'], true)."| Error: ".print_r($tmhOAuth->response['error'], true)."| MSG: ".print_r($msg, true);  
  49. return $badOut; 
  50. return $badOut; 
  51. }  
  52.  
  53. }}