diff --git a/src/system/application/libraries/Twitter.php b/src/system/application/libraries/Twitter.php index aa71e80e..a7a55966 100644 --- a/src/system/application/libraries/Twitter.php +++ b/src/system/application/libraries/Twitter.php @@ -1,75 +1,142 @@ -CI=&get_instance(); + /** + * Instantiates the twitter object + */ + public function __construct() + { + $this->CI = & get_instance(); } - //--------------------- - public function querySearchAPI($term=null) { + + /** + * Does a twitter search + * + * @param string $term Term to search for (optional) + * + * @return array + */ + public function querySearchAPI($term = null) + { $this->CI->load->library('cache'); - - if (!$term || empty($term[0])) { return array(); } - $ret=array(); - $arr=(!is_array($term)) ? array($term) : $term; + + if (!$term || empty($term[0])) { + return array(); + } + $ret = array(); + $arr = (!is_array($term)) ? array($term) : $term; if (!empty($arr)) { - foreach ($arr as $k=>$v) { - $cname='twitter_'.$v; + foreach ($arr as $k => $v) { + $cname = 'twitter_' . $v; // See if we have a file first... - $ret=$this->CI->cache->getData($cname); //echo '
'; var_dump($ret); echo '
'; + $ret = $this->CI->cache->getData($cname); + //echo '
'; var_dump($ret); echo '
'; if (!$ret) { - $url=$this->api_url.str_replace('#','%23', $v); //echo $url; - $tmp=json_decode(@file_get_contents($url)); - $this->CI->cache->cacheData(trim($cname), $tmp); - if (!empty($tmp)) { foreach ($tmp as $ok=>$ov) { $ret[]=$ov; } } + $url = $this->api_url . str_replace('#', '%23', $v); //echo $url; + $tmp = json_decode(@file_get_contents($url)); + $this->CI->cache->cacheData(trim($cname), $tmp); + if (!empty($tmp)) { + foreach ($tmp as $ok => $ov) { + $ret[] = $ov; + } + } } else { - $tmp=array(); - foreach ($ret->results as $k=>$v) { $tmp[0][$k]=$v; } - $ret=$tmp; + $tmp = array(); + foreach ($ret->results as $k => $v) { + $tmp[0][$k] = $v; + } + $ret = $tmp; } } } + //echo ''; //echo '
'; print_r($ret); echo '
'; return $ret; } - public function sendMsg($msg, $link=null) { - $uname = $this->CI->config->item('twitter_user'); - $pass = $this->CI->config->item('twitter_pass'); - $out = ''; - - $auth = base64_encode($uname.':'.$pass); - $content= "status=".$msg; - - $out.="POST /statuses/update.xml HTTP/1.1\r\n"; - $out.="Authorization: Basic ".$auth."\r\n"; - $out.="Content-Length: ".strlen($content)."\r\n"; - $out.="Host: twitter.com\r\n"; - $out.="\r\n".$content; - - $response=''; - $fp=fsockopen('twitter.com',80, $errno, $errstr); + + /** + * Sends a message on twitter + * + * @param string $msg Message to send + * @param null $link Not used + * + * @return string + */ + public function sendMsg($msg, $link = null) + { + $uname = $this->CI->config->item('twitter_user'); + $pass = $this->CI->config->item('twitter_pass'); + $out = ''; + + $auth = base64_encode($uname . ':' . $pass); + $content = "status=" . $msg; + + $out .= "POST /statuses/update.xml HTTP/1.1\r\n"; + $out .= "Authorization: Basic " . $auth . "\r\n"; + $out .= "Content-Length: " . strlen($content) . "\r\n"; + $out .= "Host: twitter.com\r\n"; + $out .= "\r\n" . $content; + + $response = ''; + $fp = fsockopen('twitter.com', 80, $errno, $errstr); if ($fp) { fwrite($fp, $out); - while(!feof($fp)) { - $response.=fread($fp,1024); + while (!feof($fp)) { + $response .= fread($fp, 1024); } fclose($fp); } + return $response; } - //--------------------- - public function short_bitly($link) { - $url='http://api.bit.ly/shorten?version=2.0.1&login='.$this->blogin.'&apiKey='.$this->bkey.'&longUrl='.urlencode($link); - $ret=json_decode(file_get_contents($url)); + + /** + * Shortens a link using bit.ly + * + * @param string $link Link to shorten + * + * @return string + */ + public function short_bitly($link) + { + $url = 'http://api.bit.ly/shorten?version=2.0.1&login=' . + $this->blogin . '&apiKey=' . $this->bkey . '&longUrl=' . + urlencode($link); + $ret = json_decode(file_get_contents($url)); + return $ret->results->{"http://joind.in"}->shortUrl; } - + }