* * Copyright (c) 2008 sLajax.com * * Licensed under the MIT License: * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Read me: This class is a simple php interface for the api. * You must provide your api url and api token in the constructor. * It is object oriented so you can subsitiute xml with any source. * At this time there is no support for the code, but we * are happy to provide consultation on system integration with freshbooks. * *******************************************************************************/ class freshbooks { function freshbooks(){ define("_APIURL",""); define("_APITOKEN",""); define("_VERSION","sLajax.com - Freshbooks PHP API Client: Version: 0.1"); } /* wrapper methods will follow this format */ function getClientInvoices($uid=false){ if($uid) return $this->sendRequest( $this->xml('invoice.list', array("client_id"=>$uid)) ); else return "You must provide a client uid"; } function sendRequest($vars=false) { return $this->apiRequest('POST',$vars, $callback); } function setApiCallback($callback,$static=false){ $this->curlCallback = $callback; if($static) $this->curlStatic=true; } function apiRequest($method, $vars) { $this->setApiCallback('apiResponseHandler'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, _APIURL); curl_setopt($ch, CURLOPT_USERPWD, _APITOKEN); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 4); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE); curl_setopt($ch, CURLOPT_USERAGENT, _VERSION); if ($method == 'POST') { curl_setopt($ch, CURLOPT_POST, 1); if($vars) curl_setopt($ch, CURLOPT_POSTFIELDS, $vars); } $data = curl_exec($ch); curl_close($ch); if ($data) { if ($this->curlCallback) { $callback = $this->curlCallback; if(!$this->curlStatic)$this->curlCallback = false; return $this->$callback($data); } else { return new SimpleXMLElement($data); } } else { return curl_error($ch); } } function xml($method,$nodes=false){ $xml_data = "" ."" .$this->buildXML($nodes) .""; return $xml_data; } function buildXML($nodes){ if(!empty($nodes)) foreach($nodes as $k => $v) $xml_data .= "<".$k.">".$v.""; else $xml_data=''; return $xml_data; } function apiResponseHandler($xml){ return new SimpleXMLElement($xml); } function printPre($nodes){ print "
";
        print_r($nodes);
        print "
"; } } $f=new freshbooks(); print "simple invoices list example
"; $f->printPre( $f->sendRequest( $f->xml('invoice.list') ) ); print "invoices list with sub node passed
"; $f->printPre( $f->getClientInvoices(7) ); ?>