PHP example class

<?php
/**
 * Example class for shortner
 * @author John Bakker <me@johnbakker.name>
 * @version 0.3
 * @uses curl,json
 * @see http://wiki.shortner.net/
 * 
 */
 
/**
 * Shortner_rest class
 * Example usage see bottom of file
 */
class shortner_rest
{
    /**
     * prepares a shortner client.
     *
     * @param hostname $host
     * @param apikey $apikey
     * @param user's username $username
     * @param user's password $password
     */
    function __construct($host,$apikey,$username,$password)
    {
        $this->baseurl="http://$host/_REST/$apikey/";
        $this->format = "json";
        $this->username=$username;
        //always send the password as md5
        $this->password=md5($password);
 
    }
    /**
     * Destructor
     * signs the user out
     */
    function __destruct()
    {
        if($this->session)
        {
            $this->signOut();
        }
 
    }
    /**
     * Signs you in.
     *
     * @return boolean
     */
    function signIn()
    {
        if($this->session)
        {
            return true;
        }
        $returnobj = $this->doRequest('signIn');
        if($returnobj->type=='success')
        {
            $this->session = $returnobj->dataNodes->sessionid->data;
            return true;
        }
        else 
        {
            return false;
        }
 
    }
    /**
     * Signs a user out
     *
     * @return boolean
     */
    function signOut()
    {
        $returnobj = $this->doRequest('signOut',array('sessionid'=>$this->session));
        return true;
    }    
    /**
     * Adds a url,always returns a url,
     *
     * @param string $url
     * @param string $description
     * @param string,csv $tags
     * @param string $adult
     * @param string $password
     * @param string $donotrecycle
     * @param string $testmode
     * @return string
     */
    function addUrl($url,$description,$tags='',$adult='no',$password='',$donotrecycle='no',$testmode='no')
    {
        //if you want to use sessions you can use a session :) but not really needed anymore
        /*
        if(!$this->signIn())
        {
            return false;
        }*/
            //build the message we want to send along,which is a post..
            $msg = array();
            $msg['url']=$url;
            $msg['tags']=$tags;
            $msg['adult']=$adult;
            $msg['username']=$this->username;
            $msg['userpwd']=$this->password;
 
            if($password!='')
            {
                $msg['password']=$password;
            }
            $msg['donotrecycle']=$donotrecycle;
            $msg['testmode']=$testmode;
            $msg['description']=$description;
 
            //do the actual request
            $response = $this->doRequest('addUrl',$msg);
 
            //if it was a success ,return the new url, otherwise return the original url.
            if($response->type=='success')
            {
                return $response->dataNodes->url->data;
            }
            else 
            {
                return $url;
            }
 
 
    }
    /**
     * Does a request to shortner, don't fiddle with this ;)
     *
     * @see http://php.net/curl
     * @param string $command
     * @param array $post
     * @return object
     */
    private function doRequest($command,$post=array())
    {
        $ch = curl_init();
        if($this->session)
        {
            $post['sessionid']=$this->session;
        }
        else 
        {
            if(!isset($post['userpwd']))
            {
 
                $post['username']=$this->username;
                $post['password']=$this->password;
            }
        }
        curl_setopt($ch, CURLOPT_URL, $this->baseurl.$command.".".$this->format);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $response = curl_exec($ch);
        curl_close($ch);
        $responseobj = json_decode($response);
 
        return $responseobj;
    }
}
 
 
/**
 * Example usage
 */
 
 
/* uncomment the following code to use the example.. please fill in your own apikey and username/password*/
 
//$domain='shortner.com';
//$apikey='somethingyougotfromshortner';
//$username='someuser';
//$password='secret'; //seriously if this is your password go hide in shame
//$shortner = new shortner_rest($domain,$apikey,$username,$password);
//$url='http://www.blog.johnbakker.name/';
//$newurl = $shortner->addUrl($url,$title);
 
example/php.txt · Last modified: 2011/10/15 19:11 (external edit)
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki