WikidotSite Class

Description

This is the parent class. It has only the basic properties of the site and user (from the API Key). This class can perform the following API calls:

  • categories.select
  • tags.select
  • post.select
  • posts.get
  • users.get_me

Each API method has an example under the API Methods drop down menu. The below steps are common to the use of this class.

Syntax Examples

Create the start of the PHP page by filling in the site variables:

<?php
//set globals before includes
$newdebuglevel = 0; //debug level     
//add includes
include("wikidot_class.inc");
//set startup parameters for xmlrpc
require_once("xmlrpc.inc");
$GLOBALS ['xmlrpc_null_extension' ] = true;
$ServerPath = "xml-rpc-api.php";
$ServerHostName = "www.wikidot.com";
$ServerPort = "";
//create a client for your site
$Client = new xmlrpc_client($ServerPath,$ServerHostName,$ServerPort);
$Client->setCredentials("USERNAME","API KEY");
$Client->setDebug($newdebuglevel);
$Client->setSSLVerifyPeer(false);
$Client->setSSLVerifyHost(false);
$Client->return_type = "phpvals"; //forces return as php values
$SiteName = "SITENAME";

Create the object by making a call to the constructor and setup the return variable:
$siteobj = new WikidotSite($SiteName, $Client);
$returnarray = array();

Use the new object's method to call the API method you wish covered by this object:
//calling the categories.select API method
$siteobj->php_categories_select($SiteName, $returnarray);

You can now manipulate the $returnarray php array to use the data returned by the API.

The Code

<?php
//Wikidot classes have minimal error handling,
//error messages will be criptic at best
//Wikidot parent class
class WikidotSite
{private $site;
private $client;
private $timeout = 30;
private $debug = 0;
private $name;
private $usertitle;
private $user;
//////////////////constructor destructor
function __construct($pSite, $pClient)
{    echo "Constructor called....</br>";
    $this->site = $pSite;
    $this->client = $pClient;
}
function __destruct()
{    echo "Destructor called....</br>";
    foreach ($this as $key => $value) {
        unset($this->$key);} 
}
//////////////////set functions
function setSite($pSite){$this->site = $pSite;}
function setTimeout($pTimeout){$this->timeout = $pTimeout;}
function setDebug($pDebug){$this->debug = $pDebug;}
function setClient($pClient){$this->client = $pClient;}
function setName($pName){$this->name = $pName;}
function setUsertitle($pUTitle){$this->usertitle = $pUTitle;}
function setUser($pUser){$this->user = $pUser;}
//////////////////get functions
function getClient(){return $this->client;}
function getSite(){return $this->site;}
function getTimeout(){return $this->timeout;}
function getDebug(){return $this->debug;}
//////////////////api wrapper functions
function php_categories_select($sSite, &$returnar)
{    echo "</br>Start New Categories Select</br>\n";
    $request = new xmlrpcmsg("categories.select");
    $request->addParam(new xmlrpcval(
            array("site" => new xmlrpcval($sSite, "string")),"struct"));
    $result = $this->client->send($request, $this->timeout, "https");
    $resultvalue = $result->value();
    if(!$result->faultCode()) { // SUCCESS!
        $returnar = $resultvalue;
        return true; }
    else {echo "<font color=\"red\"> <H1>Fault</H1><BR>\n"." Code: ".$result->faultCode()."\n<BR>".
        " Reason: ".$result->faultString()."'\n<BR></font>";
        return false;}
}
function php_tags_select($criteria, &$returnar)
{    echo "</br>Start New Tags Select</br>\n";
    $request = new xmlrpcmsg("tags.select");
    $selectors = array();
    $criteria[0] = ""; //forces $criteria to be an array
    $selectors['site'] = new xmlrpcval($this->site, "string");
//////////parse critera
    foreach($criteria as $key=>$data){
        switch($key){
            case "categories":
                $sCat = array();
                foreach($data as $dkey=>$cat){
                    $sCat[$dkey] = new xmlrpcval($cat);}
                $selectors['categories'] = new xmlrpcval($sCat, "array");
                break;
            case "page":
                $sPage = array();
                foreach($data as $dkey=>$cat){
                    $sPage[$dkey] = new xmlrpcval($cat);}
                $selectors['pages'] = new xmlrpcval($sPage, "array");
                break;}}  
    $request->addParam(new xmlrpcval($selectors, "struct"));
    $result = $this->client->send($request, $this->timeout, "https");
    $resultvalue = $result->value();
    if(!$result->faultCode()) { // SUCCESS!
        $returnar = $resultvalue;
        return true; }
    else {echo "<font color=\"red\"> <H1>Fault</H1><BR>\n"." Code: ".$result->faultCode()."\n<BR>".
        " Reason: ".$result->faultString()."'\n<BR></font>";
        return false;}
}
function php_posts_select($criteria, &$returnar)
{    echo "</br>Start New Posts Select</br>\n";
    $request = new xmlrpcmsg("posts.select");
    $selectors = array();
    $criteria[0] = ""; //forces $criteria to be an array
    $selectors['site'] = new xmlrpcval($this->site, "string");
//////////parse critera
    foreach($criteria as $key=>$data){
        switch($key){
            case "page": $selectors['page'] = new xmlrpcval($data, "string" );
                break;
            case "thread": $selectors['thread'] = new xmlrpcval($data, "string" );
                break;
            case "reply_to": $selectors['reply_to'] = new xmlrpcval($data, "string" );
                break;
            case "created_by": $selectors['created_by'] = new xmlrpcval($data, "string" );
                break;}}  
    $request->addParam(new xmlrpcval($selectors, "struct"));
    $result = $this->client->send($request, $this->timeout, "https");
    $resultvalue = $result->value();
    if(!$result->faultCode()) { // SUCCESS!
        $returnar = $resultvalue;
        return true; }
    else {echo "<font color=\"red\"> <H1>Fault</H1><BR>\n"." Code: ".$result->faultCode()."\n<BR>".
        " Reason: ".$result->faultString()."'\n<BR></font>";
        return false;}
}
function php_posts_get($posts, &$returnar)
{    echo "</br>Start New Posts Get</br>\n";
    $request = new xmlrpcmsg("posts.get");
    $temparray = array();
//    $posts['a'] = ""; //forces $posts to be an array
    foreach($posts as $key=>$data){
        $temparray[$key] = new xmlrpcval($data);}
    $xmlposts = new xmlrpcval($temparray, "array");    
    $request->addParam(new xmlrpcval(array(
        "site" => new xmlrpcval($this->site, "string"),
        "posts" => $xmlposts), "struct"));
    $result = $this->client->send($request, $this->timeout, "https");
    $resultvalue = $result->value();
    if(!$result->faultCode()) { // SUCCESS!
        $returnar = $resultvalue;
        return true; }
    else {echo "<font color=\"red\"> <H1>Fault</H1><BR>\n"." Code: ".$result->faultCode()."\n<BR>".
        " Reason: ".$result->faultString()."'\n<BR></font>";
        return false;}
}
function php_users_get_me(&$returnar)
{    echo "</br>Start New Users Get Me</br>\n";
    $request = new xmlrpcmsg("users.get_me");
    $result = $this->client->send($request, $this->timeout, "https");
    $resultvalue = $result->value();
    if(!$result->faultCode()) { // SUCCESS!
        $returnar = $resultvalue;
        return true; }
    else {echo "<font color=\"red\"> <H1>Fault</H1><BR>\n"." Code: ".$result->faultCode()."\n<BR>".
        " Reason: ".$result->faultString()."'\n<BR></font>";
        return false;}
}
}//end class WikidotSite
?>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License