*
* Copyright (c) 2007 FreshBooks (a service of 2ndSite Inc.)
*
* 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.
*
*******************************************************************************/
function sendRequest($api_url, $token, $xml_request)
{
// Submit curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_USERPWD, $token);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return as variable
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); // Validate SSL certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "FreshBooks API tester 1.0");
$curl_result = curl_exec($ch);
curl_close ($ch);
// Fail if no result
if (strlen($curl_result) < 2)
return "Could not connect to server.";
//
// This doesn't work very well yet. If you'd like to fix it
// to make it recursive, and then contribute the improvements
// back to the FreshBooks community, make it so! Just e-mail
// me your fix and I'll be happy to update the code.
//
if ($_POST['arrayify'])
{
// Split string result so $array['key'] returns 'value'
preg_match_all ("/<(.*?)>(.*?)\", $curl_result, $curl_array, PREG_SET_ORDER);
for ( $n = 0; isset($curl_array[$n]); $n++ )
{
$token_array[$curl_array[$n][1]] = strip_tags($curl_array[$n][0]);
}
// Return array
return print_r($token_array, TRUE);
}
// Return text
return $curl_result;
}
function cleanOutput($output)
{
$search = array("<", ">", "bold_open_tag", "bold_close_tag" );
$replace = array("bold_open_tag<", ">bold_close_tag", "", "");
$output = stripslashes($output);
$output = str_replace($search, $replace, $output);
return $output;
}
//
// Set defaults
//
$api_url = "API URL";
$token = "Authentication token";
$xml_request = "\n