[eluser]seth.aldridge[/eluser]
Hi,
I just recently put together this quick Library that makes calls to the Freshbooks API...but it could really make calls to any api now that I think about it...but I have only tested with the Freshbooks API.
Save in Library folder as freshbooks.php:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Freshbooks {
function loop($request)
{
// Define request, username, password, and api url
define('XML_PAYLOAD',$request);
define('XML_POST_USER','TOKEN_ID');
define('XML_POST_PASS', 'X');
define('XML_POST_URL', 'https://example.freshbooks.com/api/2.1/xml-in');
// cURL to make it rain
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_USERPWD, XML_POST_USER.":".XML_POST_PASS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
// Run timer to see how long the total result takes.
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch);
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;
// Check for errors
if ( curl_errno($ch) ) {
$result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 200:
break;
default:
$result = 'HTTP ERROR -> ' . $returnCode;
break;
}
}
// Close the cURL stuff that just rained
curl_close($ch);
// Return the result
return $result;
// Probably will need to delete this
exit(0);
}
}
?>
Then the controller:
Code:
<?
class Update_billing extends Controller {
function Update_billing()
{
parent::Controller();
}
function index()
{
$this->load->library('freshbooks');
$request = '<?xml version="1.0" encoding="utf-8"?><request method="recurring.get"><recurring_id>1</recurring_id></request>';
$result = $this->freshbooks->loop($request);
echo $result;
}
}
This is my first anything when it comes to PHP so I'm really open to critiques. A lot of the code is pulled together from different forms and I'm still Googling what some of it does
Thanks,
Seth