Daniweb API Class for Getting Forums

Updated diafol 3 Tallied Votes 454 Views Share

As of 4/4/2013

This class has now been updated and posted here: http://www.daniweb.com/web-development/php/code/451674/all-in-one-daniweb-api-class#

ORIGINAL POST

This code can be used to retrieve a forum list with various options with regard to display. This class could be extended for articles, posts, etc etc.

The retrieval methods include file_get_contents() and cURL. The constructor allows manually setting the method or allowing php to determine whether a retrieval method is available.

The call to retrieve data is pretty simple, for example:

//CLIENT CODE
require 'dwapi.class.php';  
$dw = new dwAPI;
echo $dw->getForums();

Alternatively if you needed more specific output, the getForums() method takes 3 optional params:

1) (bool) 'include self' default = false
2) (mixed) 'forum list' default = false [can be: array e.g. array(1,3,4); string e.g. '1,3,4'; int e.g. 4]
3) (string) 'relatives' default = false [can be 'children'|'descendants'|'ancestors']

e.g.1

require 'dwapi.class.php';  
$dw = new dwAPI;
echo $dw->getForums(false,'1,3,4'); //get details for forums 1,3 and 4

e.g.2

require 'dwapi.class.php';  
$dw = new dwAPI;
echo $dw->getForums(true, array(1,2), 'descendants');

The output will be in json format, but you could to the following to get php assoc array:

require 'dwapi.class.php';  
$dw = new dwAPI;
$arr = $dw->json2Array($dw->getForums(true, array(1,2), 'descendants'));

echo "<pre>";print_r($arr);echo "</pre>";

Anyhow, I'm sure seasoned OOPers will find plenty to complain about. If so, please send in your improvements.

LastMitch commented: Nice! +11
cereal commented: thanks for sharing! +11
class dwAPI{

	private $conn;
	private $url;
	
	//construct - select retrieve data method (file_get_contents [default] or cURL) 
	public function __construct($pref='file')
	{
		$this->conn = ($pref != 'file' || !ini_get('allow_url_fopen')) ? new dwCurlGet : new dwFileGet;
	}
	
	//get forum list shortcut
	public function getForums($self=false, $specify=false, $related=false)
	{
		$incSelf = ($self) ? '?include_self=' : '';
		$incRelated = ($related && in_array($related, array("ancestors","children","descendants"))) ? '/' . $related : ''; 
		if($specify)
		{
			if(is_array($specify)){	
				$incSpecifics = implode(";",$specify);
			}elseif(is_int($specify)){
				$incSpecifics = $specify;	
			}else{
				$incSpecifics = str_replace(',',';',$specify);	
			}
			$incSpecifics = '/' . $incSpecifics;
		}else{
			$incSpecifics = '';	
		}
		
		$this->url = 'http://www.daniweb.com/api/forums' . $incSpecifics . $incRelated . $incSelf;
		
		//====TEST URL====//
		//echo $this->url;
		//====/TEST URL===//
		
		return $this->returnData();
	}
	
	//get data from direct url
	public function getList($url)
	{
		$this->url = $url;
		return $this->returnData();
	}
	
	//Hand off return data to the applied 'get' type
	private function returnData()
	{
		return $this->conn->getJSON($this->url);	
	}
	
	//Return data as PHP multidimensional array
	public function json2Array($str)
	{
		return json_decode($str,true);	
	}
}


//classes implementing iConn must have this/these methods
interface iConn{
	function getJSON($url);
}

//filegetcontents retrieve
class dwFileGet implements iConn{
	public function getJSON($url){
		$result = false;
		if($content = file_get_contents($url))
		{
			$result = $content;	
		}
		return $result;	
	}
}

//curl retrieve
class dwCurlGet implements iConn{
	//Thanks to pritaeas for most of this cURL bit
	public function getJSON($url){
		$result = false;
		if (extension_loaded('curl'))
		{
			$ch = curl_init();
			if ($ch)
			{
				curl_setopt($ch, CURLOPT_URL, $url);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$curl = curl_exec($ch);
				if ($curl)
				{
					$result = $curl;
				}
				curl_close($ch);
			}
		}
		return $result;
	}
}
?>
Member Avatar for diafol
diafol

This is downright ugly :(

Look out for a major update soon. It will have...

1) Methods for extracting all non-OAuth data (as seen in the API documentation)
2) OAuth connection
3) Methods for extracting OAuth data

Am at part 3) at the moment...

Member Avatar for LastMitch
LastMitch

This is downright ugly :(

This code look similiar to the one you did for (Dani) JSON code a few weeks back.
I understand how it work because I try it few weeks back. I like it so if you are gonna to update (improve) it I will be looking forward to it.

I know pritaeas post a couple of ones but I find hard make it work so I didn't make any comment until I understand what pritaeas wrote.

Member Avatar for diafol
diafol

No worries, pritaeas was concentrating on the RSS end to start off with. I was looking at the non-OAuth calls. However, I've since decided to expand the class. My OOP skills are pretty basic, so it'll be up to the user to fiddle, improve and hack the class to bits. :)

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

I know pritaeas post a couple of ones but I find hard make it work so I didn't make any comment until I understand what pritaeas wrote.

Just ask.

Member Avatar for diafol
diafol
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.