* * 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 ("/<(.*?)>(.*?)\", "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\n"; $xml_response = ""; $arrayify = null; // If form was submitted, create and submit XML request if ( isset($_POST['submit']) ) { $api_url = stripslashes( $_POST['api_url'] ); $token_field_name = $_POST['token_field_name']; $token = stripslashes( $_POST[$token_field_name] ); $xml_request = stripslashes( $_POST['xml_request'] ); $xml_response = sendRequest($api_url, $token, $xml_request); if ($_POST['arrayify']) $arrayify = "1"; } // Generate a new name for the authentication token field $token_field_name = md5( time() . rand() ); // // Start the output // header("Content-Type: text/html; charset=utf-8"); ?> API Tester

:

  /> Print response as array (doesn't work very well yet)