Location: PHPKode > projects > Bloggy API Class > bloggy-api-class/api_bloggy.class.php
<?
/*
   Author:	Jamie Telin (hide@address.com), currently at employed Zebramedia.se
   
   Name: Bloggy Api Class v1.0
   
   Use:		
   		//Create new object
			$myBloggy = new BloggyApi;
				
			//Send to users Bloggy
				$myBloggy = new BloggyApi;
				$myBloggy->Login = 'username';
				$myBloggy->Password = 'password';
				$myBloggy->Msg = 'Here is my message to Bloggy!';
				$myBloggy->write();
				
			//Recive Bloggy posts from user
				$myBloggy = new BloggyApi;
				$myBloggy->Login = 'username';
				$myBloggy->read();
				
				//Settings for reciving
					$myBloggy->Rss = Set the users rss feed's url	
					$myBloggy->RecivePosts = How many posts you want to recive	
					$myBloggy->UseLinks = Use links or not back to orginal post
					$myBloggy->UseDate = Show date under message
					$myBloggy->Echo = true or false if to echo out result or not

				//Example
					$myBloggy = new BloggyApi;
					$myBloggy->Login = 'username';
					$myBloggy->RecivePosts = 5;
					$myBloggy->UseLinks = false;
					$myBloggy->UseDate = false;
					$myBloggy->read();

				//Recive full rss feed
					$myBloggy->Rss = 'http://twitter.com/statuses/user_timeline/23596839.rss';
					echo $myBloggy->get();
				
				All messages are also returned as a serialized string wich you 
				can unserialize into array with unserialize();
*/
class BloggyApi{
	var $Msg;
	
	var $Login;
	var $Password;
	var $Method = 'json';
	var $Utf8encode = false;
	
	var $Rss;
	var $RecivePosts = 5;
	var $UseLinks = true;
	var $UseDate = true;
	var $Echo = true;
	var $TransFormat = 'utf8';
	
	var $Filter = '';
	var $BoldFilter = true;
	
	function write(){
		if($this->Method == 'json'){
			if($this->Utf8encode){
				$this->Msg = utf8_encode($this->Msg);
			}
			$out="POST http://bloggy.se/statuses/update.xml HTTP/1.1\r\n"
			  ."Host: bloggy.se\r\n"
			  ."Authorization: Basic ".base64_encode ($this->Login.':'.$this->Password)."\r\n"
			  ."Content-type: application/x-www-form-urlencoded\r\n"
			  ."Content-length: ".strlen ("status=".$this->Msg)."\r\n"
			  ."Connection: Close\r\n\r\n"
			  ."status=".$this->Msg;
			$fp = fsockopen ('bloggy.se', 80);
			fwrite ($fp, $out);
			fclose ($fp);
		}
	}
	
	function read(){
		if($this->Rss == ''){
		
			$this->Rss = 'http://search.bloggy.se/search.atom?q=from:'.$this->Login;
			
		}
		$string = $this->get();
		
		//CHECK HOW MANY MSGS THERE ARE
		$i = 0;
		while(strpos($string, '<entry>', $i) !== FALSE){
			$i = strpos($string, '<entry>', $i);
			$i++;
			$msgs_found++;
		}
		
		//IF REQUESTED MORE MSGS THAN EXISTS SET NEW AMOUNT TO GET
		if($msgs_found < $this->RecivePosts){
			$this->RecivePosts = $msgs_found;
		}
		
		//GET MSGS
		$a = 0;
		for($i = 0; $i <= $this->RecivePosts-1; $i++){
			$a = strpos($string, '<entry>', $a);
			
			$a = strpos($string, '<link type="text/html" rel="alternate" href="', $a);
			$a = strpos($string, 'href="', $a);
			$a += 6;
			$b = strpos($string, '"/>', $a);
			$c = $b - $a;
			$msg[$i]['link'] = substr($string, $a, $c);
			
			$a = strpos($string, '<title>', $a);
			$a = strpos($string, '>', $a+1);
			$a++;
			$b = strpos($string, '</title>', $a);
			$c = $b - $a;
			$msg[$i]['msg'] = substr($string, $a, $c);
			
			$a = strpos($string, '<updated>', $a);
			$a = strpos($string, '>', $a+1);
			$a++;
			$b = strpos($string, '</updated>', $a);
			$c = $b - $a;
			$msg[$i]['date'] = substr($string, $a, $c);
			
			$msg[$i]['full'] = $msg[$i]['msg'];
			if($this->Utf8encode){
				$msg[$i]['full'] = utf8_encode($msg[$i]['full']);
			}
			if($this->UseLinks){
				$msg[$i]['full'] = '<a href="'.$msg[$i]['link'].'">'.$msg[$i]['full'].'</a>';
			}
			
			if($this->UseDate){
				$msg[$i]['full'] = $msg[$i]['full'].'<br />'.$msg[$i]['date'];
			}
			
			if($msgs_found > 1){
				$msg[$i]['full'] = $msg[$i]['full'].'<br />';
			}
			if($this->Echo){
				echo $msg[$i]['full'];
			}
		}
		return serialize($msg);
	}
	
	function get(){
		$data = '';
		if($this->Rss == ''){
		
			$this->Rss = 'http://search.bloggy.se/search.atom?q=from:'.$this->Login;
			
		}
		if (function_exists("curl_init")){
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_URL, $this->Rss);
			$data = curl_exec($ch);
			curl_close($ch);
		} 
		
		else if (function_exists("file_get_contents")){
			$data = file_get_contents($this->Rss);
		} 
		else {
			$handle = fopen($this->Rss, "r");
			while(!feof($handle)){
				$data .= fgets($handle);
			}
			fclose($handle);
		}
		if($this->TransFormat == 'utf8'){
			$data = utf8_encode($data);
		}

		return $data;
	}
}
?>
Return current item: Bloggy API Class